From: Georg Brandl Date: Wed, 3 May 2006 18:18:32 +0000 (+0000) Subject: Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package. X-Git-Tag: v2.5b1~710 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1bb62309307fa8a199ef5a53ae0f775dfff4fa3f;p=python Bug #1385040: don't allow "def foo(a=1, b): pass" in the compiler package. --- diff --git a/Lib/compiler/transformer.py b/Lib/compiler/transformer.py index 8225dfa3fe..e1a97752d4 100644 --- a/Lib/compiler/transformer.py +++ b/Lib/compiler/transformer.py @@ -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 diff --git a/Lib/test/test_compiler.py b/Lib/test/test_compiler.py index 483bc18289..48f1643be8 100644 --- a/Lib/test/test_compiler.py +++ b/Lib/test/test_compiler.py @@ -56,6 +56,9 @@ class CompilerTest(unittest.TestCase): def testYieldExpr(self): compiler.compile("def g(): yield\n\n", "", "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__ diff --git a/Misc/NEWS b/Misc/NEWS index 8d88d9dc51..cedefef31d 100644 --- 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.