summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-07-18 17:19:14 (GMT)
committerGuido van Rossum <guido@python.org>2007-07-18 17:19:14 (GMT)
commitb6ac23cd07ce560dcfd41e74e915d6afe4e8a525 (patch)
tree9305b114506e853de2059d7506c0471f30bf051b
parenta3a4c2f4114fb9d43d80681dea155fd7538c9939 (diff)
downloadcpython-b6ac23cd07ce560dcfd41e74e915d6afe4e8a525.zip
cpython-b6ac23cd07ce560dcfd41e74e915d6afe4e8a525.tar.gz
cpython-b6ac23cd07ce560dcfd41e74e915d6afe4e8a525.tar.bz2
SF patch# 1755885 by Kurt Kaiser: show location of Unicode escape errors.
(Slightly tweaked for style and refcounts.)
-rw-r--r--Lib/test/test_parser.py6
-rw-r--r--Python/ast.c21
2 files changed, 25 insertions, 2 deletions
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py
index 4bd2943..fd28610 100644
--- a/Lib/test/test_parser.py
+++ b/Lib/test/test_parser.py
@@ -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,
diff --git a/Python/ast.c b/Python/ast.c
index 42dcc2d..b426df2 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -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);
}