Skip to content

Commit 440847c

Browse files
committed
Merged revisions 76416-76417 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r76416 | benjamin.peterson | 2009-11-19 16:54:57 -0600 (Thu, 19 Nov 2009) | 10 lines improve several corner cases related with argument names in parenthesis - Fix python#7362: give a good error message for parenthesized arguments with defaults. - Add a py3k warning for any parenthesized arguments since those are not allowed in Py3. This warning is not given in tuple unpacking, since that incurs the tuple unpacking warning. ........ r76417 | benjamin.peterson | 2009-11-19 16:58:01 -0600 (Thu, 19 Nov 2009) | 1 line add news notes for r76416 ........
1 parent aa4af33 commit 440847c

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎Lib/test/test_py3kwarn.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ def test_backquote(self):
3131
exec "`2`" in {}
3232
self.assertWarning(None, w, expected)
3333

34+
def test_paren_arg_names(self):
35+
expected = 'parenthesized argument names are invalid in 3.x'
36+
def check(s):
37+
exec s in {}
38+
self.assertWarning(None, w, expected)
39+
with check_warnings() as w:
40+
check("def f((x)): pass")
41+
check("def f((((x))), (y)): pass")
42+
check("def f((x), (((y))), m=32): pass")
43+
# Something like def f((a, (b))): pass will raise the tuple
44+
# unpacking warning.
45+
3446
def test_bool_assign(self):
3547
# So we don't screw up our globals
3648
def safe_exec(expr):

‎Lib/test/test_syntax.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,14 @@ def _check_error(self, code, errtext,
447447
self.fail("SyntaxError is not a %s" % subclass.__name__)
448448
mo = re.search(errtext, str(err))
449449
if mo is None:
450-
self.fail("SyntaxError did not contain '%r'" % (errtext,))
450+
self.fail("%s did not contain '%r'" % (err, errtext,))
451451
else:
452452
self.fail("compile() did not raise SyntaxError")
453453

454+
def test_paren_arg_with_default(self):
455+
self._check_error("def f((x)=23): pass",
456+
"parenthesized arg with default")
457+
454458
def test_assign_call(self):
455459
self._check_error("f() = 1", "assign")
456460

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.6.5
1212
Core and Builtins
1313
-----------------
1414

15+
- Add Py3k warnings for parameter names in parenthesis.
16+
17+
- Issue #7362: Give a propery error message for def f((x)=3): pass.
18+
1519
- Issue #7085: Fix crash when importing some extensions in a thread
1620
on MacOSX 10.6.
1721

‎Python/ast.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,8 @@ ast_for_arguments(struct compiling *c, const node *n)
682682
while (i < NCH(n)) {
683683
ch = CHILD(n, i);
684684
switch (TYPE(ch)) {
685-
case fpdef:
685+
case fpdef: {
686+
int complex_args = 0, parenthesized = 0;
686687
handle_fpdef:
687688
/* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
688689
anything other than EQUAL or a comma? */
@@ -697,6 +698,12 @@ ast_for_arguments(struct compiling *c, const node *n)
697698
found_default = 1;
698699
}
699700
else if (found_default) {
701+
/* def f((x)=4): pass should raise an error.
702+
def f((x, (y))): pass will just incur the tuple unpacking warning. */
703+
if (parenthesized && !complex_args) {
704+
ast_error(n, "parenthesized arg with default");
705+
goto error;
706+
}
700707
ast_error(n,
701708
"non-default argument follows default argument");
702709
goto error;
@@ -709,13 +716,15 @@ ast_for_arguments(struct compiling *c, const node *n)
709716
if (Py_Py3kWarningFlag && !ast_warn(c, ch,
710717
"tuple parameter unpacking has been removed in 3.x"))
711718
goto error;
719+
complex_args = 1;
712720
asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
713721
if (!asdl_seq_GET(args, k-1))
714722
goto error;
715723
} else {
716724
/* def foo((x)): setup for checking NAME below. */
717725
/* Loop because there can be many parens and tuple
718726
unpacking mixed in. */
727+
parenthesized = 1;
719728
ch = CHILD(ch, 0);
720729
assert(TYPE(ch) == fpdef);
721730
goto handle_fpdef;
@@ -737,7 +746,13 @@ ast_for_arguments(struct compiling *c, const node *n)
737746

738747
}
739748
i += 2; /* the name and the comma */
749+
if (parenthesized && Py_Py3kWarningFlag &&
750+
!ast_warn(c, ch, "parenthesized argument names "
751+
"are invalid in 3.x"))
752+
goto error;
753+
740754
break;
755+
}
741756
case STAR:
742757
if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
743758
goto error;

0 commit comments

Comments
 (0)