bpo-29723: Consistently configure sys.path[0] (#575) · python/cpython@d2977a3 (original) (raw)

`@@ -572,6 +572,73 @@ def test_syntaxerror_indented_caret_position(self):

`

572

572

`self.assertNotIn("\f", text)

`

573

573

`self.assertIn("\n 1 + 1 = 2\n ^", text)

`

574

574

``

``

575

`+

def test_consistent_sys_path_for_direct_execution(self):

`

``

576

`+

This test case ensures that the following all give the same

`

``

577

`+

sys.path configuration:

`

``

578

`+

`

``

579

`+

./python -s script_dir/main.py

`

``

580

`+

./python -s script_dir

`

``

581

`+

./python -I script_dir

`

``

582

`+

script = textwrap.dedent("""\

`

``

583

`+

import sys

`

``

584

`+

for entry in sys.path:

`

``

585

`+

print(entry)

`

``

586

`+

""")

`

``

587

`+

Always show full path diffs on errors

`

``

588

`+

self.maxDiff = None

`

``

589

`+

with support.temp_dir() as work_dir, support.temp_dir() as script_dir:

`

``

590

`+

script_name = _make_test_script(script_dir, 'main', script)

`

``

591

`+

Reference output comes from directly executing main.py

`

``

592

`+

We omit PYTHONPATH and user site to align with isolated mode

`

``

593

`+

p = spawn_python("-Es", script_name, cwd=work_dir)

`

``

594

`+

out_by_name = kill_python(p).decode().splitlines()

`

``

595

`+

self.assertEqual(out_by_name[0], script_dir)

`

``

596

`+

self.assertNotIn(work_dir, out_by_name)

`

``

597

`+

Directory execution should give the same output

`

``

598

`+

p = spawn_python("-Es", script_dir, cwd=work_dir)

`

``

599

`+

out_by_dir = kill_python(p).decode().splitlines()

`

``

600

`+

self.assertEqual(out_by_dir, out_by_name)

`

``

601

`+

As should directory execution in isolated mode

`

``

602

`+

p = spawn_python("-I", script_dir, cwd=work_dir)

`

``

603

`+

out_by_dir_isolated = kill_python(p).decode().splitlines()

`

``

604

`+

self.assertEqual(out_by_dir_isolated, out_by_dir, out_by_name)

`

``

605

+

``

606

`+

def test_consistent_sys_path_for_module_execution(self):

`

``

607

`+

This test case ensures that the following both give the same

`

``

608

`+

sys.path configuration:

`

``

609

`+

./python -sm script_pkg.main

`

``

610

`+

./python -sm script_pkg

`

``

611

`+

`

``

612

`+

And that this fails as unable to find the package:

`

``

613

`+

./python -Im script_pkg

`

``

614

`+

script = textwrap.dedent("""\

`

``

615

`+

import sys

`

``

616

`+

for entry in sys.path:

`

``

617

`+

print(entry)

`

``

618

`+

""")

`

``

619

`+

Always show full path diffs on errors

`

``

620

`+

self.maxDiff = None

`

``

621

`+

with support.temp_dir() as work_dir:

`

``

622

`+

script_dir = os.path.join(work_dir, "script_pkg")

`

``

623

`+

os.mkdir(script_dir)

`

``

624

`+

script_name = _make_test_script(script_dir, 'main', script)

`

``

625

`` +

Reference output comes from -m script_pkg.__main__

``

``

626

`+

We omit PYTHONPATH and user site to better align with the

`

``

627

`+

direct execution test cases

`

``

628

`+

p = spawn_python("-sm", "script_pkg.main", cwd=work_dir)

`

``

629

`+

out_by_module = kill_python(p).decode().splitlines()

`

``

630

`+

self.assertEqual(out_by_module[0], '')

`

``

631

`+

self.assertNotIn(script_dir, out_by_module)

`

``

632

`+

Package execution should give the same output

`

``

633

`+

p = spawn_python("-sm", "script_pkg", cwd=work_dir)

`

``

634

`+

out_by_package = kill_python(p).decode().splitlines()

`

``

635

`+

self.assertEqual(out_by_package, out_by_module)

`

``

636

`+

Isolated mode should fail with an import error

`

``

637

`+

exitcode, stdout, stderr = assert_python_failure(

`

``

638

`+

"-Im", "script_pkg", cwd=work_dir

`

``

639

`+

)

`

``

640

`+

traceback_lines = stderr.decode().splitlines()

`

``

641

`+

self.assertIn("No module named script_pkg", traceback_lines[-1])

`

575

642

``

576

643

`def test_main():

`

577

644

`support.run_unittest(CmdLineTest)

`