Skip to content

Commit 329afe7

Browse files
authored
gh-57684: Update tests for PYTHONSAFEPATH=1 (#92358)
Fix tests failing with the PYTHONSAFEPATH=1 env var. Enhance also -P help in Python usage (python --help).
1 parent 1303f8c commit 329afe7

File tree

8 files changed

+33
-15
lines changed

8 files changed

+33
-15
lines changed

Lib/distutils/tests/test_bdist_rpm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def tearDown(self):
4949
'the rpm command is not found')
5050
@unittest.skipIf(find_executable('rpmbuild') is None,
5151
'the rpmbuild command is not found')
52+
# import foo fails with safe path
53+
@unittest.skipIf(sys.flags.safe_path,
54+
'PYTHONSAFEPATH changes default sys.path')
5255
def test_quiet(self):
5356
# let's create a package
5457
tmp_dir = self.mkdtemp()
@@ -93,6 +96,9 @@ def test_quiet(self):
9396
'the rpm command is not found')
9497
@unittest.skipIf(find_executable('rpmbuild') is None,
9598
'the rpmbuild command is not found')
99+
# import foo fails with safe path
100+
@unittest.skipIf(sys.flags.safe_path,
101+
'PYTHONSAFEPATH changes default sys.path')
96102
def test_no_optimize_flag(self):
97103
# let's create a package that breaks bdist_rpm
98104
tmp_dir = self.mkdtemp()

Lib/test/test_cmd_line.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ def test_large_PYTHONPATH(self):
363363
self.assertIn(path1.encode('ascii'), out)
364364
self.assertIn(path2.encode('ascii'), out)
365365

366+
@unittest.skipIf(sys.flags.safe_path,
367+
'PYTHONSAFEPATH changes default sys.path')
366368
def test_empty_PYTHONPATH_issue16309(self):
367369
# On Posix, it is documented that setting PATH to the
368370
# empty string is equivalent to not setting PATH at all,
@@ -594,9 +596,10 @@ def test_isolatedmode(self):
594596
with open(main, "w", encoding="utf-8") as f:
595597
f.write("import uuid\n")
596598
f.write("print('ok')\n")
599+
# Use -E to ignore PYTHONSAFEPATH env var
597600
self.assertRaises(subprocess.CalledProcessError,
598601
subprocess.check_output,
599-
[sys.executable, main], cwd=tmpdir,
602+
[sys.executable, '-E', main], cwd=tmpdir,
600603
stderr=subprocess.DEVNULL)
601604
out = subprocess.check_output([sys.executable, "-I", main],
602605
cwd=tmpdir)

