bpo-33944: site: Add site-packages tracing in verbose mode (GH-12110) · python/cpython@2145c8c (original) (raw)
4 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -369,6 +369,11 @@ Miscellaneous options | ||
369 | 369 | (filename or built-in module) from which it is loaded. When given twice |
370 | 370 | (:option:`!-vv`), print a message for each file that is checked for when |
371 | 371 | searching for a module. Also provides information on module cleanup at exit. |
372 | + | |
373 | + .. versionchanged:: 3.10 | |
374 | + The :mod:`site` module reports the site-specific paths | |
375 | + and :file:`.pth` files being processed. | |
376 | + | |
372 | 377 | See also :envvar:`PYTHONVERBOSE`. |
373 | 378 | |
374 | 379 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -88,6 +88,11 @@ | ||
88 | 88 | USER_BASE = None |
89 | 89 | |
90 | 90 | |
91 | +def _trace(message): | |
92 | +if sys.flags.verbose: | |
93 | +print(message, file=sys.stderr) | |
94 | + | |
95 | + | |
91 | 96 | def makepath(*paths): |
92 | 97 | dir = os.path.join(*paths) |
93 | 98 | try: |
@@ -156,6 +161,7 @@ def addpackage(sitedir, name, known_paths): | ||
156 | 161 | else: |
157 | 162 | reset = False |
158 | 163 | fullname = os.path.join(sitedir, name) |
164 | +_trace(f"Processing .pth file: {fullname!r}") | |
159 | 165 | try: |
160 | 166 | f = io.TextIOWrapper(io.open_code(fullname)) |
161 | 167 | except OSError: |
@@ -190,6 +196,7 @@ def addpackage(sitedir, name, known_paths): | ||
190 | 196 | def addsitedir(sitedir, known_paths=None): |
191 | 197 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
192 | 198 | 'sitedir'""" |
199 | +_trace(f"Adding directory: {sitedir!r}") | |
193 | 200 | if known_paths is None: |
194 | 201 | known_paths = _init_pathinfo() |
195 | 202 | reset = True |
@@ -310,6 +317,7 @@ def addusersitepackages(known_paths): | ||
310 | 317 | """ |
311 | 318 | # get the per user site-package path |
312 | 319 | # this call will also make sure USER_BASE and USER_SITE are set |
320 | +_trace("Processing user site-packages") | |
313 | 321 | user_site = getusersitepackages() |
314 | 322 | |
315 | 323 | if ENABLE_USER_SITE and os.path.isdir(user_site): |
@@ -354,6 +362,7 @@ def getsitepackages(prefixes=None): | ||
354 | 362 | |
355 | 363 | def addsitepackages(known_paths, prefixes=None): |
356 | 364 | """Add site-packages to sys.path""" |
365 | +_trace("Processing global site-packages") | |
357 | 366 | for sitedir in getsitepackages(prefixes): |
358 | 367 | if os.path.isdir(sitedir): |
359 | 368 | addsitedir(sitedir, known_paths) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -13,6 +13,7 @@ | ||
13 | 13 | import builtins |
14 | 14 | import encodings |
15 | 15 | import glob |
16 | +import io | |
16 | 17 | import os |
17 | 18 | import re |
18 | 19 | import shutil |
@@ -320,6 +321,14 @@ def test_no_home_directory(self): | ||
320 | 321 | mock_addsitedir.assert_not_called() |
321 | 322 | self.assertFalse(known_paths) |
322 | 323 | |
324 | +def test_trace(self): | |
325 | +message = "bla-bla-bla" | |
326 | +for verbose, out in (True, message + "\n"), (False, ""): | |
327 | +with mock.patch('sys.flags', mock.Mock(verbose=verbose)), \ | |
328 | +mock.patch('sys.stderr', io.StringIO()): | |
329 | +site._trace(message) | |
330 | +self.assertEqual(sys.stderr.getvalue(), out) | |
331 | + | |
323 | 332 | |
324 | 333 | class PthFile(object): |
325 | 334 | """Helper class for handling testing of .pth files""" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 | +Added site.py site-packages tracing in verbose mode. |