]> granicus.if.org Git - python/commitdiff
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
authorJohannes Gijsbers <jlg@dds.nl>
Sat, 12 Mar 2005 16:37:11 +0000 (16:37 +0000)
committerJohannes Gijsbers <jlg@dds.nl>
Sat, 12 Mar 2005 16:37:11 +0000 (16:37 +0000)
etc., had comments after the colon, and some other cases. This patch
take a simpler approach that doesn't rely on looking for a ':'. Thanks
Simon Percivall!

Lib/inspect.py
Lib/test/inspect_fodder2.py
Lib/test/test_inspect.py

index dd89932cafa4c7b5c14ce5d6d2d94f4ea411fcaf..a801a298eac5c7e3b64b60b3d9d7e5f30aab4617 100644 (file)
@@ -504,6 +504,7 @@ class BlockFinder:
     """Provide a tokeneater() method to detect the end of a code block."""
     def __init__(self):
         self.indent = 0
+        self.islambda = False
         self.started = False
         self.passline = False
         self.last = 0
@@ -511,11 +512,8 @@ class BlockFinder:
     def tokeneater(self, type, token, (srow, scol), (erow, ecol), line):
         if not self.started:
             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
+                if token == "lambda":
+                    self.islambda = True
                 self.started = True
             self.passline = True
         elif type == tokenize.NEWLINE:
@@ -523,6 +521,8 @@ class BlockFinder:
             self.last = srow
         elif self.passline:
             pass
+        elif self.islambda:
+            raise EndOfBlock, self.last
         elif type == tokenize.INDENT:
             self.indent = self.indent + 1
             self.passline = True
index 44c757230a8c0315523481fcc2d53ffa2eeeaf5d..f216c825a90e2c73d23942f0e0dcfb874d5b3d00 100644 (file)
@@ -53,3 +53,14 @@ a = [None,
 def setfunc(func):
     globals()["anonymous"] = func
 setfunc(lambda x, y: x*y)
+
+# line 57
+def with_comment():  # hello
+    world
+
+# line 61
+multiline_sig = [
+    lambda (x,
+            y): x+y,
+    None,
+    ]
index 9e60a9c7129c2ff32f7a418c013cb93976f27eda..1fb48c526de9a44926010566c3a220b8fc1d4197 100644 (file)
@@ -229,6 +229,15 @@ class TestOneliners(GetSourceBase):
         # as argument to another function.
         self.assertSourceEqual(mod2.anonymous, 55, 55)
 
+class TestBuggyCases(GetSourceBase):
+    fodderFile = mod2
+
+    def test_with_comment(self):
+        self.assertSourceEqual(mod2.with_comment, 58, 59)
+
+    def test_multiline_sig(self):
+        self.assertSourceEqual(mod2.multiline_sig[0], 63, 64)
+
 # Helper for testing classify_class_attrs.
 def attrs_wo_objs(cls):
     return [t[:3] for t in inspect.classify_class_attrs(cls)]
@@ -414,6 +423,7 @@ class TestClassesAndFunctions(unittest.TestCase):
 
 def test_main():
     run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
+                 TestBuggyCases,
                  TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
 
 if __name__ == "__main__":