@@ -598,12 +598,19 @@ def test_startup_interactivehook_isolated_explicit(self):
598
598
@unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
599
599
class _pthFileTests (unittest .TestCase ):
600
600
601
- def _create_underpth_exe (self , lines ):
601
+ def _create_underpth_exe (self , lines , exe_pth = True ):
602
+ import _winapi
602
603
temp_dir = tempfile .mkdtemp ()
603
604
self .addCleanup (test .support .rmtree , temp_dir )
604
605
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 ])
605
608
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'
607
614
with open (_pth_file , 'w' ) as f :
608
615
for line in lines :
609
616
print (line , file = f )
@@ -671,5 +678,30 @@ def test_underpth_file(self):
671
678
self .assertTrue (rc , "sys.path is incorrect" )
672
679
673
680
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
+
674
706
if __name__ == "__main__" :
675
707
unittest .main ()
0 commit comments