1717import shutil
1818import subprocess
1919import sysconfig
20+ import tempfile
2021from copy import copy
2122
2223# These tests are not particularly useful if Python was invoked with -S.
@@ -489,29 +490,25 @@ def test_startup_interactivehook_isolated_explicit(self):
489490 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))' ]).wait ()
490491 self .assertTrue (r , "'__interactivehook__' not added by enablerlcompleter()" )
491492
492- @classmethod
493+
494+ @unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
495+ class _pthFileTests (unittest .TestCase ):
496+
493497 def _create_underpth_exe (self , lines ):
494- exe_file = os .path .join (os .getenv ('TEMP' ), os .path .split (sys .executable )[1 ])
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+ )
495504 shutil .copy (sys .executable , exe_file )
496505
497506 _pth_file = os .path .splitext (exe_file )[0 ] + '._pth'
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 )
507+ with open (_pth_file , 'w' ) as f :
508+ for line in lines :
509+ print (line , file = f )
510+ return exe_file
513511
514- @classmethod
515512 def _calc_sys_path_for_underpth_nosite (self , sys_prefix , lines ):
516513 sys_path = []
517514 for line in lines :
@@ -521,7 +518,6 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
521518 sys_path .append (abs_path )
522519 return sys_path
523520
524- @unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
525521 def test_underpth_nosite_file (self ):
526522 libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
527523 exe_prefix = os .path .dirname (sys .executable )
@@ -536,20 +532,16 @@ def test_underpth_nosite_file(self):
536532 os .path .dirname (exe_file ),
537533 pth_lines )
538534
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 )
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 )
550543 self .assertTrue (rc , "sys.path is incorrect" )
551544
552- @unittest .skipUnless (sys .platform == 'win32' , "only supported on Windows" )
553545 def test_underpth_file (self ):
554546 libpath = os .path .dirname (os .path .dirname (encodings .__file__ ))
555547 exe_prefix = os .path .dirname (sys .executable )
@@ -561,20 +553,17 @@ def test_underpth_file(self):
561553 'import site'
562554 ])
563555 sys_prefix = os .path .dirname (exe_file )
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 )
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 )
578567 self .assertTrue (rc , "sys.path is incorrect" )
579568
580569
0 commit comments