Kaydet (Commit) 01b53e1f authored tarafından Johannes Gijsbers's avatar Johannes Gijsbers

Patch #1011890: fix inspect.getsource breaking with line-continuation &

more. Thanks to Simon Percivall!

The patch makes changes to inspect.py in two places:

* the pattern to match against functions at line 436 is
modified: lambdas should be matched even if not
preceded by whitespace, as long as "lambda" isn't part
of another word.

* the BlockFinder class is heavily modified. Changes are:
- checking for "def", "class" or "lambda" names
before setting self.started to True. Then checking the
same line for word characters after the colon (if the
colon is on that line). If so, and the line does not
end with a line continuation marker, raise EndOfBlock
immediately.
- adding self.passline to show that the line is to be
included and no more checking is necessary on that
line. Since a NEWLINE token is not generated when a
line continuation marker exists, this allows getsource
to continue with these functions even if the following
line would not be indented.

Don't backport the tests, as these have been too heavily modified on the trunk.
üst 4dc49d61
...@@ -432,7 +432,7 @@ def findsource(object): ...@@ -432,7 +432,7 @@ def findsource(object):
if not hasattr(object, 'co_firstlineno'): if not hasattr(object, 'co_firstlineno'):
raise IOError('could not find function definition') raise IOError('could not find function definition')
lnum = object.co_firstlineno - 1 lnum = object.co_firstlineno - 1
pat = re.compile(r'^(\s*def\s)|(.*\slambda(:|\s))|^(\s*@)') pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
while lnum > 0: while lnum > 0:
if pat.match(lines[lnum]): break if pat.match(lines[lnum]): break
lnum = lnum - 1 lnum = lnum - 1
...@@ -503,17 +503,28 @@ class BlockFinder: ...@@ -503,17 +503,28 @@ class BlockFinder:
"""Provide a tokeneater() method to detect the end of a code block.""" """Provide a tokeneater() method to detect the end of a code block."""
def __init__(self): def __init__(self):
self.indent = 0 self.indent = 0
self.started = 0 self.started = False
self.passline = False
self.last = 0 self.last = 0
def tokeneater(self, type, token, (srow, scol), (erow, ecol), line): def tokeneater(self, type, token, (srow, scol), (erow, ecol), line):
if not self.started: if not self.started:
if '@' in line: pass if token in ("def", "class", "lambda"):
elif type == tokenize.NAME: self.started = 1 lastcolon = line.rfind(":")
if lastcolon:
oneline = re.search(r"\w", line[lastcolon:])
if oneline and line[-2:] != "\\\n":
raise EndOfBlock, srow
self.started = True
self.passline = True
elif type == tokenize.NEWLINE: elif type == tokenize.NEWLINE:
self.passline = False
self.last = srow self.last = srow
elif self.passline:
pass
elif type == tokenize.INDENT: elif type == tokenize.INDENT:
self.indent = self.indent + 1 self.indent = self.indent + 1
self.passline = True
elif type == tokenize.DEDENT: elif type == tokenize.DEDENT:
self.indent = self.indent - 1 self.indent = self.indent - 1
if self.indent == 0: if self.indent == 0:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment