@@ -573,12 +573,19 @@ def test_startup_interactivehook_isolated_explicit(self):
573573@unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
574574class _pthFileTests (unittest .TestCase ):
575575
576- def _create_underpth_exe (self , lines ):
576+ def _create_underpth_exe (self , lines , exe_pth = True ):
577+ import _winapi
577578 temp_dir = tempfile .mkdtemp ()
578579 self .addCleanup (test .support .rmtree , temp_dir )
579580 exe_file = os .path .join (temp_dir , os .path .split (sys .executable )[1 ])
581+ dll_src_file = _winapi .GetModuleFileName (sys .dllhandle )
582+ dll_file = os .path .join (temp_dir , os .path .split (dll_src_file )[1 ])
580583 shutil .copy (sys .executable , exe_file )
581- _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
584+ shutil .copy (dll_src_file , dll_file )
585+ if exe_pth :
586+ _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
587+ else :
588+ _pth_file = os .path .splitext (dll_file )[0 ] + '._pth'
582589 with open (_pth_file , 'w' ) as f :
583590 for line in lines :
584591 print (line , file = f )
@@ -646,5 +653,30 @@ def test_underpth_file(self):
646653 self .assertTrue (rc , "sys.path is incorrect" )
647654
648655
656+ def test_underpth_dll_file (self ):
657+ libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
658+ exe_prefix = os .path .dirname (sys .executable )
659+ exe_file = self ._create_underpth_exe ([
660+ 'fake-path-name' ,
661+ * [libpath for _ in range (200 )],
662+ '' ,
663+ '# comment' ,
664+ 'import site'
665+ ], exe_pth = False )
666+ sys_prefix = os .path .dirname (exe_file )
667+ env = os .environ .copy ()
668+ env ['PYTHONPATH' ] = 'from-env'
669+ env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
670+ rc = subprocess .call ([exe_file , '-c' ,
671+ 'import sys; sys.exit(not sys.flags.no_site and '
672+ '%r in sys.path and %r in sys.path and %r not in sys.path and '
673+ 'all("\\ r" not in p and "\\ n" not in p for p in sys.path))' % (
674+ os .path .join (sys_prefix , 'fake-path-name' ),
675+ libpath ,
676+ os .path .join (sys_prefix , 'from-env' ),
677+ )], env = env )
678+ self .assertTrue (rc , "sys.path is incorrect" )
679+
680+
649681if __name__ == "__main__" :
650682 unittest .main ()
0 commit comments