8000 bpo-17446: Get line numbers of properties in doctest by mscuthbert · Pull Request #3419 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-17446: Get line numbers of properties in doctest #3419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-17446: Get line numbers of properties in doctest
  • Loading branch information
mscuthbert committed Sep 7, 2017
commit c0c4d6cae8dab59fe1766cad9d2a700dd789aa50
6 changes: 6 additions & 0 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,12 @@ def _find_lineno(self, obj, source_lines):
if inspect.isframe(obj): obj = obj.f_code
if inspect.iscode(obj):
lineno = getattr(obj, 'co_firstlineno', None)-1
if lineno is None and isinstance(obj, property) \
and obj.fget is not None \
and hasattr(obj.fget, "__code__"):
obj = obj.fget.__code__
# no need to subtract 1 because of decorator line
lineno = getattr(obj, 'co_firstlineno', None)

# Find the line number where the docstring starts. Assume
# that it's the first line that begins with a quote mark.
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,23 @@ def basics(): r"""
>>> test = doctest.DocTestFinder().find(f)[0]
>>> [e.lineno for e in test.examples]
[1, 9, 12]


Line numbers of properties
~~~~~~~~~~~~~~~~~~~~~~~~~~
DocTestFinder finds the line numbers of each propery example

>>> @property
... def foo(x):
... '''
... >>> 1/1
... 1
... >>> 41 + 1
... 42
... '''
>>> test = doctest.DocTestFinder().find(foo, "foo")[0]
>>> [e.lineno for e in test.examples]
[1, 3]
"""

if int.__doc__: # simple check for --without-doc-strings, skip if lacking
Expand Down
0