bpo-41304: Ensure python3x._pth is loaded on Windows (GH-21495) · python/cpython@3b6a8d2 (original) (raw)
`@@ -581,12 +581,19 @@ def test_startup_interactivehook_isolated_explicit(self):
`
581
581
`@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
`
582
582
`class _pthFileTests(unittest.TestCase):
`
583
583
``
584
``
`-
def _create_underpth_exe(self, lines):
`
``
584
`+
def _create_underpth_exe(self, lines, exe_pth=True):
`
``
585
`+
import _winapi
`
585
586
`temp_dir = tempfile.mkdtemp()
`
586
587
`self.addCleanup(test.support.rmtree, temp_dir)
`
587
588
`exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
`
``
589
`+
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
`
``
590
`+
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
`
588
591
`shutil.copy(sys.executable, exe_file)
`
589
``
`-
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
`
``
592
`+
shutil.copy(dll_src_file, dll_file)
`
``
593
`+
if exe_pth:
`
``
594
`+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
`
``
595
`+
else:
`
``
596
`+
_pth_file = os.path.splitext(dll_file)[0] + '._pth'
`
590
597
`with open(_pth_file, 'w') as f:
`
591
598
`for line in lines:
`
592
599
`print(line, file=f)
`
`@@ -654,5 +661,30 @@ def test_underpth_file(self):
`
654
661
`self.assertTrue(rc, "sys.path is incorrect")
`
655
662
``
656
663
``
``
664
`+
def test_underpth_dll_file(self):
`
``
665
`+
libpath = os.path.dirname(os.path.dirname(encodings.file))
`
``
666
`+
exe_prefix = os.path.dirname(sys.executable)
`
``
667
`+
exe_file = self._create_underpth_exe([
`
``
668
`+
'fake-path-name',
`
``
669
`+
*[libpath for _ in range(200)],
`
``
670
`+
'',
`
``
671
`+
'# comment',
`
``
672
`+
'import site'
`
``
673
`+
], exe_pth=False)
`
``
674
`+
sys_prefix = os.path.dirname(exe_file)
`
``
675
`+
env = os.environ.copy()
`
``
676
`+
env['PYTHONPATH'] = 'from-env'
`
``
677
`+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
`
``
678
`+
rc = subprocess.call([exe_file, '-c',
`
``
679
`+
'import sys; sys.exit(not sys.flags.no_site and '
`
``
680
`+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
`
``
681
`+
'all("\r" not in p and "\n" not in p for p in sys.path))' % (
`
``
682
`+
os.path.join(sys_prefix, 'fake-path-name'),
`
``
683
`+
libpath,
`
``
684
`+
os.path.join(sys_prefix, 'from-env'),
`
``
685
`+
)], env=env)
`
``
686
`+
self.assertTrue(rc, "sys.path is incorrect")
`
``
687
+
``
688
+
657
689
`if name == "main":
`
658
690
`unittest.main()
`