summaryrefslogtreecommitdiff
path: root/Lib/inspect.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 661957b513..05bb71b81b 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -432,7 +432,7 @@ def findsource(object):
if not hasattr(object, 'co_firstlineno'):
raise IOError('could not find function definition')
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:
if pat.match(lines[lnum]): break
lnum = lnum - 1
@@ -503,17 +503,28 @@ class BlockFinder:
"""Provide a tokeneater() method to detect the end of a code block."""
def __init__(self):
self.indent = 0
- self.started = 0
+ self.started = False
+ self.passline = False
self.last = 0
def tokeneater(self, type, token, (srow, scol), (erow, ecol), line):
if not self.started:
- if '@' in line: pass
- elif type == tokenize.NAME: self.started = 1
+ if token in ("def", "class", "lambda"):
+ 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:
+ self.passline = False
self.last = srow
+ elif self.passline:
+ pass
elif type == tokenize.INDENT:
self.indent = self.indent + 1
+ self.passline = True
elif type == tokenize.DEDENT:
self.indent = self.indent - 1
if self.indent == 0: