Skip to content

bpo-33944: Add site.py debugging via PYTHONSITEDEBUG #12107

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

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 9 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. versionadded:: 3.8
.. versionadded:: 3.10



Debug-mode variables
~~~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 6 additions & 0 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
import builtins
import _sitebuiltins


if os.environ.get("PYTHONSITEDEBUG", ""):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any PYTHON env var must be ignored when ignore environment is set.

Suggested change
if os.environ.get("PYTHONSITEDEBUG", ""):
if not sys.flags.ignore_environment and os.environ.get("PYTHONSITEDEBUG"):

breakpoint()


# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
Expand Down Expand Up @@ -636,5 +641,6 @@ def _script():
print(textwrap.dedent(help % (sys.argv[0], os.pathsep)))
sys.exit(10)


if __name__ == '__main__':
_script()
24 changes: 24 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ 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()
try: del environ['PYTHONBREAKPOINT']
except KeyError: pass

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"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added startup code debugging with the PYTHONSITEDEBUG environment variable