@@ -589,12 +589,19 @@ def test_startup_interactivehook_isolated_explicit(self):
589589@unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
590590class _pthFileTests (unittest .TestCase ):
591591
592- def _create_underpth_exe (self , lines ):
592+ def _create_underpth_exe (self , lines , exe_pth = True ):
593+ import _winapi
593594 temp_dir = tempfile .mkdtemp ()
594595 self .addCleanup (test .support .rmtree , temp_dir )
595596 exe_file = os .path .join (temp_dir , os .path .split (sys .executable )[1 ])
597+ dll_src_file = _winapi .GetModuleFileName (sys .dllhandle )
598+ dll_file = os .path .join (temp_dir , os .path .split (dll_src_file )[1 ])
596599 shutil .copy (sys .executable , exe_file )
597- _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
600+ shutil .copy (dll_src_file , dll_file )
601+ if exe_pth :
602+ _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
603+ else :
604+ _pth_file = os .path .splitext (dll_file )[0 ] + '._pth'
598605 with open (_pth_file , 'w' ) as f :
599606 for line in lines :
600607 print (line , file = f )
@@ -662,5 +669,30 @@ def test_underpth_file(self):
662669 self .assertTrue (rc , "sys.path is incorrect" )
663670
664671
672+ def test_underpth_dll_file (self ):
673+ libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
674+ exe_prefix = os .path .dirname (sys .executable )
675+ exe_file = self ._create_underpth_exe ([
676+ 'fake-path-name' ,
677+ * [libpath for _ in range (200 )],
678+ '' ,
679+ '# comment' ,
680+ 'import site'
681+ ], exe_pth = False )
682+ sys_prefix = os .path .dirname (exe_file )
683+ env = os .environ .copy ()
684+ env ['PYTHONPATH' ] = 'from-env'
685+ env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
686+ rc = subprocess .call ([exe_file , '-c' ,
687+ 'import sys; sys.exit(not sys.flags.no_site and '
688+ '%r in sys.path and %r in sys.path and %r not in sys.path and '
689+ 'all("\\ r" not in p and "\\ n" not in p for p in sys.path))' % (
690+ os .path .join (sys_prefix , 'fake-path-name' ),
691+ libpath ,
692+ os .path .join (sys_prefix , 'from-env' ),
693+ )], env = env )
694+ self .assertTrue (rc , "sys.path is incorrect" )
695+
696+
665697if __name__ == "__main__" :
666698 unittest .main ()
0 commit comments