Skip to content

Commit

Permalink
[3.12] gh-117692: Fix AttributeError in DocTestFinder on wrapped …
Browse files Browse the repository at this point in the history
…`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>
  • Loading branch information
3 people authored Apr 10, 2024
1 parent 559b25f commit 653ed76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,14 @@ def _find_lineno(self, obj, source_lines):
obj = obj.fget
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
# We don't use `docstring` var here, because `obj` can be changed.
obj = inspect.unwrap(obj).__code__
obj = inspect.unwrap(obj)
try:
obj = obj.__code__
except AttributeError:
# Functions implemented in C don't necessarily
# have a __code__ attribute.
# If there's no code, there's no lineno
return None
if inspect.istraceback(obj): obj = obj.tb_frame
if inspect.isframe(obj): obj = obj.f_code
if inspect.iscode(obj):
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2496,6 +2496,20 @@ def test_look_in_unwrapped():
'one other test'
"""

if support.check_impl_detail(cpython=True):
def test_wrapped_c_func():
"""
# https://github.com/python/cpython/issues/117692
>>> import binascii
>>> from test.test_doctest.decorator_mod import decorator
>>> c_func_wrapped = decorator(binascii.b2a_hex)
>>> tests = doctest.DocTestFinder(exclude_empty=False).find(c_func_wrapped)
>>> for test in tests:
... print(test.lineno, test.name)
None b2a_hex
"""

def test_unittest_reportflags():
"""Default unittest reporting flags can be set to control reporting
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes a bug when :class:`doctest.DocTestFinder` was failing on wrapped
``builtin_function_or_method``.

0 comments on commit 653ed76

Please sign in to comment.