From dce74aa583b6a5b6335192f346048a81d7ac1d9b Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Fri, 1 Mar 2019 01:16:58 +0300 Subject: [PATCH 1/3] Add site.py debugging via PYTHODSITEDEBUG --- Doc/using/cmdline.rst | 9 +++++++++ Lib/site.py | 7 +++++++ .../Library/2019-03-01-01-08-38.bpo-33944.8gWRAj.rst | 1 + 3 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-03-01-01-08-38.bpo-33944.8gWRAj.rst diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index bd3cdef5739082..dd8d1dd86e7e44 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -917,6 +917,15 @@ conflict. See :pep:`540` for more details. +.. envvar:: PYTHONSITEDEBUG + + If set to a non-empty string, drop into the :mod:`pdb` prompt + at the start of the :mod:`site` module. + This is useful to diagnose issues in 3rd-party code that runs at startup. + + .. versionadded:: 3.8 + + Debug-mode variables ~~~~~~~~~~~~~~~~~~~~ diff --git a/Lib/site.py b/Lib/site.py index ad1146332b0ab7..2453dee0084912 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -74,6 +74,12 @@ import builtins import _sitebuiltins + +if os.environ.get("PYTHONSITEDEBUG", ""): + import pdb + pdb.set_trace() + + # Prefixes for site-packages; add additional prefixes like /usr/local here PREFIXES = [sys.prefix, sys.exec_prefix] # Enable per user site-packages directory @@ -636,5 +642,6 @@ def _script(): print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) sys.exit(10) + if __name__ == '__main__': _script() diff --git a/Misc/NEWS.d/next/Library/2019-03-01-01-08-38.bpo-33944.8gWRAj.rst b/Misc/NEWS.d/next/Library/2019-03-01-01-08-38.bpo-33944.8gWRAj.rst new file mode 100644 index 00000000000000..f3c7bff9d1b8f0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-03-01-01-08-38.bpo-33944.8gWRAj.rst @@ -0,0 +1 @@ +Added startup code debugging with the PYTHONSITEDEBUG environment variable From cc8a7a44d614b65a80089ee9924e79bb30116a2b Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Fri, 1 Mar 2019 21:16:42 +0300 Subject: [PATCH 2/3] add test --- Lib/test/test_site.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 735651ec7d7550..6f8c26d07f1f02 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -310,6 +310,27 @@ def test_no_home_directory(self): mock_addsitedir.assert_not_called() self.assertFalse(known_paths) + def test_debug(self): + def _run(input_): + p = subprocess.Popen([sys.executable, "-c", "pass"], env=environ, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + out, _ = p.communicate(b"c\n" if input_ else b"") + return out + re_pdb_prompt = rb"(?m)^\(Pdb\)\s*$" + envvar = 'PYTHONSITEDEBUG' + environ = os.environ.copy() + + environ[envvar]="1" + self.assertRegex(_run(True), re_pdb_prompt) + environ[envvar]="" + self.assertNotRegex(_run(False), re_pdb_prompt) + del environ[envvar] + self.assertNotRegex(_run(False), re_pdb_prompt) + + + class PthFile(object): """Helper class for handling testing of .pth files""" From e26e98a468f68a36782280bc1223f59fbfe8c81b Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 2 Mar 2019 19:41:13 +0300 Subject: [PATCH 3/3] Use the newer breakpoint API Co-Authored-By: Inada Naoki --- Lib/site.py | 3 +-- Lib/test/test_site.py | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/site.py b/Lib/site.py index 2453dee0084912..74e2f98921f63a 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -76,8 +76,7 @@ if os.environ.get("PYTHONSITEDEBUG", ""): - import pdb - pdb.set_trace() + breakpoint() # Prefixes for site-packages; add additional prefixes like /usr/local here diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 6f8c26d07f1f02..3c6cc26365319c 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -320,7 +320,10 @@ def _run(input_): return out re_pdb_prompt = rb"(?m)^\(Pdb\)\s*$" envvar = 'PYTHONSITEDEBUG' + environ = os.environ.copy() + try: del environ['PYTHONBREAKPOINT'] + except KeyError: pass environ[envvar]="1" self.assertRegex(_run(True), re_pdb_prompt)