Index: Python/ast.c =================================================================== --- Python/ast.c (revision 67307) +++ Python/ast.c (working copy) @@ -1294,13 +1294,14 @@ if (PyErr_ExceptionMatches(PyExc_UnicodeError)){ PyObject *type, *value, *tback, *errstr; PyErr_Fetch(&type, &value, &tback); - errstr = ((PyUnicodeErrorObject *)value)->reason; + errstr = PyObject_Str(value); if (errstr) { char *s = ""; char buf[128]; s = PyString_AsString(errstr); PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s); ast_error(n, buf); + Py_DECREF(errstr); } else { ast_error(n, "(unicode error) unknown error"); } Index: Lib/test/test_unicodedata.py =================================================================== --- Lib/test/test_unicodedata.py (revision 67307) +++ Lib/test/test_unicodedata.py (working copy) @@ -5,8 +5,10 @@ (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. """#" -import unittest, test.test_support +import sys +import unittest import hashlib +import test.test_support encoding = 'utf-8' @@ -196,6 +198,16 @@ class UnicodeMiscTest(UnicodeDatabaseTest): + def test_failed_import_during_compiling(self): + # Issue 4367 + # Decoding \N escapes requires the unicodedata module. If it can't be + # imported, we shouldn't segfault. + sys.modules["unicodedata"] = None + try: + self.assertRaises(SyntaxError, eval, "u'\N{SOFT HYPHEN}'") + finally: + del sys.modules["unicodedata"] + def test_decimal_numeric_consistent(self): # Test that decimal and numeric are consistent, # i.e. if a character has a decimal value,