@@ -550,6 +550,24 @@ def temporary_pycache_prefix(self):
550550 finally :
551551 sys .pycache_prefix = old_prefix
552552
553+ @contextlib .contextmanager
554+ def no_pycache_prefix (self ):
555+ """Ignore any ambient pycache prefix for the duration of the test.
556+
557+ Some tests assume bytecode is written next to the source in a
558+ __pycache__ directory. When the test suite is run with
559+ PYTHONPYCACHEPREFIX set, neutralize it both in this process (used by
560+ cache_from_source) and in any spawned subprocesses.
561+ """
562+ old_prefix = sys .pycache_prefix
563+ sys .pycache_prefix = None
564+ try :
565+ with os_helper .EnvironmentVarGuard () as env :
566+ env .unset ('PYTHONPYCACHEPREFIX' )
567+ yield
568+ finally :
569+ sys .pycache_prefix = old_prefix
570+
553571 def _get_run_args (self , args ):
554572 return [* support .optim_args_from_interpreter_flags (),
555573 '-S' , '-m' , 'compileall' ,
@@ -646,15 +664,16 @@ def test_legacy_paths(self):
646664 def test_multiple_runs (self ):
647665 # Bug 8527 reported that multiple calls produced empty
648666 # __pycache__/__pycache__ directories.
649- self .assertRunOK ('-q' , self .pkgdir )
650- # Verify the __pycache__ directory contents.
651- self .assertTrue (os .path .exists (self .pkgdir_cachedir ))
652- cachecachedir = os .path .join (self .pkgdir_cachedir , '__pycache__' )
653- self .assertFalse (os .path .exists (cachecachedir ))
654- # Call compileall again.
655- self .assertRunOK ('-q' , self .pkgdir )
656- self .assertTrue (os .path .exists (self .pkgdir_cachedir ))
657- self .assertFalse (os .path .exists (cachecachedir ))
667+ with self .no_pycache_prefix ():
668+ self .assertRunOK ('-q' , self .pkgdir )
669+ # Verify the __pycache__ directory contents.
670+ self .assertTrue (os .path .exists (self .pkgdir_cachedir ))
671+ cachecachedir = os .path .join (self .pkgdir_cachedir , '__pycache__' )
672+ self .assertFalse (os .path .exists (cachecachedir ))
673+ # Call compileall again.
674+ self .assertRunOK ('-q' , self .pkgdir )
675+ self .assertTrue (os .path .exists (self .pkgdir_cachedir ))
676+ self .assertFalse (os .path .exists (cachecachedir ))
658677
659678 @without_source_date_epoch # timestamp invalidation test
660679 def test_force (self ):
@@ -727,10 +746,13 @@ def test_symlink_loop(self):
727746 script_helper .make_pkg (pkg )
728747 os .symlink ('.' , os .path .join (pkg , 'evil' ))
729748 os .symlink ('.' , os .path .join (pkg , 'evil2' ))
730- self .assertRunOK ('-q' , self .pkgdir )
731- self .assertCompiled (os .path .join (
732- self .pkgdir , 'spam' , 'evil' , 'evil2' , '__init__.py'
733- ))
749+ # This relies on the __pycache__ layout (shared across the symlinked
750+ # paths), so neutralize any ambient PYTHONPYCACHEPREFIX.
751+ with self .no_pycache_prefix ():
752+ self .assertRunOK ('-q' , self .pkgdir )
753+ self .assertCompiled (os .path .join (
754+ self .pkgdir , 'spam' , 'evil' , 'evil2' , '__init__.py'
755+ ))
734756
735757 def test_quiet (self ):
736758 noisy = self .assertRunOK (self .pkgdir )
@@ -817,13 +839,16 @@ def test_include_on_stdin(self):
817839 f2 = script_helper .make_script (self .pkgdir , 'f2' , '' )
818840 f3 = script_helper .make_script (self .pkgdir , 'f3' , '' )
819841 f4 = script_helper .make_script (self .pkgdir , 'f4' , '' )
820- p = script_helper .spawn_python (* (self ._get_run_args (()) + ['-i' , '-' ]))
821- p .stdin .write ((f3 + os .linesep ).encode ('ascii' ))
822- script_helper .kill_python (p )
823- self .assertNotCompiled (f1 )
824- self .assertNotCompiled (f2 )
825- self .assertCompiled (f3 )
826- self .assertNotCompiled (f4 )
842+ # spawn_python() runs with -E, ignoring PYTHONPYCACHEPREFIX, so make
843+ # cache_from_source() in this process agree by neutralizing it too.
844+ with self .no_pycache_prefix ():
845+ p = script_helper .spawn_python (* (self ._get_run_args (()) + ['-i' , '-' ]))
846+ p .stdin .write ((f3 + os .linesep ).encode ('ascii' ))
847+ script_helper .kill_python (p )
848+ self .assertNotCompiled (f1 )
849+ self .assertNotCompiled (f2 )
850+ self .assertCompiled (f3 )
851+ self .assertNotCompiled (f4 )
827852
828853 def test_compiles_as_much_as_possible (self ):
829854 bingfn = script_helper .make_script (self .pkgdir , 'bing' , 'syntax(error' )
0 commit comments