Skip to content

Allow for "pdb" module to be rewritten #5920

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 1 commit into from
Oct 6, 2019
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
10 changes: 8 additions & 2 deletions src/_pytest/debugging.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
""" interactive debugging with PDB, the Python Debugger. """
import argparse
import pdb
import sys
from doctest import UnexpectedException

from _pytest import outcomes
from _pytest.config import hookimpl
Expand Down Expand Up @@ -45,6 +43,8 @@ def pytest_addoption(parser):


def pytest_configure(config):
import pdb

if config.getvalue("trace"):
config.pluginmanager.register(PdbTrace(), "pdbtrace")
if config.getvalue("usepdb"):
Expand Down Expand Up @@ -87,6 +87,8 @@ def _is_capturing(cls, capman):
@classmethod
def _import_pdb_cls(cls, capman):
if not cls._config:
import pdb

# Happens when using pytest.set_trace outside of a test.
return pdb.Pdb

Expand All @@ -113,6 +115,8 @@ def _import_pdb_cls(cls, capman):
"--pdbcls: could not import {!r}: {}".format(value, exc)
)
else:
import pdb

pdb_cls = pdb.Pdb

wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman)
Expand Down Expand Up @@ -313,6 +317,8 @@ def _enter_pdb(node, excinfo, rep):


def _postmortem_traceback(excinfo):
from doctest import UnexpectedException

if isinstance(excinfo.value, UnexpectedException):
# A doctest.UnexpectedException is not useful for post_mortem.
# Use the underlying exception instead:
Expand Down
37 changes: 37 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,40 @@ def test_3():
assert (
result.stdout.str().count("async def functions are not natively supported") == 1
)


def test_pdb_can_be_rewritten(testdir):
testdir.makepyfile(
**{
"conftest.py": """
import pytest
pytest.register_assert_rewrite("pdb")
""",
"__init__.py": "",
"pdb.py": """
def check():
assert 1 == 2
""",
"test_pdb.py": """
def test():
import pdb
assert pdb.check()
""",
}
)
# Disable debugging plugin itself to avoid:
# > INTERNALERROR> AttributeError: module 'pdb' has no attribute 'set_trace'
result = testdir.runpytest_subprocess("-p", "no:debugging", "-vv")
result.stdout.fnmatch_lines(
[
" def check():",
"> assert 1 == 2",
"E assert 1 == 2",
"E -1",
"E +2",
"",
"pdb.py:2: AssertionError",
"*= 1 failed in *",
]
)
assert result.ret == 1