Index: Python/ast.c =================================================================== --- Python/ast.c (revision 56438) +++ Python/ast.c (working copy) @@ -1243,9 +1243,23 @@ c->c_arena); case STRING: { PyObject *str = parsestrplus(c, n); - if (!str) + if (!str){ + PyObject *type, *value, *tback, *errstr; + if (PyErr_ExceptionMatches(PyExc_UnicodeError)){ + 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"); + } + } return NULL; - + } PyArena_AddPyObject(c->c_arena, str); return Str(str, LINENO(n), n->n_col_offset, c->c_arena); } Index: Lib/test/test_parser.py =================================================================== --- Lib/test/test_parser.py (revision 56438) +++ Lib/test/test_parser.py (working copy) @@ -474,6 +474,12 @@ 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,