From 40d3a67a190ad1e1f49e123072abd7f4757fdf22 Mon Sep 17 00:00:00 2001 From: Amaury Forgeot d'Arc Date: Sun, 9 Dec 2007 21:49:48 +0000 Subject: Issue #1573, second attempt: "def f(*, **kw)" now raises a SyntaxError. --- Lib/test/test_ast.py | 3 --- Lib/test/test_keywordonlyarg.py | 8 +------- Python/ast.c | 11 ++++++++--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 03a877b..4883ed5 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -58,9 +58,6 @@ exec_tests = [ "break", # Continue "continue", - # kw only funcs - "def f(*, kw=1): pass", - "def f(*, **kw): pass", ] # These are compiled through "single" diff --git a/Lib/test/test_keywordonlyarg.py b/Lib/test/test_keywordonlyarg.py index eeda547..e64a6cd 100644 --- a/Lib/test/test_keywordonlyarg.py +++ b/Lib/test/test_keywordonlyarg.py @@ -48,6 +48,7 @@ class KeywordOnlyArgTestCase(unittest.TestCase): self.assertRaisesSyntaxError("def f(p1, *, p1=100):\n pass\n") self.assertRaisesSyntaxError("def f(p1, *k1, k1=100):\n pass\n") self.assertRaisesSyntaxError("def f(p1, *, k1, k1=100):\n pass\n") + self.assertRaisesSyntaxError("def f(p1, *, **k1):\n pass\n") self.assertRaisesSyntaxError("def f(p1, *, k1, **k1):\n pass\n") self.assertRaisesSyntaxError("def f(p1, *, None, **k1):\n pass\n") self.assertRaisesSyntaxError("def f(p, *, (k1, k2), **kw):\n pass\n") @@ -144,13 +145,6 @@ class KeywordOnlyArgTestCase(unittest.TestCase): except TypeError: pass - def test_doublestar_only(self): - def f(*, **kw): - return kw - - self.assertEqual(f(), {}) - self.assertEqual(f(k1=1, k2=2), {'k1' : 1, 'k2' : 2}) - def test_kwonly_methods(self): class Example: def f(self, *, k1=1, k2=2): diff --git a/Python/ast.c b/Python/ast.c index 0127281..f32f587 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -649,8 +649,12 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start, arg_ty arg; int i = start; int j = 0; /* index for kwdefaults and kwonlyargs */ - assert((kwonlyargs != NULL && kwdefaults != NULL) || - TYPE(CHILD(n, i)) == DOUBLESTAR); + + if (kwonlyargs == NULL) { + ast_error(CHILD(n, start), "named arguments must follow bare *"); + return -1; + } + assert(kwdefaults != NULL); while (i < NCH(n)) { ch = CHILD(n, i); switch (TYPE(ch)) { @@ -814,7 +818,8 @@ ast_for_arguments(struct compiling *c, const node *n) break; case STAR: if (i+1 >= NCH(n)) { - ast_error(CHILD(n, i), "no name for vararg"); + ast_error(CHILD(n, i), + "named arguments must follow bare *"); goto error; } ch = CHILD(n, i+1); /* tfpdef or COMMA */ -- cgit v0.12