Skip to content

Commit

Permalink
Emit warning for broken object
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 5, 2019
1 parent 5fdc2d7 commit eb06200
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog/5404.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Emit a warning when attempting to unwrap a broken object raises an exception,
for easier debugging (`#5080 <https://github.com/pytest-dev/pytest/issues/5080>`__).
21 changes: 17 additions & 4 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import platform
import sys
import traceback
import warnings
from contextlib import contextmanager

import pytest
Expand All @@ -12,6 +13,7 @@
from _pytest.compat import safe_getattr
from _pytest.fixtures import FixtureRequest
from _pytest.outcomes import Skipped
from _pytest.warning_types import PytestWarning

DOCTEST_REPORT_CHOICE_NONE = "none"
DOCTEST_REPORT_CHOICE_CDIFF = "cdiff"
Expand Down Expand Up @@ -368,10 +370,21 @@ def _patch_unwrap_mock_aware():
else:

def _mock_aware_unwrap(obj, stop=None):
if stop is None:
return real_unwrap(obj, stop=_is_mocked)
else:
return real_unwrap(obj, stop=lambda obj: _is_mocked(obj) or stop(obj))
try:
if stop is None or stop is _is_mocked:
return real_unwrap(obj, stop=_is_mocked)
else:
return real_unwrap(
obj, stop=lambda obj: _is_mocked(obj) or stop(obj)
)
except Exception as e:
warnings.warn(
"Got %r when unwrapping %r. This is usually caused "
"by a violation of Python's object protocol; see e.g. "
"https://github.com/pytest-dev/pytest/issues/5080",
PytestWarning,
)
raise

inspect.unwrap = _mock_aware_unwrap
try:
Expand Down

0 comments on commit eb06200

Please sign in to comment.