[Python-checkins] python/dist/src/Lib/test inspect_fodder2.py, 1.1, 1.2 test_inspect.py, 1.17, 1.18

jlgijsbers at users.sourceforge.net jlgijsbers at users.sourceforge.net
Sun Dec 12 17:46:31 CET 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19116/test

Modified Files:
	inspect_fodder2.py test_inspect.py 
Log Message:
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.

Also add a bunch of
'quite-unlikely-to-occur-in-real-life-but-working-anyway' tests.




Index: inspect_fodder2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/inspect_fodder2.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- inspect_fodder2.py	12 Dec 2004 16:20:22 -0000	1.1
+++ inspect_fodder2.py	12 Dec 2004 16:46:28 -0000	1.2
@@ -20,3 +20,36 @@
 @replace
 def gone():
   pass
+
+# line 24
+oll = lambda m: m
+
+# line 27
+tll = lambda g: g and \
+g and \
+g
+
+# line 32
+tlli = lambda d: d and \
+    d
+
+# line 36
+def onelinefunc(): pass
+
+# line 39
+def manyargs(arg1, arg2,
+arg3, arg4): pass
+
+# line 43
+def twolinefunc(m): return m and \
+m
+
+# line 47
+a = [None,
+     lambda x: x,
+     None]
+
+# line 52
+def setfunc(func):
+    globals()["anonymous"] = func
+setfunc(lambda x, y: x*y)

Index: test_inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_inspect.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- test_inspect.py	12 Dec 2004 16:20:22 -0000	1.17
+++ test_inspect.py	12 Dec 2004 16:46:28 -0000	1.18
@@ -187,6 +187,48 @@
     def test_replacing_decorator(self):
         self.assertSourceEqual(mod2.gone, 9, 10)
 
+class TestOneliners(GetSourceBase):
+    fodderFile = mod2
+    def test_oneline_lambda(self):
+        # Test inspect.getsource with a one-line lambda function.
+        self.assertSourceEqual(mod2.oll, 25, 25)
+    
+    def test_threeline_lambda(self):
+        # Test inspect.getsource with a three-line lambda function,
+        # where the second and third lines are _not_ indented.
+        self.assertSourceEqual(mod2.tll, 28, 30)        
+    
+    def test_twoline_indented_lambda(self):
+        # Test inspect.getsource with a two-line lambda function,
+        # where the second line _is_ indented.
+        self.assertSourceEqual(mod2.tlli, 33, 34)
+    
+    def test_onelinefunc(self):
+        # Test inspect.getsource with a regular one-line function.
+        self.assertSourceEqual(mod2.onelinefunc, 37, 37)
+    
+    def test_manyargs(self):
+        # Test inspect.getsource with a regular function where
+        # the arguments are on two lines and _not_ indented and
+        # the body on the second line with the last arguments.
+        self.assertSourceEqual(mod2.manyargs, 40, 41)
+    
+    def test_twolinefunc(self):
+        # Test inspect.getsource with a regular function where
+        # the body is on two lines, following the argument list and
+        # continued on the next line by a \\.
+        self.assertSourceEqual(mod2.twolinefunc, 44, 45)
+    
+    def test_lambda_in_list(self):
+        # Test inspect.getsource with a one-line lambda function
+        # defined in a list, indented.
+        self.assertSourceEqual(mod2.a[1], 49, 49)
+    
+    def test_anonymous(self):
+        # Test inspect.getsource with a lambda function defined
+        # as argument to another function.
+        self.assertSourceEqual(mod2.anonymous, 55, 55)
+
 # Helper for testing classify_class_attrs.
 def attrs_wo_objs(cls):
     return [t[:3] for t in inspect.classify_class_attrs(cls)]
@@ -371,7 +413,7 @@
         self.assert_(('datablob', 'data', A) in attrs, 'missing data')
 
 def test_main():
-    run_unittest(TestDecorators, TestRetrievingSourceCode,
+    run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
                  TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
 
 if __name__ == "__main__":



More information about the Python-checkins mailing list