]> granicus.if.org Git - python/commitdiff
Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package.
authorGeorg Brandl <georg@python.org>
Wed, 3 May 2006 18:18:32 +0000 (18:18 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 3 May 2006 18:18:32 +0000 (18:18 +0000)
Lib/compiler/transformer.py
Lib/test/test_compiler.py
Misc/NEWS

index 8225dfa3fe52af71ecac5840176709457ff45c7a..e1a97752d49541b8befca1e97bbbc716df97880f 100644 (file)
@@ -841,17 +841,15 @@ class Transformer:
             names.append(self.com_fpdef(node))
 
             i = i + 1
-            if i >= len(nodelist):
-                break
-
-            if nodelist[i][0] == token.EQUAL:
+            if i < len(nodelist) and nodelist[i][0] == token.EQUAL:
                 defaults.append(self.com_node(nodelist[i + 1]))
                 i = i + 2
             elif len(defaults):
-                # XXX This should be a syntax error.
-                # Treat "(a=1, b)" as "(a=1, b=None)"
-                defaults.append(Const(None))
+                # we have already seen an argument with default, but here
+                # came one without
+                raise SyntaxError, "non-default argument follows default argument"
 
+            # skip the comma
             i = i + 1
 
         return names, defaults, flags
index 483bc1828980793a2e17cf87d36ede427ace90bc..48f1643be89122bf7c21390af6b7ed72f45373fa 100644 (file)
@@ -56,6 +56,9 @@ class CompilerTest(unittest.TestCase):
     def testYieldExpr(self):
         compiler.compile("def g(): yield\n\n", "<string>", "exec")
 
+    def testDefaultArgs(self):
+        self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
+
     def testLineNo(self):
         # Test that all nodes except Module have a correct lineno attribute.
         filename = __file__
index 8d88d9dc51728860a6214843dd0cc689b93c0d73..cedefef31d7421774cae00d71c6b89a4b676fe20 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -91,6 +91,9 @@ Extension Modules
 Library
 -------
 
+- Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler
+  package.
+
 - Patch #1472854: make the rlcompleter.Completer class usable on non-
   UNIX platforms.