SF bug #661184: inspect.getsource bug
authorRaymond Hettinger <python@rcn.com>
Tue, 14 Jan 2003 02:19:36 +0000 (02:19 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 14 Jan 2003 02:19:36 +0000 (02:19 +0000)
inspect.getsource would crash with one line definitions like:
   def f(x): return x
or
   f = lambda x: x

Lib/inspect.py

index d3fd3ad195a6346a4cda6e8f4c1def0fa39f2b72..77129fd7324d7e0e194cea51e3dfe3c431d6bd7f 100644 (file)
@@ -417,7 +417,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
@@ -508,6 +508,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.