Allow for "pdb" module to be rewritten

This commit is contained in:
Daniel Hahler 2019-10-06 15:34:23 +02:00
parent b847d5712b
commit 07f20ccab6
2 changed files with 45 additions and 2 deletions

View File

@ -1,8 +1,6 @@
""" interactive debugging with PDB, the Python Debugger. """ """ interactive debugging with PDB, the Python Debugger. """
import argparse import argparse
import pdb
import sys import sys
from doctest import UnexpectedException
from _pytest import outcomes from _pytest import outcomes
from _pytest.config import hookimpl from _pytest.config import hookimpl
@ -45,6 +43,8 @@ def pytest_addoption(parser):
def pytest_configure(config): def pytest_configure(config):
import pdb
if config.getvalue("trace"): if config.getvalue("trace"):
config.pluginmanager.register(PdbTrace(), "pdbtrace") config.pluginmanager.register(PdbTrace(), "pdbtrace")
if config.getvalue("usepdb"): if config.getvalue("usepdb"):
@ -87,6 +87,8 @@ class pytestPDB:
@classmethod @classmethod
def _import_pdb_cls(cls, capman): def _import_pdb_cls(cls, capman):
if not cls._config: if not cls._config:
import pdb
# Happens when using pytest.set_trace outside of a test. # Happens when using pytest.set_trace outside of a test.
return pdb.Pdb return pdb.Pdb
@ -113,6 +115,8 @@ class pytestPDB:
"--pdbcls: could not import {!r}: {}".format(value, exc) "--pdbcls: could not import {!r}: {}".format(value, exc)
) )
else: else:
import pdb
pdb_cls = pdb.Pdb pdb_cls = pdb.Pdb
wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman) wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman)
@ -313,6 +317,8 @@ def _enter_pdb(node, excinfo, rep):
def _postmortem_traceback(excinfo): def _postmortem_traceback(excinfo):
from doctest import UnexpectedException
if isinstance(excinfo.value, UnexpectedException): if isinstance(excinfo.value, UnexpectedException):
# A doctest.UnexpectedException is not useful for post_mortem. # A doctest.UnexpectedException is not useful for post_mortem.
# Use the underlying exception instead: # Use the underlying exception instead:

View File

@ -1238,3 +1238,40 @@ def test_warn_on_async_gen_function(testdir):
assert ( assert (
result.stdout.str().count("async def functions are not natively supported") == 1 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