|
20 | 20 | requires_subprocess, |
21 | 21 | verbose, |
22 | 22 | ) |
| 23 | +from test import support |
| 24 | +from test.support import os_helper |
23 | 25 | from test.support.import_helper import forget, make_legacy_pyc, unload |
24 | 26 | from test.support.os_helper import create_empty_file, temp_dir, FakePath |
25 | 27 | from test.support.script_helper import make_script, make_zip_script |
@@ -875,5 +877,95 @@ def test_pymain_run_module(self): |
875 | 877 | self.assertSigInt(["-m", ham.stem], cwd=ham.parent) |
876 | 878 |
|
877 | 879 |
|
| 880 | +class MiscTests(unittest.TestCase): |
| 881 | + def test_remove_sys_path0(self): |
| 882 | + # Check that all path0 copies are removed |
| 883 | + PATH0 = '/path/zero' |
| 884 | + STDLIB_PATH = '/to/stdlib' |
| 885 | + OTHER_PATH = '/other/path' |
| 886 | + sys_path = [PATH0, STDLIB_PATH, PATH0, '', OTHER_PATH, PATH0] |
| 887 | + expected = [STDLIB_PATH, '', OTHER_PATH] |
| 888 | + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), |
| 889 | + support.swap_attr(sys, '_path0', PATH0), |
| 890 | + support.swap_attr(sys, 'path', sys_path)): |
| 891 | + runpy._remove_sys_path0() |
| 892 | + self.assertEqual(sys.path, expected) |
| 893 | + |
| 894 | + # Calling it twice does nothing |
| 895 | + runpy._remove_sys_path0() |
| 896 | + self.assertEqual(sys.path, expected) |
| 897 | + |
| 898 | + # Do nothing if sys._path0 is sys._stdlib_dir |
| 899 | + sys_path = ['', STDLIB_PATH, OTHER_PATH] |
| 900 | + expected = sys_path.copy() |
| 901 | + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), |
| 902 | + support.swap_attr(sys, '_path0', STDLIB_PATH), |
| 903 | + support.swap_attr(sys, 'path', sys_path)): |
| 904 | + runpy._remove_sys_path0() |
| 905 | + self.assertEqual(sys.path, expected) |
| 906 | + |
| 907 | + # Do nothing if sys._path0 is None |
| 908 | + sys_path = ['', STDLIB_PATH, OTHER_PATH] |
| 909 | + expected = sys_path.copy() |
| 910 | + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), |
| 911 | + support.swap_attr(sys, '_path0', None), |
| 912 | + support.swap_attr(sys, 'path', sys_path)): |
| 913 | + runpy._remove_sys_path0() |
| 914 | + self.assertEqual(sys.path, expected) |
| 915 | + |
| 916 | + def run_python(self, *args, PYTHONPATH=None): |
| 917 | + cmd = [sys.executable, *args] |
| 918 | + env = support.make_clean_env() |
| 919 | + if PYTHONPATH: |
| 920 | + env['PYTHONPATH'] = PYTHONPATH |
| 921 | + proc = subprocess.run(cmd, text=True, stderr=subprocess.PIPE, env=env) |
| 922 | + self.assertEqual(proc.returncode, 0, proc.stderr) |
| 923 | + |
| 924 | + @support.requires_subprocess() |
| 925 | + def test_remove_sys_path0_run_python(self): |
| 926 | + CODE = textwrap.dedent(""" |
| 927 | + import runpy, sys |
| 928 | +
|
| 929 | + # Remove sys.path[0] |
| 930 | + orig_path = sys.path.copy() |
| 931 | + path0 = sys._path0 |
| 932 | + runpy._remove_sys_path0() |
| 933 | +
|
| 934 | + if path0 is not None and path0 in sys.path: |
| 935 | + raise Exception(f"path0 {path0!r} found in sys.path {sys.path!r}") |
| 936 | + if not sys.flags.safe_path: |
| 937 | + if sys.path == orig_path: |
| 938 | + raise Exception("sys.path was not changed") |
| 939 | + else: |
| 940 | + if sys.path != orig_path: |
| 941 | + raise Exception("sys.path was changed") |
| 942 | +
|
| 943 | + # Calling it twice must do nothing |
| 944 | + clean_path = sys.path.copy() |
| 945 | + runpy._remove_sys_path0() |
| 946 | + if sys.path != clean_path: |
| 947 | + raise Exception("sys.path was changed") |
| 948 | + """) |
| 949 | + |
| 950 | + # Test "python -c code" |
| 951 | + self.run_python('-c', CODE) |
| 952 | + self.run_python('-P', '-c', CODE) |
| 953 | + |
| 954 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 955 | + name = 'test_remove_sys_path0' |
| 956 | + filename = os.path.join(tmpdir, f'{name}.py') |
| 957 | + with open(filename, 'w') as fp: |
| 958 | + fp.write(CODE) |
| 959 | + |
| 960 | + # Test "python script.py" |
| 961 | + self.run_python(filename) |
| 962 | + self.run_python('-P', filename) |
| 963 | + |
| 964 | + with os_helper.change_cwd(tmpdir): |
| 965 | + # Test "python -m module" |
| 966 | + self.run_python('-m', name) |
| 967 | + self.run_python('-P', '-m', name, PYTHONPATH=tmpdir) |
| 968 | + |
| 969 | + |
878 | 970 | if __name__ == "__main__": |
879 | 971 | unittest.main() |
0 commit comments