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

Skip to content

Commit 4bb7d12

Browse files
gh-117692: Fix AttributeError in DocTestFinder on wrapped builtin_or_method (#117699)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent f90ff03 commit 4bb7d12

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
@@ -1140,7 +1140,14 @@ def _find_lineno(self, obj, source_lines):
11401140
obj = obj.fget
11411141
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
11421142
# We don't use `docstring` var here, because `obj` can be changed.
1143-
obj = inspect.unwrap(obj).__code__
1143+
obj = inspect.unwrap(obj)
1144+
try:
1145+
obj = obj.__code__
1146+
except AttributeError:
1147+
# Functions implemented in C don't necessarily
1148+
# have a __code__ attribute.
1149+
# If there's no code, there's no lineno
1150+
return None
11441151
if inspect.istraceback(obj): obj = obj.tb_frame
11451152
if inspect.isframe(obj): obj = obj.f_code
11461153
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
@@ -2553,6 +2553,20 @@ def test_look_in_unwrapped():
25532553
'one other test'
25542554
"""
25552555

2556+
@doctest_skip_if(support.check_impl_detail(cpython=False))
2557+
def test_wrapped_c_func():
2558+
"""
2559+
# https://github.com/python/cpython/issues/117692
2560+
>>> import binascii
2561+
>>> from test.test_doctest.decorator_mod import decorator
2562+
2563+
>>> c_func_wrapped = decorator(binascii.b2a_hex)
2564+
>>> tests = doctest.DocTestFinder(exclude_empty=False).find(c_func_wrapped)
2565+
>>> for test in tests:
2566+
... print(test.lineno, test.name)
2567+
None b2a_hex
2568+
"""
2569+
25562570
def test_unittest_reportflags():
25572571
"""Default unittest reporting flags can be set to control reporting
25582572
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