8000 [3.12] gh-117692: Fix `AttributeError` in `DocTestFinder` on wrapped … · python/cpython@653ed76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 653ed76

Browse files
miss-islingtonsobolevnAlexWaygood
authored
[3.12] gh-117692: Fix AttributeError in DocTestFinder on wrapped builtin_or_method (GH-117699) (#117708)
* gh-117692: Fix `AttributeError` in `DocTestFinder` on wrapped `builtin_or_method` (GH-117699) (cherry picked from commit 4bb7d12) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 559b25f commit 653ed76

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/doctest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,14 @@ def _find_lineno(self, obj, source_lines):
11241124
obj = obj.fget
11251125
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
11261126
# We don't use `docstring` var here, because `obj` can be changed.
1127-
obj = inspect.unwrap(obj).__code__
1127+
obj = inspect.unwrap(obj)
1128+
try:
1129+
obj = obj.__code__
1130+
except AttributeError:
1131+
# Functions implemented in C don't necessarily
1132+
# have a __code__ attribute.
1133+
# If there's no code, there's no lineno
1134+
return None
11281135
if inspect.istraceback(obj): obj = obj.tb_frame
11291136
if inspect.isframe(obj): obj = obj.f_code
11301137
if inspect.iscode(obj):

Lib/test/test_doctest/test_doctest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,20 @@ def test_look_in_unwrapped():
24962496
'one other test'
24972497
"""
24982498

2499+
if support.check_impl_detail(cpython=True):
2500+
def test_wrapped_c_func():
2501+
"""
2502+
# https://github.com/python/cpython/issues/117692
2503+
>>> import binascii
2504+
>>> from test.test_doctest.decorator_mod import decorator
2505+
2506+
>>> c_func_wrapped = decorator(binascii.b2a_hex)
2507+
>>> tests = doctest.DocTestFinder(exclude_empty=False).find(c_func_wrapped)
2508+
>>> for test in tests:
2509+
... print(test.lineno, test.name)
2510+
None b2a_hex
2511+
"""
2512+
24992513
def test_unittest_reportflags():
25002514
"""Default unittest reporting flags can be set to control reporting
25012515
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes a bug when :class:`doctest.DocTestFinder` was failing on wrapped
2+
``builtin_function_or_method``.

0 commit comments

Comments
 (0)
0