17
17
import shutil
18
18
import subprocess
19
19
import sysconfig
20
- import tempfile
21
20
from copy import copy
22
21
23
22
# These tests are not particularly useful if Python was invoked with -S.
@@ -490,25 +489,29 @@ def test_startup_interactivehook_isolated_explicit(self):
490
489
'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))' ]).wait ()
491
490
self .assertTrue (r , "'__interactivehook__' not added by enablerlcompleter()" )
492
491
493
-
494
- @unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
495
- class _pthFileTests (unittest .TestCase ):
496
-
492
+ @classmethod
497
493
def _create_underpth_exe (self , lines ):
498
- self .temp_dir = tempfile .TemporaryDirectory ()
499
- self .addCleanup (self .temp_dir .cleanup )
500
- exe_file = os .path .join (
501
- self .temp_dir .name ,
502
- os .path .split (sys .executable )[1 ],
503
- )
494
+ exe_file = os .path .join (os .getenv ('TEMP' ), os .path .split (sys .executable )[1 ])
504
495
shutil .copy (sys .executable , exe_file )
505
496
506
497
_pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
507
- with open (_pth_file , 'w' ) as f :
508
- for line in lines :
509
- print (line , file = f )
510
- return exe_file
498
+ try :
499
+ with open (_pth_file , 'w' ) as f :
500
+ for line in lines :
501
+ print (line , file = f )
502
+ return exe_file
503
+ except :
504
+ test .support .unlink (_pth_file )
505
+ test .support .unlink (exe_file )
506
+ raise
507
+
508
+ @classmethod
509
+ def _cleanup_underpth_exe (self , exe_file ):
510
+ _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
511
+ test .support .unlink (_pth_file )
512
+ test .support .unlink (exe_file )
511
513
514
+ @classmethod
512
515
def _calc_sys_path_for_underpth_nosite (self , sys_prefix , lines ):
513
516
sys_path = []
514
517
for line in lines :
@@ -518,6 +521,7 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
518
521
sys_path .append (abs_path )
519
522
return sys_path
520
523
524
+ @unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
521
525
def test_underpth_nosite_file (self ):
522
526
libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
523
527
exe_prefix = os .path .dirname (sys .executable )
@@ -532,16 +536,20 @@ def test_underpth_nosite_file(self):
532
536
os .path .dirname (exe_file ),
533
537
pth_lines )
534
538
535
- env = os .environ .copy ()
536
- env ['PYTHONPATH' ] = 'from-env'
537
- env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
538
- rc = subprocess .call ([exe_file , '-c' ,
539
- 'import sys; sys.exit(sys.flags.no_site and '
540
- 'len(sys.path) > 200 and '
541
- 'sys.path == %r)' % sys_path ,
542
- ], env = env )
539
+ try :
540
+ env = os .environ .copy ()
541
+ env ['PYTHONPATH' ] = 'from-env'
542
+ env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
543
+ rc = subprocess .call ([exe_file , '-c' ,
544
+ 'import sys; sys.exit(sys.flags.no_site and '
545
+ 'len(sys.path) > 200 and '
546
+ 'sys.path == %r)' % sys_path ,
547
+ ], env = env )
548
+ finally :
549
+ self ._cleanup_underpth_exe (exe_file )
543
550
self .assertTrue (rc , "sys.path is incorrect" )
544
551
552
+ @unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
545
553
def test_underpth_file (self ):
546
554
libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
547
555
exe_prefix = os .path .dirname (sys .executable )
@@ -553,17 +561,20 @@ def test_underpth_file(self):
553
561
'import site'
554
562
])
555
563
sys_prefix = os .path .dirname (exe_file )
556
- env = os .environ .copy ()
557
- env ['PYTHONPATH' ] = 'from-env'
558
- env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
559
- rc = subprocess .call ([exe_file , '-c' ,
560
- 'import sys; sys.exit(not sys.flags.no_site and '
561
- '%r in sys.path and %r in sys.path and %r not in sys.path and '
562
- 'all("\\ r" not in p and "\\ n" not in p for p in sys.path))' % (
563
- os .path .join (sys_prefix , 'fake-path-name' ),
564
- libpath ,
565
- os .path .join (sys_prefix , 'from-env' ),
566
- )], env = env )
564
+ try :
565
+ env = os .environ .copy ()
566
+ env ['PYTHONPATH' ] = 'from-env'
567
+ env ['PATH' ] = '{};{}' .format (exe_prefix , os .getenv ('PATH' ))
568
+ rc = subprocess .call ([exe_file , '-c' ,
569
+ 'import sys; sys.exit(not sys.flags.no_site and '
570
+ '%r in sys.path and %r in sys.path and %r not in sys.path and '
571
+ 'all("\\ r" not in p and "\\ n" not in p for p in sys.path))' % (
572
+ os .path .join (sys_prefix , 'fake-path-name' ),
573
+ libpath ,
574
+ os .path .join (sys_prefix , 'from-env' ),
575
+ )], env = env )
576
+ finally :
577
+ self ._cleanup_underpth_exe (exe_file )
567
578
self .assertTrue (rc , "sys.path is incorrect" )
568
579
569
580
0 commit comments