diff options
author | Daniel Hahler <git@thequod.de> | 2019-05-24 18:10:05 +0200 |
---|---|---|
committer | Daniel Hahler <git@thequod.de> | 2019-05-26 15:56:38 +0200 |
commit | f2ed796c41516f35b54e668eb469787df132d7b3 (patch) | |
tree | 2177ec11e8c7dcc2bfdbe9b9a69c5d7ef1836b5f /src/_pytest/debugging.py | |
parent | e393a7389071854f2ff8b9f5e116a7f51cf0b93d (diff) | |
download | pytest-f2ed796c41516f35b54e668eb469787df132d7b3.tar.gz |
pdb: import pdbcls lazily
Fixes https://github.com/pytest-dev/pytest/issues/2064.
Diffstat (limited to 'src/_pytest/debugging.py')
-rw-r--r-- | src/_pytest/debugging.py | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/src/_pytest/debugging.py b/src/_pytest/debugging.py index 3605490e6..8912477db 100644 --- a/src/_pytest/debugging.py +++ b/src/_pytest/debugging.py @@ -49,42 +49,18 @@ def pytest_addoption(parser): ) -def _import_pdbcls(modname, classname): - try: - __import__(modname) - mod = sys.modules[modname] - - # Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp). - parts = classname.split(".") - pdb_cls = getattr(mod, parts[0]) - for part in parts[1:]: - pdb_cls = getattr(pdb_cls, part) - - return pdb_cls - except Exception as exc: - value = ":".join((modname, classname)) - raise UsageError("--pdbcls: could not import {!r}: {}".format(value, exc)) - - def pytest_configure(config): - pdb_cls = config.getvalue("usepdb_cls") - if pdb_cls: - pdb_cls = _import_pdbcls(*pdb_cls) - else: - pdb_cls = pdb.Pdb - if config.getvalue("trace"): config.pluginmanager.register(PdbTrace(), "pdbtrace") if config.getvalue("usepdb"): config.pluginmanager.register(PdbInvoke(), "pdbinvoke") pytestPDB._saved.append( - (pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config, pytestPDB._pdb_cls) + (pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config) ) pdb.set_trace = pytestPDB.set_trace pytestPDB._pluginmanager = config.pluginmanager pytestPDB._config = config - pytestPDB._pdb_cls = pdb_cls # NOTE: not using pytest_unconfigure, since it might get called although # pytest_configure was not (if another plugin raises UsageError). @@ -93,7 +69,6 @@ def pytest_configure(config): pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config, - pytestPDB._pdb_cls, ) = pytestPDB._saved.pop() config._cleanup.append(fin) @@ -104,7 +79,6 @@ class pytestPDB(object): _pluginmanager = None _config = None - _pdb_cls = pdb.Pdb _saved = [] _recursive_debug = 0 @@ -115,6 +89,33 @@ class pytestPDB(object): return False @classmethod + def _import_pdb_cls(cls): + if not cls._config: + # Happens when using pytest.set_trace outside of a test. + return pdb.Pdb + + pdb_cls = cls._config.getvalue("usepdb_cls") + if not pdb_cls: + return pdb.Pdb + + modname, classname = pdb_cls + + try: + __import__(modname) + mod = sys.modules[modname] + + # Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp). + parts = classname.split(".") + pdb_cls = getattr(mod, parts[0]) + for part in parts[1:]: + pdb_cls = getattr(pdb_cls, part) + + return pdb_cls + except Exception as exc: + value = ":".join((modname, classname)) + raise UsageError("--pdbcls: could not import {!r}: {}".format(value, exc)) + + @classmethod def _init_pdb(cls, *args, **kwargs): """ Initialize PDB debugging, dropping any IO capturing. """ import _pytest.config @@ -144,7 +145,9 @@ class pytestPDB(object): else: tw.sep(">", "PDB set_trace") - class PytestPdbWrapper(cls._pdb_cls, object): + pdb_cls = cls._import_pdb_cls() + + class PytestPdbWrapper(pdb_cls, object): _pytest_capman = capman _continued = False @@ -227,7 +230,8 @@ class pytestPDB(object): _pdb = PytestPdbWrapper(**kwargs) cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb) else: - _pdb = cls._pdb_cls(**kwargs) + pdb_cls = cls._import_pdb_cls() + _pdb = pdb_cls(**kwargs) return _pdb @classmethod |