Lib/test/test_cmd_line_script.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ def _check_output(self, script_name, exit_code, data,
116116
self.assertIn(printed_file.encode('utf-8'), data)
117117
self.assertIn(printed_package.encode('utf-8'), data)
118118
self.assertIn(printed_argv0.encode('utf-8'), data)
119-
self.assertIn(printed_path0.encode('utf-8'), data)
119+
# PYTHONSAFEPATH=1 changes the default sys.path[0]
120+
if not sys.flags.safe_path:
121+
self.assertIn(printed_path0.encode('utf-8'), data)
120122
self.assertIn(printed_cwd.encode('utf-8'), data)
121123

122124
def _check_script(self, script_exec_args, expected_file,

Lib/test/test_pdb.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,8 @@ class PdbTestCase(unittest.TestCase):
13691369
def tearDown(self):
13701370
os_helper.unlink(os_helper.TESTFN)
13711371

1372+
@unittest.skipIf(sys.flags.safe_path,
1373+
'PYTHONSAFEPATH changes default sys.path')
13721374
def _run_pdb(self, pdb_args, commands):
13731375
self.addCleanup(os_helper.rmtree, '__pycache__')
13741376
cmd = [sys.executable, '-m', 'pdb'] + pdb_args

Lib/test/test_runpy.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,15 @@ def run(self, *args, **kwargs):
781781
super().run(*args, **kwargs)
782782

783783
@requires_subprocess()
784-
def assertSigInt(self, *args, **kwargs):
785-
proc = subprocess.run(*args, **kwargs, text=True, stderr=subprocess.PIPE)
786-
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
784+
def assertSigInt(self, cmd, *args, **kwargs):
785+
# Use -E to ignore PYTHONSAFEPATH
786+
cmd = [sys.executable, '-E', *cmd]
787+
proc = subprocess.run(cmd, *args, **kwargs, text=True, stderr=subprocess.PIPE)
788+
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"), proc.stderr)
787789
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
788790

789791
def test_pymain_run_file(self):
790-
self.assertSigInt([sys.executable, self.ham])
792+
self.assertSigInt([self.ham])
791793

792794
def test_pymain_run_file_runpy_run_module(self):
793795
tmp = self.ham.parent
@@ -800,7 +802,7 @@ def test_pymain_run_file_runpy_run_module(self):
800802
"""
801803
)
802804
)
803-
self.assertSigInt([sys.executable, run_module], cwd=tmp)
805+
self.assertSigInt([run_module], cwd=tmp)
804806

805807
def test_pymain_run_file_runpy_run_module_as_main(self):
806808
tmp = self.ham.parent
@@ -813,23 +815,23 @@ def test_pymain_run_file_runpy_run_module_as_main(self):
813815
"""
814816
)
815817
)
816-
self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
818+
self.assertSigInt([run_module_as_main], cwd=tmp)
817819

818820
def test_pymain_run_command_run_module(self):
819821
self.assertSigInt(
820-
[sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
822+
["-c", "import runpy; runpy.run_module('ham')"],
821823
cwd=self.ham.parent,
822824
)
823825

824826
def test_pymain_run_command(self):
825-
self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
827+
self.assertSigInt(["-c", "import ham"], cwd=self.ham.parent)
826828

827829
def test_pymain_run_stdin(self):
828-
self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
830+
self.assertSigInt([], input="import ham", cwd=self.ham.parent)
829831

830832
def test_pymain_run_module(self):
831833
ham = self.ham
832-
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
834+
self.assertSigInt(["-m", ham.stem], cwd=ham.parent)
833835

834836

835837
if __name__ == "__main__":

Lib/unittest/test/test_program.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ def testParseArgsSelectedTestNames(self):
454454

455455
def testSelectedTestNamesFunctionalTest(self):
456456
def run_unittest(args):
457-
p = subprocess.Popen([sys.executable, '-m', 'unittest'] + args,
457+
# Use -E to ignore PYTHONSAFEPATH env var
458+
cmd = [sys.executable, '-E', '-m', 'unittest'] + args
459+
p = subprocess.Popen(cmd,
458460
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
459461
with p:
460462
_, stderr = p.communicate()

Python/initconfig.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static const char usage_2[] = "\
4949
.pyc extension; also PYTHONOPTIMIZE=x\n\
5050
-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
5151
.pyc extension\n\
52-
-P : don't add sys.path[0]\n\
52+
-P : don't prepend a potentially unsafe path to sys.path\n\
5353
-q : don't print version and copyright messages on interactive startup\n\
5454
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
5555
-S : don't imply 'import site' on initialization\n\

Tools/freeze/test/freeze.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def freeze(python, scriptfile, outdir):
172172

173173
print(f'freezing {scriptfile}...')
174174
os.makedirs(outdir, exist_ok=True)
175-
_run_quiet([python, FREEZE, '-o', outdir, scriptfile], outdir)
175+
# Use -E to ignore PYTHONSAFEPATH
176+
_run_quiet([python, '-E', FREEZE, '-o', outdir, scriptfile], outdir)
176177
_run_quiet([MAKE, '-C', os.path.dirname(scriptfile)])
177178

178179
name = os.path.basename(scriptfile).rpartition('.')[0]

0 commit comments

Comments
 (0)