]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33944: site: Add site-packages tracing in verbose mode (GH-12110)
authornative-api <vano@mail.mipt.ru>
Fri, 12 Jun 2020 06:20:11 +0000 (09:20 +0300)
committerGitHub <noreply@github.com>
Fri, 12 Jun 2020 06:20:11 +0000 (15:20 +0900)
Doc/using/cmdline.rst
Lib/site.py
Lib/test/test_site.py
Misc/NEWS.d/next/Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst [new file with mode: 0644]

index 7aacd8ffe822ed4b30e3c6683ba24e10f8c07cd8..8c65d99ef31f9fec9e16f03bd95b29e5db83fb15 100644 (file)
@@ -369,6 +369,11 @@ Miscellaneous options
    (filename or built-in module) from which it is loaded.  When given twice
    (:option:`!-vv`), print a message for each file that is checked for when
    searching for a module.  Also provides information on module cleanup at exit.
+
+   .. versionchanged:: 3.10
+      The :mod:`site` module reports the site-specific paths
+      and :file:`.pth` files being processed.
+
    See also :envvar:`PYTHONVERBOSE`.
 
 
index e981a142088fdf8be570effdd7512b79459bad16..544306cd40e3282ca5152b246a16da133576caee 100644 (file)
@@ -88,6 +88,11 @@ USER_SITE = None
 USER_BASE = None
 
 
+def _trace(message):
+    if sys.flags.verbose:
+        print(message, file=sys.stderr)
+
+
 def makepath(*paths):
     dir = os.path.join(*paths)
     try:
@@ -156,6 +161,7 @@ def addpackage(sitedir, name, known_paths):
     else:
         reset = False
     fullname = os.path.join(sitedir, name)
+    _trace(f"Processing .pth file: {fullname!r}")
     try:
         f = io.TextIOWrapper(io.open_code(fullname))
     except OSError:
@@ -190,6 +196,7 @@ def addpackage(sitedir, name, known_paths):
 def addsitedir(sitedir, known_paths=None):
     """Add 'sitedir' argument to sys.path if missing and handle .pth files in
     'sitedir'"""
+    _trace(f"Adding directory: {sitedir!r}")
     if known_paths is None:
         known_paths = _init_pathinfo()
         reset = True
@@ -310,6 +317,7 @@ def addusersitepackages(known_paths):
     """
     # get the per user site-package path
     # this call will also make sure USER_BASE and USER_SITE are set
+    _trace("Processing user site-packages")
     user_site = getusersitepackages()
 
     if ENABLE_USER_SITE and os.path.isdir(user_site):
@@ -354,6 +362,7 @@ def getsitepackages(prefixes=None):
 
 def addsitepackages(known_paths, prefixes=None):
     """Add site-packages to sys.path"""
+    _trace("Processing global site-packages")
     for sitedir in getsitepackages(prefixes):
         if os.path.isdir(sitedir):
             addsitedir(sitedir, known_paths)
index 957e7a41d5466b033c16a799307c18b29611d941..9f4a8bc64f7eee14cb16030b806f2489c328ae13 100644 (file)
@@ -13,6 +13,7 @@ from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
 import builtins
 import encodings
 import glob
+import io
 import os
 import re
 import shutil
@@ -320,6 +321,14 @@ class HelperFunctionsTests(unittest.TestCase):
             mock_addsitedir.assert_not_called()
             self.assertFalse(known_paths)
 
+    def test_trace(self):
+        message = "bla-bla-bla"
+        for verbose, out in (True, message + "\n"), (False, ""):
+            with mock.patch('sys.flags', mock.Mock(verbose=verbose)), \
+                    mock.patch('sys.stderr', io.StringIO()):
+                site._trace(message)
+                self.assertEqual(sys.stderr.getvalue(), out)
+
 
 class PthFile(object):
     """Helper class for handling testing of .pth files"""
diff --git a/Misc/NEWS.d/next/Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst b/Misc/NEWS.d/next/Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst
new file mode 100644 (file)
index 0000000..b0c953d
--- /dev/null
@@ -0,0 +1 @@
+Added site.py site-packages tracing in verbose mode.