]> granicus.if.org Git - python/commitdiff
SF patch# 1755885 by Kurt Kaiser: show location of Unicode escape errors.
authorGuido van Rossum <guido@python.org>
Wed, 18 Jul 2007 17:19:14 +0000 (17:19 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 18 Jul 2007 17:19:14 +0000 (17:19 +0000)
(Slightly tweaked for style and refcounts.)

Lib/test/test_parser.py
Python/ast.c

index 4bd2943b11333ac2e945c8887b8b623e3b6f1eb8..fd2861034d672cc473f124127cedc1be710d8fec 100644 (file)
@@ -474,6 +474,12 @@ class CompileTestCase(unittest.TestCase):
         st = parser.suite('1 = 3 + 4')
         self.assertRaises(SyntaxError, parser.compilest, st)
 
+    def test_compile_badunicode(self):
+        st = parser.suite('a = u"\U12345678"')
+        self.assertRaises(SyntaxError, parser.compilest, st)
+        st = parser.suite('a = u"\u1"')
+        self.assertRaises(SyntaxError, parser.compilest, st)
+
 def test_main():
     test_support.run_unittest(
         RoundtripLegalSyntaxTestCase,
index 42dcc2d82646410466204e573f9e84d25f8cf2a7..b426df290f7a32889ad3ccfbb189f1a25c736d19 100644 (file)
@@ -1243,9 +1243,26 @@ ast_for_atom(struct compiling *c, const node *n)
                     c->c_arena);
     case STRING: {
         PyObject *str = parsestrplus(c, n);
-        if (!str)
+        if (!str) {
+            if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
+                PyObject *type, *value, *tback, *errstr;
+                PyErr_Fetch(&type, &value, &tback);
+                errstr = ((PyUnicodeErrorObject *)value)->reason;
+                if (errstr) {
+                    char *s = "";
+                    char buf[128];
+                    s = PyString_AsString(errstr);
+                    PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
+                    ast_error(n, buf);
+                } else {
+                    ast_error(n, "(unicode error) unknown error");
+                }
+                Py_DECREF(type);
+                Py_DECREF(value);
+                Py_XDECREF(tback);
+            }
             return NULL;
-
+        }
         PyArena_AddPyObject(c->c_arena, str);
         return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
     }