1010from test .support import (captured_stderr , TESTFN , EnvironmentVarGuard ,
1111 change_cwd )
1212import builtins
13+ import glob
1314import os
1415import sys
1516import re
@@ -512,6 +513,23 @@ def test_license_exists_at_url(self):
512513class StartupImportTests (unittest .TestCase ):
513514
514515 def test_startup_imports (self ):
516+ # Get sys.path in isolated mode (python3 -I)
517+ popen = subprocess .Popen ([sys .executable , '-I' , '-c' ,
518+ 'import sys; print(repr(sys.path))' ],
519+ stdout = subprocess .PIPE ,
520+ encoding = 'utf-8' )
521+ stdout = popen .communicate ()[0 ]
522+ self .assertEqual (popen .returncode , 0 , repr (stdout ))
523+ isolated_paths = eval (stdout )
524+
525+ # bpo-27807: Even with -I, the site module executes all .pth files
526+ # found in sys.path (see site.addpackage()). Skip the test if at least
527+ # one .pth file is found.
528+ for path in isolated_paths :
529+ pth_files = glob .glob (os .path .join (path , "*.pth" ))
530+ if pth_files :
531+ self .skipTest (f"found { len (pth_files )} .pth files in: { path } " )
532+
515533 # This tests checks which modules are loaded by Python when it
516534 # initially starts upon startup.
517535 popen = subprocess .Popen ([sys .executable , '-I' , '-v' , '-c' ,
@@ -520,6 +538,7 @@ def test_startup_imports(self):
520538 stderr = subprocess .PIPE ,
521539 encoding = 'utf-8' )
522540 stdout , stderr = popen .communicate ()
541+ self .assertEqual (popen .returncode , 0 , (stdout , stderr ))
523542 modules = eval (stdout )
524543
525544 self .assertIn ('site' , modules )
0 commit comments