mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Fix SF bug [ 561825 ] Confusing error for "del f()"
In the error message, say del for del and assign for everything else.
This commit is contained in:
parent
5352d8c0c6
commit
05ab2e693c
2 changed files with 40 additions and 2 deletions
34
Lib/test/test_syntax.py
Normal file
34
Lib/test/test_syntax.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import re
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import test_support
|
||||||
|
|
||||||
|
class SyntaxTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def _check_error(self, code, errtext,
|
||||||
|
filename="<testcase>", mode="exec"):
|
||||||
|
"""Check that compiling code raises SyntaxError with errtext.
|
||||||
|
|
||||||
|
errtest is a regular expression that must be present in the
|
||||||
|
test of the exception raised.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
compile(code, filename, mode)
|
||||||
|
except SyntaxError, err:
|
||||||
|
mo = re.search(errtext, str(err))
|
||||||
|
if mo is None:
|
||||||
|
self.fail("SyntaxError did not contain '%s'" % `errtext`)
|
||||||
|
else:
|
||||||
|
self.fail("compile() did not raise SyntaxError")
|
||||||
|
|
||||||
|
def test_assign_call(self):
|
||||||
|
self._check_error("f() = 1", "assign")
|
||||||
|
|
||||||
|
def test_assign_del(self):
|
||||||
|
self._check_error("del f()", "delete")
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(SyntaxTestCase)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
|
@ -2417,8 +2417,12 @@ com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
|
||||||
REQ(n, trailer);
|
REQ(n, trailer);
|
||||||
switch (TYPE(CHILD(n, 0))) {
|
switch (TYPE(CHILD(n, 0))) {
|
||||||
case LPAR: /* '(' [exprlist] ')' */
|
case LPAR: /* '(' [exprlist] ')' */
|
||||||
com_error(c, PyExc_SyntaxError,
|
if (assigning == OP_DELETE)
|
||||||
"can't assign to function call");
|
com_error(c, PyExc_SyntaxError,
|
||||||
|
"can't delete function call");
|
||||||
|
else
|
||||||
|
com_error(c, PyExc_SyntaxError,
|
||||||
|
"can't assign to function call");
|
||||||
break;
|
break;
|
||||||
case DOT: /* '.' NAME */
|
case DOT: /* '.' NAME */
|
||||||
if (assigning > OP_APPLY)
|
if (assigning > OP_APPLY)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue