From: Raymond Hettinger Date: Sun, 19 Jan 2003 14:16:19 +0000 (+0000) Subject: Backport 1.40 and 1.41 so that inspect isn't fooled by single line X-Git-Tag: v2.2.3c1~169 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=28f35a2afa71b63ca5a904a608621cfc9cc2c3f9;p=thirdparty%2FPython%2Fcpython.git Backport 1.40 and 1.41 so that inspect isn't fooled by single line definitions, pass statements, and lambdas. --- diff --git a/Lib/inspect.py b/Lib/inspect.py index 9e2d23d58baa..0248dd59c201 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -410,7 +410,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') + pat = re.compile(r'^(\s*def\s)|(.*\slambda(:|\s))') while lnum > 0: if pat.match(lines[lnum]): break lnum = lnum - 1 @@ -489,6 +489,8 @@ class BlockFinder: elif type == tokenize.DEDENT: self.indent = self.indent - 1 if self.indent == 0: raise EndOfBlock, self.last + elif type == tokenize.NAME and scol == 0: + raise EndOfBlock, self.last def getblock(lines): """Extract the block of code at the top of the given list of lines.""" @@ -496,6 +498,8 @@ def getblock(lines): tokenize.tokenize(ListReader(lines).readline, BlockFinder().tokeneater) except EndOfBlock, eob: return lines[:eob.args[0]] + # Fooling the indent/dedent logic implies a one-line definition + return lines[:1] def getsourcelines(object): """Return a list of source lines and starting line number for an object.