cpython: 5400e21e92a7 (original) (raw)
Mercurial > cpython
changeset 97032:5400e21e92a7 3.5
Issue #24485: Function source inspection fails on closures. The fix for Issue #21217 introduced a regression that caused `inspect.getsource` to return incorrect results on nested functions. The root cause of the regression was due to switching the implementation to analyze the underlying bytecode instead of the source code. This commit switches things back to analyzing the source code in a more complete way. The original bug and the regression are both fixed by the new source code analysis. [#24485]
Meador Inge meadori@gmail.com | |
---|---|
date | Thu, 23 Jul 2015 22:49:37 -0500 |
parents | 732de5ba1f2b |
children | 0e7d64595223 d8229c26dd92 |
files | Lib/inspect.py Lib/test/test_inspect.py Misc/NEWS |
diffstat | 3 files changed, 29 insertions(+), 5 deletions(-)[+] [-] Lib/inspect.py 26 Lib/test/test_inspect.py 1 Misc/NEWS 7 |
line wrap: on
line diff
--- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -859,21 +859,37 @@ class BlockFinder: self.islambda = False self.started = False self.passline = False
self.indecorator = False[](#l1.7)
self.decoratorhasargs = False[](#l1.8) self.last = 1[](#l1.9)
def tokeneater(self, type, token, srowcol, erowcol, line):
if not self.started:[](#l1.12)
if not self.started and not self.indecorator:[](#l1.13)
# skip any decorators[](#l1.14)
if token == "@":[](#l1.15)
self.indecorator = True[](#l1.16) # look for the first "def", "class" or "lambda"[](#l1.17)
if token in ("def", "class", "lambda"):[](#l1.18)
elif token in ("def", "class", "lambda"):[](#l1.19) if token == "lambda":[](#l1.20) self.islambda = True[](#l1.21) self.started = True[](#l1.22) self.passline = True # skip to the end of the line[](#l1.23)
elif token == "(":[](#l1.24)
if self.indecorator:[](#l1.25)
self.decoratorhasargs = True[](#l1.26)
elif token == ")":[](#l1.27)
if self.indecorator:[](#l1.28)
self.indecorator = False[](#l1.29)
self.decoratorhasargs = False[](#l1.30) elif type == tokenize.NEWLINE:[](#l1.31) self.passline = False # stop skipping when a NEWLINE is seen[](#l1.32) self.last = srowcol[0][](#l1.33) if self.islambda: # lambdas always end at the first NEWLINE[](#l1.34) raise EndOfBlock[](#l1.35)
# hitting a NEWLINE when in a decorator without args[](#l1.36)
# ends the decorator[](#l1.37)
if self.indecorator and not self.decoratorhasargs:[](#l1.38)
self.indecorator = False[](#l1.39) elif self.passline:[](#l1.40) pass[](#l1.41) elif type == tokenize.INDENT:[](#l1.42)
@@ -913,8 +929,10 @@ def getsourcelines(object): object = unwrap(object) lines, lnum = findsource(object)
- if ismodule(object):
return lines, 0[](#l1.50)
- else:
return getblock(lines[lnum:]), lnum + 1[](#l1.52)
def getsource(object): """Return the text of the source code for an object.
--- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -464,7 +464,6 @@ class TestDecorators(GetSourceBase): def test_getsource_unwrap(self): self.assertSourceEqual(mod2.real, 130, 132)
- @unittest.expectedFailure def test_decorator_with_lambda(self): self.assertSourceEqual(mod2.func114, 113, 115)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -28,6 +28,9 @@ Core and Builtins
Library
-------
+- Issue #22485: Fixed an issue that caused inspect.getsource
to return incorrect
- Issue #22153: Improve unittest docs. Patch from Martin Panter and evilzero.
- Issue #24580: Symbolic group references to open group in re patterns now are @@ -551,6 +554,10 @@ Library
- Issue #23342: Add a subprocess.run() function than returns a CalledProcess instance for a more consistent API than the existing call* functions. +- Issue #21217: inspect.getsourcelines() now tries to compute the start and end