mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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).
This commit is contained in:
parent
1303f8c927
commit
329afe78c3
8 changed files with 33 additions and 15 deletions
|
@ -49,6 +49,9 @@ class BuildRpmTestCase(support.TempdirManager,
|
||||||
'the rpm command is not found')
|
'the rpm command is not found')
|
||||||
@unittest.skipIf(find_executable('rpmbuild') is None,
|
@unittest.skipIf(find_executable('rpmbuild') is None,
|
||||||
'the rpmbuild command is not found')
|
'the rpmbuild command is not found')
|
||||||
|
# import foo fails with safe path
|
||||||
|
@unittest.skipIf(sys.flags.safe_path,
|
||||||
|
'PYTHONSAFEPATH changes default sys.path')
|
||||||
def test_quiet(self):
|
def test_quiet(self):
|
||||||
# let's create a package
|
# let's create a package
|
||||||
tmp_dir = self.mkdtemp()
|
tmp_dir = self.mkdtemp()
|
||||||
|
@ -93,6 +96,9 @@ class BuildRpmTestCase(support.TempdirManager,
|
||||||
'the rpm command is not found')
|
'the rpm command is not found')
|
||||||
@unittest.skipIf(find_executable('rpmbuild') is None,
|
@unittest.skipIf(find_executable('rpmbuild') is None,
|
||||||
'the rpmbuild command is not found')
|
'the rpmbuild command is not found')
|
||||||
|
# import foo fails with safe path
|
||||||
|
@unittest.skipIf(sys.flags.safe_path,
|
||||||
|
'PYTHONSAFEPATH changes default sys.path')
|
||||||
def test_no_optimize_flag(self):
|
def test_no_optimize_flag(self):
|
||||||
# let's create a package that breaks bdist_rpm
|
# let's create a package that breaks bdist_rpm
|
||||||
tmp_dir = self.mkdtemp()
|
tmp_dir = self.mkdtemp()
|
||||||
|
|
|
@ -363,6 +363,8 @@ class CmdLineTest(unittest.TestCase):
|
||||||
self.assertIn(path1.encode('ascii'), out)
|
self.assertIn(path1.encode('ascii'), out)
|
||||||
self.assertIn(path2.encode('ascii'), out)
|
self.assertIn(path2.encode('ascii'), out)
|
||||||
|
|
||||||
|
@unittest.skipIf(sys.flags.safe_path,
|
||||||
|
'PYTHONSAFEPATH changes default sys.path')
|
||||||
def test_empty_PYTHONPATH_issue16309(self):
|
def test_empty_PYTHONPATH_issue16309(self):
|
||||||
# On Posix, it is documented that setting PATH to the
|
# On Posix, it is documented that setting PATH to the
|
||||||
# empty string is equivalent to not setting PATH at all,
|
# empty string is equivalent to not setting PATH at all,
|
||||||
|
@ -594,9 +596,10 @@ class CmdLineTest(unittest.TestCase):
|
||||||
with open(main, "w", encoding="utf-8") as f:
|
with open(main, "w", encoding="utf-8") as f:
|
||||||
f.write("import uuid\n")
|
f.write("import uuid\n")
|
||||||
f.write("print('ok')\n")
|
f.write("print('ok')\n")
|
||||||
|
# Use -E to ignore PYTHONSAFEPATH env var
|
||||||
self.assertRaises(subprocess.CalledProcessError,
|
self.assertRaises(subprocess.CalledProcessError,
|
||||||
subprocess.check_output,
|
subprocess.check_output,
|
||||||
[sys.executable, main], cwd=tmpdir,
|
[sys.executable, '-E', main], cwd=tmpdir,
|
||||||
stderr=subprocess.DEVNULL)
|
stderr=subprocess.DEVNULL)
|
||||||
out = subprocess.check_output([sys.executable, "-I", main],
|
out = subprocess.check_output([sys.executable, "-I", main],
|
||||||
cwd=tmpdir)
|
cwd=tmpdir)
|
||||||
|
|
|
@ -116,7 +116,9 @@ class CmdLineTest(unittest.TestCase):
|
||||||
self.assertIn(printed_file.encode('utf-8'), data)
|
self.assertIn(printed_file.encode('utf-8'), data)
|
||||||
self.assertIn(printed_package.encode('utf-8'), data)
|
self.assertIn(printed_package.encode('utf-8'), data)
|
||||||
self.assertIn(printed_argv0.encode('utf-8'), data)
|
self.assertIn(printed_argv0.encode('utf-8'), data)
|
||||||
self.assertIn(printed_path0.encode('utf-8'), data)
|
# PYTHONSAFEPATH=1 changes the default sys.path[0]
|
||||||
|
if not sys.flags.safe_path:
|
||||||
|
self.assertIn(printed_path0.encode('utf-8'), data)
|
||||||
self.assertIn(printed_cwd.encode('utf-8'), data)
|
self.assertIn(printed_cwd.encode('utf-8'), data)
|
||||||
|
|
||||||
def _check_script(self, script_exec_args, expected_file,
|
def _check_script(self, script_exec_args, expected_file,
|
||||||
|
|
|
@ -1369,6 +1369,8 @@ class PdbTestCase(unittest.TestCase):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
os_helper.unlink(os_helper.TESTFN)
|
os_helper.unlink(os_helper.TESTFN)
|
||||||
|
|
||||||
|
@unittest.skipIf(sys.flags.safe_path,
|
||||||
|
'PYTHONSAFEPATH changes default sys.path')
|
||||||
def _run_pdb(self, pdb_args, commands):
|
def _run_pdb(self, pdb_args, commands):
|
||||||
self.addCleanup(os_helper.rmtree, '__pycache__')
|
self.addCleanup(os_helper.rmtree, '__pycache__')
|
||||||
cmd = [sys.executable, '-m', 'pdb'] + pdb_args
|
cmd = [sys.executable, '-m', 'pdb'] + pdb_args
|
||||||
|
|
|
@ -781,13 +781,15 @@ class TestExit(unittest.TestCase):
|
||||||
super().run(*args, **kwargs)
|
super().run(*args, **kwargs)
|
||||||
|
|
||||||
@requires_subprocess()
|
@requires_subprocess()
|
||||||
def assertSigInt(self, *args, **kwargs):
|
def assertSigInt(self, cmd, *args, **kwargs):
|
||||||
proc = subprocess.run(*args, **kwargs, text=True, stderr=subprocess.PIPE)
|
# Use -E to ignore PYTHONSAFEPATH
|
||||||
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
|
cmd = [sys.executable, '-E', *cmd]
|
||||||
|
proc = subprocess.run(cmd, *args, **kwargs, text=True, stderr=subprocess.PIPE)
|
||||||
|
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"), proc.stderr)
|
||||||
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
|
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
|
||||||
|
|
||||||
def test_pymain_run_file(self):
|
def test_pymain_run_file(self):
|
||||||
self.assertSigInt([sys.executable, self.ham])
|
self.assertSigInt([self.ham])
|
||||||
|
|
||||||
def test_pymain_run_file_runpy_run_module(self):
|
def test_pymain_run_file_runpy_run_module(self):
|
||||||
tmp = self.ham.parent
|
tmp = self.ham.parent
|
||||||
|
@ -800,7 +802,7 @@ class TestExit(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertSigInt([sys.executable, run_module], cwd=tmp)
|
self.assertSigInt([run_module], cwd=tmp)
|
||||||
|
|
||||||
def test_pymain_run_file_runpy_run_module_as_main(self):
|
def test_pymain_run_file_runpy_run_module_as_main(self):
|
||||||
tmp = self.ham.parent
|
tmp = self.ham.parent
|
||||||
|
@ -813,23 +815,23 @@ class TestExit(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
|
self.assertSigInt([run_module_as_main], cwd=tmp)
|
||||||
|
|
||||||
def test_pymain_run_command_run_module(self):
|
def test_pymain_run_command_run_module(self):
|
||||||
self.assertSigInt(
|
self.assertSigInt(
|
||||||
[sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
|
["-c", "import runpy; runpy.run_module('ham')"],
|
||||||
cwd=self.ham.parent,
|
cwd=self.ham.parent,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_pymain_run_command(self):
|
def test_pymain_run_command(self):
|
||||||
self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
|
self.assertSigInt(["-c", "import ham"], cwd=self.ham.parent)
|
||||||
|
|
||||||
def test_pymain_run_stdin(self):
|
def test_pymain_run_stdin(self):
|
||||||
self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
|
self.assertSigInt([], input="import ham", cwd=self.ham.parent)
|
||||||
|
|
||||||
def test_pymain_run_module(self):
|
def test_pymain_run_module(self):
|
||||||
ham = self.ham
|
ham = self.ham
|
||||||
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
|
self.assertSigInt(["-m", ham.stem], cwd=ham.parent)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -454,7 +454,9 @@ class TestCommandLineArgs(unittest.TestCase):
|
||||||
|
|
||||||
def testSelectedTestNamesFunctionalTest(self):
|
def testSelectedTestNamesFunctionalTest(self):
|
||||||
def run_unittest(args):
|
def run_unittest(args):
|
||||||
p = subprocess.Popen([sys.executable, '-m', 'unittest'] + args,
|
# Use -E to ignore PYTHONSAFEPATH env var
|
||||||
|
cmd = [sys.executable, '-E', '-m', 'unittest'] + args
|
||||||
|
p = subprocess.Popen(cmd,
|
||||||
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
|
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
|
||||||
with p:
|
with p:
|
||||||
_, stderr = p.communicate()
|
_, stderr = p.communicate()
|
||||||
|
|
|
@ -49,7 +49,7 @@ static const char usage_2[] = "\
|
||||||
.pyc extension; also PYTHONOPTIMIZE=x\n\
|
.pyc extension; also PYTHONOPTIMIZE=x\n\
|
||||||
-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
|
-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
|
||||||
.pyc extension\n\
|
.pyc extension\n\
|
||||||
-P : don't add sys.path[0]\n\
|
-P : don't prepend a potentially unsafe path to sys.path\n\
|
||||||
-q : don't print version and copyright messages on interactive startup\n\
|
-q : don't print version and copyright messages on interactive startup\n\
|
||||||
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
|
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
|
||||||
-S : don't imply 'import site' on initialization\n\
|
-S : don't imply 'import site' on initialization\n\
|
||||||
|
|
|
@ -172,7 +172,8 @@ def freeze(python, scriptfile, outdir):
|
||||||
|
|
||||||
print(f'freezing {scriptfile}...')
|
print(f'freezing {scriptfile}...')
|
||||||
os.makedirs(outdir, exist_ok=True)
|
os.makedirs(outdir, exist_ok=True)
|
||||||
_run_quiet([python, FREEZE, '-o', outdir, scriptfile], outdir)
|
# Use -E to ignore PYTHONSAFEPATH
|
||||||
|
_run_quiet([python, '-E', FREEZE, '-o', outdir, scriptfile], outdir)
|
||||||
_run_quiet([MAKE, '-C', os.path.dirname(scriptfile)])
|
_run_quiet([MAKE, '-C', os.path.dirname(scriptfile)])
|
||||||
|
|
||||||
name = os.path.basename(scriptfile).rpartition('.')[0]
|
name = os.path.basename(scriptfile).rpartition('.')[0]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue