Index: Python/Python-ast.c =================================================================== --- Python/Python-ast.c (revision 86416) +++ Python/Python-ast.c (working copy) @@ -610,7 +610,7 @@ PyObject *s = PyObject_Repr(obj); if (s == NULL) return 1; PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s", - PyBytes_AS_STRING(s)); + _PyUnicode_AsString(s)); Py_DECREF(s); return 1; } @@ -3516,7 +3516,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -4714,7 +4714,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -5832,7 +5832,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -5895,7 +5895,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -6020,7 +6020,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -6051,7 +6051,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -6162,7 +6162,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -6209,7 +6209,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -6304,7 +6304,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; @@ -6475,7 +6475,7 @@ tmp = PyObject_Repr(obj); if (tmp == NULL) goto failed; - PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyBytes_AS_STRING(tmp)); + PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", _PyUnicode_AsString(tmp)); failed: Py_XDECREF(tmp); return 1; Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 86416) +++ Misc/NEWS (working copy) @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #10391: Fix a crash in error-handling when invoking ``compile`` on + malformed ``ast`` trees. + - Issue #10372: Import the warnings module only after the IO library is initialized, so as to avoid bootstrap issues with the '-W' option. Index: Parser/asdl_c.py =================================================================== --- Parser/asdl_c.py (revision 86416) +++ Parser/asdl_c.py (working copy) @@ -375,7 +375,7 @@ # there's really nothing more we can do if this fails ... self.emit("if (tmp == NULL) goto failed;", 1) error = "expected some sort of %s, but got %%.400s" % name - format = "PyErr_Format(PyExc_TypeError, \"%s\", PyBytes_AS_STRING(tmp));" + format = "PyErr_Format(PyExc_TypeError, \"%s\", _PyUnicode_AsString(tmp));" self.emit(format % error, 1, reflow=False) self.emit("failed:", 0) self.emit("Py_XDECREF(tmp);", 1) @@ -805,7 +805,7 @@ PyObject *s = PyObject_Repr(obj); if (s == NULL) return 1; PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s", - PyBytes_AS_STRING(s)); + _PyUnicode_AsString(s)); Py_DECREF(s); return 1; } Index: Lib/test/test_compile.py =================================================================== --- Lib/test/test_compile.py (revision 86416) +++ Lib/test/test_compile.py (working copy) @@ -433,6 +433,13 @@ ast.body = [_ast.BoolOp()] self.assertRaises(TypeError, compile, ast, '', 'exec') + # raise exception when expr is not an expression: + self.assertRaises(TypeError, compile, + _ast.Module(body=[_ast.Expr(_ast.Assign(lineno=0, + col_offset=0), + lineno=0, col_offset=0)], + lineno=0), + 'test', 'exec') def test_main(): support.run_unittest(TestSpecifics)