From 8be6c9775f82529a12ba312bb20ba519d6fcbdb8 Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Fri, 1 Mar 2019 03:31:28 +0300 Subject: [PATCH 1/8] Add site.py site-packages tracing in verbose mode --- Lib/site.py | 8 ++++++++ Lib/test/test_site.py | 9 +++++++++ .../Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst | 1 + 3 files changed, 18 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst diff --git a/Lib/site.py b/Lib/site.py index ad1146332b0ab7..c0392dd87d9b1e 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -87,6 +87,10 @@ USER_BASE = None +def _trace(message): + if sys.flags.verbose: + print(message,file=sys.stderr) + def makepath(*paths): dir = os.path.join(*paths) try: @@ -155,6 +159,7 @@ def addpackage(sitedir, name, known_paths): else: reset = False fullname = os.path.join(sitedir, name) + _trace("Processing .pth file: %r" % fullname) try: f = open(fullname, "r") except OSError: @@ -189,6 +194,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("Adding directory: %r" % sitedir) if known_paths is None: known_paths = _init_pathinfo() reset = True @@ -309,6 +315,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): @@ -344,6 +351,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) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 735651ec7d7550..d76f8497d29b8e 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -310,6 +310,15 @@ def test_no_home_directory(self): mock_addsitedir.assert_not_called() self.assertFalse(known_paths) + def test_trace(self): + import io + 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 index 00000000000000..8dccac6aa43712 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst @@ -0,0 +1 @@ +Added site.py site-packages tracing in verbose mode \ No newline at end of file From 5fecd8371bcc223d5368cd46e43c9ca2e54b2553 Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Fri, 1 Mar 2019 08:35:47 +0300 Subject: [PATCH 2/8] add docs --- Doc/using/cmdline.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index bd3cdef5739082..2ad38af450842a 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -351,6 +351,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. + + .. versionadded:: 3.8 + When running the :mod:`site` module, report the site-specific paths + and :file:`.pth` files being processed. + See also :envvar:`PYTHONVERBOSE`. From 793a6a5ea7561d243919015eb7861fd1c18467a7 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 2 Mar 2019 20:28:11 +0300 Subject: [PATCH 3/8] code style fix Co-Authored-By: native-api --- Lib/site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index c0392dd87d9b1e..5b0a646cf9ab18 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -89,7 +89,7 @@ def _trace(message): if sys.flags.verbose: - print(message,file=sys.stderr) + print(message, file=sys.stderr) def makepath(*paths): dir = os.path.join(*paths) From 2b77747a123ae74cbe8b01ad5c80221d17f3c496 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 2 Mar 2019 20:28:58 +0300 Subject: [PATCH 4/8] use newer string formatting syntax Co-Authored-By: native-api --- Lib/site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index 5b0a646cf9ab18..92a1b0b4a42a63 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -159,7 +159,7 @@ def addpackage(sitedir, name, known_paths): else: reset = False fullname = os.path.join(sitedir, name) - _trace("Processing .pth file: %r" % fullname) + _trace(f"Processing .pth file: {fullname!r}") try: f = open(fullname, "r") except OSError: From b8de14d0fb69269019bb4a91837819c0deb87e3e Mon Sep 17 00:00:00 2001 From: native-api Date: Sun, 3 Mar 2019 06:49:49 +0300 Subject: [PATCH 5/8] use newer string formatting syntax --- Lib/site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index 92a1b0b4a42a63..140ec2113bda18 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -194,7 +194,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("Adding directory: %r" % sitedir) + _trace(f"Adding directory: {sitedir!r}") if known_paths is None: known_paths = _init_pathinfo() reset = True From f01f6a61c895aa4b75d912a38f3e81bbd3b7fa7b Mon Sep 17 00:00:00 2001 From: native-api Date: Sun, 7 Jun 2020 19:20:23 +0300 Subject: [PATCH 6/8] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Lapeyre --- Doc/using/cmdline.rst | 2 +- Lib/test/test_site.py | 4 ++-- .../next/Library/2019-03-01-01-56-23.bpo-33944.-82Pkt.rst | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 2ad38af450842a..bf2b510faf11d1 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -352,7 +352,7 @@ Miscellaneous options (: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. - .. versionadded:: 3.8 + .. versionchanged:: 3.10 When running the :mod:`site` module, report the site-specific paths and :file:`.pth` files being processed. diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index d76f8497d29b8e..83bcdd72e90467 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -312,9 +312,9 @@ def test_no_home_directory(self): def test_trace(self): import io - message="bla-bla-bla" + message = "bla-bla-bla" for verbose, out in (True, message+"\n"), (False, ""): - with mock.patch('sys.flags', mock.Mock(verbose=verbose)),\ + with mock.patch('sys.flags', mock.Mock(verbose=verbose)), \ mock.patch('sys.stderr', io.StringIO()): site._trace(message) self.assertEqual(sys.stderr.getvalue(), out) 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 index 8dccac6aa43712..b0c953dd6752e9 100644 --- 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 @@ -1 +1 @@ -Added site.py site-packages tracing in verbose mode \ No newline at end of file +Added site.py site-packages tracing in verbose mode. From 8d1901ce08c9d9687446748fde9796ea2b3c0c74 Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Mon, 8 Jun 2020 00:24:52 +0300 Subject: [PATCH 7/8] fix PEP8 violations --- Lib/site.py | 1 + Lib/test/test_site.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/site.py b/Lib/site.py index 71c454e9d3ba48..544306cd40e328 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -92,6 +92,7 @@ def _trace(message): if sys.flags.verbose: print(message, file=sys.stderr) + def makepath(*paths): dir = os.path.join(*paths) try: diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 35778d03a600b9..9f4a8bc64f7eee 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -13,6 +13,7 @@ import builtins import encodings import glob +import io import os import re import shutil @@ -321,9 +322,8 @@ def test_no_home_directory(self): self.assertFalse(known_paths) def test_trace(self): - import io message = "bla-bla-bla" - for verbose, out in (True, message+"\n"), (False, ""): + 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) From bf76e64cd559c13d0b1f05328e0732497588dbfa Mon Sep 17 00:00:00 2001 From: native-api Date: Wed, 10 Jun 2020 11:16:16 +0300 Subject: [PATCH 8/8] Update Doc/using/cmdline.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Lapeyre --- Doc/using/cmdline.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 365a04735a126c..10ae561d971b91 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -371,7 +371,7 @@ Miscellaneous options searching for a module. Also provides information on module cleanup at exit. .. versionchanged:: 3.10 - When running the :mod:`site` module, report the site-specific paths + The :mod:`site` module reports the site-specific paths and :file:`.pth` files being processed. See also :envvar:`PYTHONVERBOSE`.