[Python-checkins] [3.8] bpo-27807: Skip test_site.test_startup_imports() if pth file (GH-19060) (GH-19090)

Victor Stinner webhook-mailer at python.org
Fri Mar 20 10:10:21 EDT 2020


https://github.com/python/cpython/commit/ba26bf30940f4347fedcf8ebc374c6e2dc375afa
commit: ba26bf30940f4347fedcf8ebc374c6e2dc375afa
branch: 3.8
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-03-20T15:10:14+01:00
summary:

[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.

files:
A Misc/NEWS.d/next/Tests/2020-03-18-16-04-33.bpo-27807.9gKjET.rst
M Lib/test/test_site.py

diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 41c4229919507..1bbc6979368db 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -10,6 +10,7 @@
 from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
                           change_cwd)
 import builtins
+import glob
 import os
 import sys
 import re
@@ -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',
@@ -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)
diff --git a/Misc/NEWS.d/next/Tests/2020-03-18-16-04-33.bpo-27807.9gKjET.rst b/Misc/NEWS.d/next/Tests/2020-03-18-16-04-33.bpo-27807.9gKjET.rst
new file mode 100644
index 0000000000000..3749a747c84b3
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2020-03-18-16-04-33.bpo-27807.9gKjET.rst
@@ -0,0 +1,2 @@
+``test_site.test_startup_imports()`` is now skipped if a path of
+:data:`sys.path` contains a ``.pth`` file.



More information about the Python-checkins mailing list