diff options
author | Steve Dower <steve.dower@python.org> | 2020-07-15 23:24:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-15 18:24:56 -0400 |
commit | 4bfcffe16e9742c154f54ae96b5b36903500abaa (patch) | |
tree | 8c24f10a0f6d1a0b2ef48dfaac56f387316e43ef | |
parent | bpo-39017: Avoid infinite loop in the tarfile module (GH-21454) (GH-21484) (diff) | |
download | cpython-4bfcffe16e9742c154f54ae96b5b36903500abaa.tar.gz cpython-4bfcffe16e9742c154f54ae96b5b36903500abaa.tar.bz2 cpython-4bfcffe16e9742c154f54ae96b5b36903500abaa.zip |
bpo-41304: Ensure python3x._pth is loaded on Windows (GH-21495) (#21499)
-rw-r--r-- | Lib/test/test_site.py | 36 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Security/2020-07-15-20-15-08.bpo-41304.vNEeYA.rst | 1 | ||||
-rw-r--r-- | PC/getpathp.c | 2 |
3 files changed, 36 insertions, 3 deletions
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 6def4e59f4e..8815c839984 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -573,12 +573,19 @@ class StartupImportTests(unittest.TestCase): @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows") class _pthFileTests(unittest.TestCase): - def _create_underpth_exe(self, lines): + def _create_underpth_exe(self, lines, exe_pth=True): + import _winapi temp_dir = tempfile.mkdtemp() self.addCleanup(test.support.rmtree, temp_dir) exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1]) + dll_src_file = _winapi.GetModuleFileName(sys.dllhandle) + dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1]) shutil.copy(sys.executable, exe_file) - _pth_file = os.path.splitext(exe_file)[0] + '._pth' + shutil.copy(dll_src_file, dll_file) + if exe_pth: + _pth_file = os.path.splitext(exe_file)[0] + '._pth' + else: + _pth_file = os.path.splitext(dll_file)[0] + '._pth' with open(_pth_file, 'w') as f: for line in lines: print(line, file=f) @@ -646,5 +653,30 @@ class _pthFileTests(unittest.TestCase): self.assertTrue(rc, "sys.path is incorrect") + def test_underpth_dll_file(self): + libpath = os.path.dirname(os.path.dirname(encodings.__file__)) + exe_prefix = os.path.dirname(sys.executable) + exe_file = self._create_underpth_exe([ + 'fake-path-name', + *[libpath for _ in range(200)], + '', + '# comment', + 'import site' + ], exe_pth=False) + sys_prefix = os.path.dirname(exe_file) + env = os.environ.copy() + env['PYTHONPATH'] = 'from-env' + env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH')) + rc = subprocess.call([exe_file, '-c', + 'import sys; sys.exit(not sys.flags.no_site and ' + '%r in sys.path and %r in sys.path and %r not in sys.path and ' + 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % ( + os.path.join(sys_prefix, 'fake-path-name'), + libpath, + os.path.join(sys_prefix, 'from-env'), + )], env=env) + self.assertTrue(rc, "sys.path is incorrect") + + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Security/2020-07-15-20-15-08.bpo-41304.vNEeYA.rst b/Misc/NEWS.d/next/Security/2020-07-15-20-15-08.bpo-41304.vNEeYA.rst new file mode 100644 index 00000000000..90423e9a665 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2020-07-15-20-15-08.bpo-41304.vNEeYA.rst @@ -0,0 +1 @@ +Fixes `python3x._pth` being ignored on Windows diff --git a/PC/getpathp.c b/PC/getpathp.c index f7022aea1f5..387ac60ac9c 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -673,7 +673,7 @@ calculate_init(PyCalculatePath *calculate, static int get_pth_filename(wchar_t *spbuffer, _PyPathConfig *config) { - if (get_dllpath(spbuffer) && + if (!get_dllpath(spbuffer) && !change_ext(spbuffer, spbuffer, L"._pth") && exists(spbuffer)) { |