Skip to content

bpo-33944: Add site.py site-packages tracing in verbose mode #12110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 12, 2020
Merged
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
5 changes: 5 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.


Expand Down
9 changes: 9 additions & 0 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
USER_BASE = None


def _trace(message):
if sys.flags.verbose:
print(message, file=sys.stderr)


def makepath(*paths):
dir = os.path.join(*paths)
try:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import builtins
import encodings
import glob
import io
import os
import re
import shutil
Expand Down Expand Up @@ -320,6 +321,14 @@ def test_no_home_directory(self):
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"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added site.py site-packages tracing in verbose mode.