diff Python/ast.c @ 42308:6775ee6d89d3 legacy-trunk

SF patch# 1755885 by Kurt Kaiser: show location of Unicode escape errors. (Slightly tweaked for style and refcounts.)
author Guido van Rossum <guido@python.org>
date Wed, 18 Jul 2007 17:19:14 +0000
parents c8b0e3b8b456
children dbfc3cf1522d
line wrap: on
line diff
--- a/Python/ast.c	Wed Jul 18 06:37:55 2007 +0000
+++ b/Python/ast.c	Wed Jul 18 17:19:14 2007 +0000
@@ -1243,9 +1243,26 @@
                     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);
     }