|
13 | 13 | import unittest |
14 | 14 | import warnings |
15 | 15 | from test.support import no_tracing, verbose, requires_subprocess, requires_resource |
| 16 | +from test import support |
| 17 | +from test.support import os_helper |
16 | 18 | from test.support.import_helper import forget, make_legacy_pyc, unload |
17 | 19 | from test.support.os_helper import create_empty_file, temp_dir |
18 | 20 | from test.support.script_helper import make_script, make_zip_script |
@@ -835,5 +837,95 @@ def test_pymain_run_module(self): |
835 | 837 | self.assertSigInt(["-m", ham.stem], cwd=ham.parent) |
836 | 838 |
|
837 | 839 |
|
| 840 | +class MiscTests(unittest.TestCase): |
| 841 | + def test_remove_sys_path0(self): |
| 842 | + # Check that all path0 copies are removed |
| 843 | + PATH0 = '/path/zero' |
| 844 | + STDLIB_PATH = '/to/stdlib' |
| 845 | + OTHER_PATH = '/other/path' |
| 846 | + sys_path = [PATH0, STDLIB_PATH, PATH0, '', OTHER_PATH, PATH0] |
| 847 | + expected = [STDLIB_PATH, '', OTHER_PATH] |
| 848 | + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), |
| 849 | + support.swap_attr(sys, '_path0', PATH0), |
| 850 | + support.swap_attr(sys, 'path', sys_path)): |
| 851 | + runpy._remove_sys_path0() |
| 852 | + self.assertEqual(sys.path, expected) |
| 853 | + |
| 854 | + # Calling it twice does nothing |
| 855 | + runpy._remove_sys_path0() |
| 856 | + self.assertEqual(sys.path, expected) |
| 857 | + |
| 858 | + # Do nothing if sys._path0 is sys._stdlib_dir |
| 859 | + sys_path = ['', STDLIB_PATH, OTHER_PATH] |
| 860 | + expected = sys_path.copy() |
| 861 | + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), |
| 862 | + support.swap_attr(sys, '_path0', STDLIB_PATH), |
| 863 | + support.swap_attr(sys, 'path', sys_path)): |
| 864 | + runpy._remove_sys_path0() |
| 865 | + self.assertEqual(sys.path, expected) |
| 866 | + |
| 867 | + # Do nothing if sys._path0 is None |
| 868 | + sys_path = ['', STDLIB_PATH, OTHER_PATH] |
| 869 | + expected = sys_path.copy() |
| 870 | + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), |
| 871 | + support.swap_attr(sys, '_path0', None), |
| 872 | + support.swap_attr(sys, 'path', sys_path)): |
| 873 | + runpy._remove_sys_path0() |
| 874 | + self.assertEqual(sys.path, expected) |
| 875 | + |
| 876 | + def run_python(self, *args, PYTHONPATH=None): |
| 877 | + cmd = [sys.executable, *args] |
| 878 | + env = support.make_clean_env() |
| 879 | + if PYTHONPATH: |
| 880 | + env['PYTHONPATH'] = PYTHONPATH |
| 881 | + proc = subprocess.run(cmd, text=True, stderr=subprocess.PIPE, env=env) |
| 882 | + self.assertEqual(proc.returncode, 0, proc.stderr) |
| 883 | + |
| 884 | + @support.requires_subprocess() |
| 885 | + def test_remove_sys_path0_run_python(self): |
| 886 | + CODE = textwrap.dedent(""" |
| 887 | + import runpy, sys |
| 888 | +
|
| 889 | + # Remove sys.path[0] |
| 890 | + orig_path = sys.path.copy() |
| 891 | + path0 = sys._path0 |
| 892 | + runpy._remove_sys_path0() |
| 893 | +
|
| 894 | + if path0 is not None and path0 in sys.path: |
| 895 | + raise Exception(f"path0 {path0!r} found in sys.path {sys.path!r}") |
| 896 | + if not sys.flags.safe_path: |
| 897 | + if sys.path == orig_path: |
| 898 | + raise Exception("sys.path was not changed") |
| 899 | + else: |
| 900 | + if sys.path != orig_path: |
| 901 | + raise Exception("sys.path was changed") |
| 902 | +
|
| 903 | + # Calling it twice must do nothing |
| 904 | + clean_path = sys.path.copy() |
| 905 | + runpy._remove_sys_path0() |
| 906 | + if sys.path != clean_path: |
| 907 | + raise Exception("sys.path was changed") |
| 908 | + """) |
| 909 | + |
| 910 | + # Test "python -c code" |
| 911 | + self.run_python('-c', CODE) |
| 912 | + self.run_python('-P', '-c', CODE) |
| 913 | + |
| 914 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 915 | + name = 'test_remove_sys_path0' |
| 916 | + filename = os.path.join(tmpdir, f'{name}.py') |
| 917 | + with open(filename, 'w') as fp: |
| 918 | + fp.write(CODE) |
| 919 | + |
| 920 | + # Test "python script.py" |
| 921 | + self.run_python(filename) |
| 922 | + self.run_python('-P', filename) |
| 923 | + |
| 924 | + with os_helper.change_cwd(tmpdir): |
| 925 | + # Test "python -m module" |
| 926 | + self.run_python('-m', name) |
| 927 | + self.run_python('-P', '-m', name, PYTHONPATH=tmpdir) |
| 928 | + |
| 929 | + |
838 | 930 | if __name__ == "__main__": |
839 | 931 | unittest.main() |
0 commit comments