summaryrefslogtreecommitdiff
path: root/Lib/doctest.py
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2022-05-19 18:46:15 +0300
committerGitHub <noreply@github.com>2022-05-19 17:46:15 +0200
commit8db2b3b6878aba9f12844526bce966b7eed81aee (patch)
treec25ebe4f9cb418ece49f9c84207cc1679462e7b0 /Lib/doctest.py
parent09be18a73d81aa82cc57a782f3b0a9811e43ad3b (diff)
downloadcpython-git-8db2b3b6878aba9f12844526bce966b7eed81aee.tar.gz
bpo-28249: fix `lineno` location for empty `DocTest` instances (GH-30498)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/doctest.py')
-rw-r--r--Lib/doctest.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index ed94d15c0e..b2ef2ce636 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1085,19 +1085,21 @@ class DocTestFinder:
def _find_lineno(self, obj, source_lines):
"""
- Return a line number of the given object's docstring. Note:
- this method assumes that the object has a docstring.
+ Return a line number of the given object's docstring.
+
+ Returns `None` if the given object does not have a docstring.
"""
lineno = None
+ docstring = getattr(obj, '__doc__', None)
# Find the line number for modules.
- if inspect.ismodule(obj):
+ if inspect.ismodule(obj) and docstring is not None:
lineno = 0
# Find the line number for classes.
# Note: this could be fooled if a class is defined multiple
# times in a single file.
- if inspect.isclass(obj):
+ if inspect.isclass(obj) and docstring is not None:
if source_lines is None:
return None
pat = re.compile(r'^\s*class\s*%s\b' %
@@ -1109,7 +1111,9 @@ class DocTestFinder:
# Find the line number for functions & methods.
if inspect.ismethod(obj): obj = obj.__func__
- if inspect.isfunction(obj): obj = obj.__code__
+ if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
+ # We don't use `docstring` var here, because `obj` can be changed.
+ obj = obj.__code__
if inspect.istraceback(obj): obj = obj.tb_frame
if inspect.isframe(obj): obj = obj.f_code
if inspect.iscode(obj):