Skip to content

Commit 6056b7b

Browse files
[3.8] bpo-27807: Skip test_site.test_startup_imports() if pth file (GH-19060) (GH-19090)
test_site.test_startup_imports() is now skipped if a path of sys.path contains a .pth file. Sort test_site imports. (cherry picked from commit ba26bf3) Co-authored-by: Victor Stinner <[email protected]>
1 parent 64937d3 commit 6056b7b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Lib/test/test_site.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
1111
change_cwd)
1212
import builtins
13+
import glob
1314
import os
1415
import sys
1516
import re
@@ -504,6 +505,23 @@ def test_license_exists_at_url(self):
504505
class StartupImportTests(unittest.TestCase):
505506

506507
def test_startup_imports(self):
508+
# Get sys.path in isolated mode (python3 -I)
509+
popen = subprocess.Popen([sys.executable, '-I', '-c',
510+
'import sys; print(repr(sys.path))'],
511+
stdout=subprocess.PIPE,
512+
encoding='utf-8')
513+
stdout = popen.communicate()[0]
514+
self.assertEqual(popen.returncode, 0, repr(stdout))
515+
isolated_paths = eval(stdout)
516+
517+
# bpo-27807: Even with -I, the site module executes all .pth files
518+
# found in sys.path (see site.addpackage()). Skip the test if at least
519+
# one .pth file is found.
520+
for path in isolated_paths:
521+
pth_files = glob.glob(os.path.join(path, "*.pth"))
522+
if pth_files:
523+
self.skipTest(f"found {len(pth_files)} .pth files in: {path}")
524+
507525
# This tests checks which modules are loaded by Python when it
508526
# initially starts upon startup.
509527
popen = subprocess.Popen([sys.executable, '-I', '-v', '-c',
@@ -512,6 +530,7 @@ def test_startup_imports(self):
512530
stderr=subprocess.PIPE,
513531
encoding='utf-8')
514532
stdout, stderr = popen.communicate()
533+
self.assertEqual(popen.returncode, 0, (stdout, stderr))
515534
modules = eval(stdout)
516535

517536
self.assertIn('site', modules)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``test_site.test_startup_imports()`` is now skipped if a path of
2+
:data:`sys.path` contains a ``.pth`` file.

0 commit comments

Comments
 (0)