]> granicus.if.org Git - python/commitdiff
Fix SF bug [ 561825 ] Confusing error for "del f()"
authorJeremy Hylton <jeremy@alum.mit.edu>
Fri, 31 May 2002 14:08:29 +0000 (14:08 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Fri, 31 May 2002 14:08:29 +0000 (14:08 +0000)
In the error message, say del for del and assign for everything else.

Lib/test/test_syntax.py [new file with mode: 0644]
Python/compile.c

diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
new file mode 100644 (file)
index 0000000..6605e27
--- /dev/null
@@ -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()
index cdb72acf84e44a5f75e0b90ae5ffbafc3c1b0703..4bbe44f7c981725a59eea060f1a775bdd9ecc881 100644 (file)
@@ -2417,8 +2417,12 @@ com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
        REQ(n, trailer);
        switch (TYPE(CHILD(n, 0))) {
        case LPAR: /* '(' [exprlist] ')' */
-               com_error(c, PyExc_SyntaxError,
-                         "can't assign to function call");
+               if (assigning == OP_DELETE)
+                       com_error(c, PyExc_SyntaxError,
+                                 "can't delete function call");
+               else
+                       com_error(c, PyExc_SyntaxError,
+                                 "can't assign to function call");
                break;
        case DOT: /* '.' NAME */
                if (assigning > OP_APPLY)