Skip to content

Commit 2145c8c

Browse files
authored
bpo-33944: site: Add site-packages tracing in verbose mode (GH-12110)
1 parent ddef3bd commit 2145c8c

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

Doc/using/cmdline.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ Miscellaneous options
369369
(filename or built-in module) from which it is loaded. When given twice
370370
(:option:`!-vv`), print a message for each file that is checked for when
371371
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+
372377
See also :envvar:`PYTHONVERBOSE`.
373378

374379

Lib/site.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
USER_BASE = None
8989

9090

91+
def _trace(message):
92+
if sys.flags.verbose:
93+
print(message, file=sys.stderr)
94+
95+
9196
def makepath(*paths):
9297
dir = os.path.join(*paths)
9398
try:
@@ -156,6 +161,7 @@ def addpackage(sitedir, name, known_paths):
156161
else:
157162
reset = False
158163
fullname = os.path.join(sitedir, name)
164+
_trace(f"Processing .pth file: {fullname!r}")
159165
try:
160166
f = io.TextIOWrapper(io.open_code(fullname))
161167
except OSError:
@@ -190,6 +196,7 @@ def addpackage(sitedir, name, known_paths):
190196
def addsitedir(sitedir, known_paths=None):
191197
"""Add 'sitedir' argument to sys.path if missing and handle .pth files in
192198
'sitedir'"""
199+
_trace(f"Adding directory: {sitedir!r}")
193200
if known_paths is None:
194201
known_paths = _init_pathinfo()
195202
reset = True
@@ -310,6 +317,7 @@ def addusersitepackages(known_paths):
310317
"""
311318
# get the per user site-package path
312319
# this call will also make sure USER_BASE and USER_SITE are set
320+
_trace("Processing user site-packages")
313321
user_site = getusersitepackages()
314322

315323
if ENABLE_USER_SITE and os.path.isdir(user_site):
@@ -354,6 +362,7 @@ def getsitepackages(prefixes=None):
354362

355363
def addsitepackages(known_paths, prefixes=None):
356364
"""Add site-packages to sys.path"""
365+
_trace("Processing global site-packages")
357366
for sitedir in getsitepackages(prefixes):
358367
if os.path.isdir(sitedir):
359368
addsitedir(sitedir, known_paths)

Lib/test/test_site.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import builtins
1414
import encodings
1515
import glob
16+
import io
1617
import os
1718
import re
1819
import shutil
@@ -320,6 +321,14 @@ def test_no_home_directory(self):
320321
mock_addsitedir.assert_not_called()
321322
self.assertFalse(known_paths)
322323

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+
323332

324333
class PthFile(object):
325334
"""Helper class for handling testing of .pth files"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added site.py site-packages tracing in verbose mode.

0 commit comments

Comments
 (0)