Skip to content

Commit ba26bf3

Browse files
authored
[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.
1 parent 2972336 commit ba26bf3

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
@@ -512,6 +513,23 @@ def test_license_exists_at_url(self):
512513
class 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)
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)