1010from test .support import (captured_stderr , TESTFN , EnvironmentVarGuard ,
1111 change_cwd )
1212import builtins
13+ import encodings
14+ import glob
1315import os
14- import sys
1516import re
16- import encodings
17- import urllib .request
18- import urllib .error
1917import shutil
2018import subprocess
19+ import sys
2120import sysconfig
2221import tempfile
22+ import urllib .error
23+ import urllib .request
2324from unittest import mock
2425from copy import copy
2526
@@ -519,6 +520,23 @@ def test_license_exists_at_url(self):
519520class StartupImportTests (unittest .TestCase ):
520521
521522 def test_startup_imports (self ):
523+ # Get sys.path in isolated mode (python3 -I)
524+ popen = subprocess .Popen ([sys .executable , '-I' , '-c' ,
525+ 'import sys; print(repr(sys.path))' ],
526+ stdout = subprocess .PIPE ,
527+ encoding = 'utf-8' )
528+ stdout = popen .communicate ()[0 ]
529+ self .assertEqual (popen .returncode , 0 , repr (stdout ))
530+ isolated_paths = eval (stdout )
531+
532+ # bpo-27807: Even with -I, the site module executes all .pth files
533+ # found in sys.path (see site.addpackage()). Skip the test if at least
534+ # one .pth file is found.
535+ for path in isolated_paths :
536+ pth_files = glob .glob (os .path .join (path , "*.pth" ))
537+ if pth_files :
538+ self .skipTest (f"found { len (pth_files )} .pth files in: { path } " )
539+
522540 # This tests checks which modules are loaded by Python when it
523541 # initially starts upon startup.
524542 popen = subprocess .Popen ([sys .executable , '-I' , '-v' , '-c' ,
@@ -527,6 +545,7 @@ def test_startup_imports(self):
527545 stderr = subprocess .PIPE ,
528546 encoding = 'utf-8' )
529547 stdout , stderr = popen .communicate ()
548+ self .assertEqual (popen .returncode , 0 , (stdout , stderr ))
530549 modules = eval (stdout )
531550
532551 self .assertIn ('site' , modules )
0 commit comments