@@ -598,12 +598,19 @@ def test_startup_interactivehook_isolated_explicit(self):
598598@unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
599599class _pthFileTests (unittest .TestCase ):
600600
601- def _create_underpth_exe (self , lines ):
601+ def _create_underpth_exe (self , lines , exe_pth = True ):
602+ import _winapi
602603 temp_dir = tempfile .mkdtemp ()
603604 self .addCleanup (test .support .rmtree , temp_dir )
604605 exe_file = os .path .join (temp_dir , os .path .split (sys .executable )[1 ])
606+ dll_src_file = _winapi .GetModuleFileName (sys .dllhandle )
607+ dll_file = os .path .join (temp_dir , os .path .split (dll_src_file )[1 ])
605608 shutil .copy (sys .executable , exe_file )
606- _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
609+ shutil .copy (dll_src_file , dll_file )
610+ if exe_pth :
611+ _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
612+ else :
613+ _pth_file = os .path .splitext (dll_file )[0 ] + '._pth'
607614 with open (_pth_file , 'w' ) as f :
608615 for line in lines :
609616 print (line , file = f )
@@ -671,5 +678,30 @@ def test_underpth_file(self):
671678 self .assertTrue (rc , "sys.path is incorrect" )
672679
673680
681+ def test_underpth_dll_file (self ):
682+ libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
683+ exe_prefix = os .path .dirname (sys .executable )
684+ exe_file = self ._create_underpth_exe ([
685+ 'fake-path-name' ,
686+ * [libpath for _ in range (200 )],
687+ '' ,
688+ '# comment' ,
689+ 'import site'
690+ ], exe_pth = False )
691+ sys_prefix = os .path .dirname (exe_file )
692+ env = os .environ .copy ()
693+ env ['PYTHONPATH' ] = 'from-env'
694+ env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
695+ rc = subprocess .call ([exe_file , '-c' ,
696+ 'import sys; sys.exit(not sys.flags.no_site and '
697+ '%r in sys.path and %r in sys.path and %r not in sys.path and '
698+ 'all("\\ r" not in p and "\\ n" not in p for p in sys.path))' % (
699+ os .path .join (sys_prefix , 'fake-path-name' ),
700+ libpath ,
701+ os .path .join (sys_prefix , 'from-env' ),
702+ )], env = env )
703+ self .assertTrue (rc , "sys.path is incorrect" )
704+
705+
674706if __name__ == "__main__" :
675707 unittest .main ()
0 commit comments