Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
change_cwd)
import builtins
import encodings
import glob
import os
import sys
import re
import encodings
import urllib.request
import urllib.error
import shutil
import subprocess
import sys
import sysconfig
import tempfile
import urllib.error
import urllib.request
from unittest import mock
from copy import copy

Expand Down Expand Up @@ -512,6 +513,23 @@ def test_license_exists_at_url(self):
class StartupImportTests(unittest.TestCase):

def test_startup_imports(self):
# Get sys.path in isolated mode (python3 -I)
popen = subprocess.Popen([sys.executable, '-I', '-c',
'import sys; print(repr(sys.path))'],
stdout=subprocess.PIPE,
encoding='utf-8')
stdout = popen.communicate()[0]
self.assertEqual(popen.returncode, 0, repr(stdout))
isolated_paths = eval(stdout)

# bpo-27807: Even with -I, the site module executes all .pth files
# found in sys.path (see site.addpackage()). Skip the test if at least
# one .pth file is found.
for path in isolated_paths:
pth_files = glob.glob(os.path.join(path, "*.pth"))
if pth_files:
self.skipTest(f"found {len(pth_files)} .pth files in: {path}")

# This tests checks which modules are loaded by Python when it
# initially starts upon startup.
popen = subprocess.Popen([sys.executable, '-I', '-v', '-c',
Expand All @@ -520,6 +538,7 @@ def test_startup_imports(self):
stderr=subprocess.PIPE,
encoding='utf-8')
stdout, stderr = popen.communicate()
self.assertEqual(popen.returncode, 0, (stdout, stderr))
modules = eval(stdout)

self.assertIn('site', modules)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``test_site.test_startup_imports()`` is now skipped if a path of
:data:`sys.path` contains a ``.pth`` file.