Skip to content

Commit e6ffa78

Browse files
authored
Refactoring doctests (#5518)
Refactoring doctests
2 parents 73d918d + bf39e89 commit e6ffa78

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/_pytest/doctest.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -364,30 +364,27 @@ def _patch_unwrap_mock_aware():
364364
contextmanager which replaces ``inspect.unwrap`` with a version
365365
that's aware of mock objects and doesn't recurse on them
366366
"""
367-
real_unwrap = getattr(inspect, "unwrap", None)
368-
if real_unwrap is None:
369-
yield
370-
else:
371-
372-
def _mock_aware_unwrap(obj, stop=None):
373-
try:
374-
if stop is None or stop is _is_mocked:
375-
return real_unwrap(obj, stop=_is_mocked)
376-
return real_unwrap(obj, stop=lambda obj: _is_mocked(obj) or stop(obj))
377-
except Exception as e:
378-
warnings.warn(
379-
"Got %r when unwrapping %r. This is usually caused "
380-
"by a violation of Python's object protocol; see e.g. "
381-
"https://github.com/pytest-dev/pytest/issues/5080" % (e, obj),
382-
PytestWarning,
383-
)
384-
raise
367+
real_unwrap = inspect.unwrap
385368

386-
inspect.unwrap = _mock_aware_unwrap
369+
def _mock_aware_unwrap(obj, stop=None):
387370
try:
388-
yield
389-
finally:
390-
inspect.unwrap = real_unwrap
371+
if stop is None or stop is _is_mocked:
372+
return real_unwrap(obj, stop=_is_mocked)
373+
return real_unwrap(obj, stop=lambda obj: _is_mocked(obj) or stop(obj))
374+
except Exception as e:
375+
warnings.warn(
376+
"Got %r when unwrapping %r. This is usually caused "
377+
"by a violation of Python's object protocol; see e.g. "
378+
"https://github.com/pytest-dev/pytest/issues/5080" % (e, obj),
379+
PytestWarning,
380+
)
381+
raise
382+
383+
inspect.unwrap = _mock_aware_unwrap
384+
try:
385+
yield
386+
finally:
387+
inspect.unwrap = real_unwrap
391388

392389

393390
class DoctestModule(pytest.Module):

0 commit comments

Comments
 (0)