diff options
| author | Łukasz Langa <lukasz@langa.pl> | 2022-05-19 19:16:57 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-19 19:16:57 (GMT) |
| commit | c146525844b716e73386205ee8bed9268cc73fc8 (patch) | |
| tree | 08f86ca00e38d65868ada13cb66556def07f0daa /Lib/doctest.py | |
| parent | 5d7f3dc3dca506cf23c7a502c5083b44cf759a1c (diff) | |
| download | cpython-c146525844b716e73386205ee8bed9268cc73fc8.zip cpython-c146525844b716e73386205ee8bed9268cc73fc8.tar.gz cpython-c146525844b716e73386205ee8bed9268cc73fc8.tar.bz2 | |
[3.10] bpo-28249: fix `lineno` location for empty `DocTest` instances (GH-30498) (#92981)
(cherry picked from commit 8db2b3b6878aba9f12844526bce966b7eed81aee)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Lib/doctest.py')
| -rw-r--r-- | Lib/doctest.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 67cc343..37b31cf 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): |
