Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,10 +1098,17 @@ def test_required_field_messages(self):
):
compile(expr_with_wrong_body, "<test>", "eval")

variable = ast.parse("test", mode="eval")
variable.body.id = b'test'
with self.assertRaisesRegex(TypeError,
"field 'id' was expecting a string object, got bytes"
):
compile(variable, "<test>", "eval")

constant = ast.parse("u'test'", mode="eval")
constant.body.kind = 0xFF
with self.assertRaisesRegex(
TypeError, "field 'kind' was expecting a string or bytes object"
with self.assertRaisesRegex(TypeError,
"field 'kind' was expecting a string or bytes object, got int"
):
compile(constant, "<test>", "eval")

Expand Down
19 changes: 9 additions & 10 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ def typeCheck(self, name):
self.emit("return 1;", 2)
self.emit("}", 1)
self.emit("if (!isinstance && field != NULL) {", 1)
error = "field '%%s' was expecting node of type '%s', got '%%s'" % name
self.emit("PyErr_Format(PyExc_TypeError, \"%s\", field, _PyType_Name(Py_TYPE(obj)));" % error, 2, reflow=False)
error = "field '%%s' was expecting node of type '%s', got '%%T'" % name
self.emit("PyErr_Format(PyExc_TypeError, \"%s\", field, obj);" % error, 2, reflow=False)
self.emit("return 1;", 2)
self.emit("}", 1)

Expand Down Expand Up @@ -692,7 +692,7 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
self.emit("Py_ssize_t i;", depth+1)
self.emit("if (!PyList_Check(tmp)) {", depth+1)
self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
"be a list, not a %%.200s\", _PyType_Name(Py_TYPE(tmp)));" %
"be a list, not a %%T\", tmp);" %
(name, field.name),
depth+2, reflow=False)
self.emit("goto failed;", depth+2)
Expand Down Expand Up @@ -991,10 +991,9 @@ def visitModule(self, mod):

res = 0; /* if no error occurs, this stays 0 to the end */
if (numfields < PyTuple_GET_SIZE(args)) {
PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
PyErr_Format(PyExc_TypeError, "%T constructor takes at most "
"%zd positional argument%s",
_PyType_Name(Py_TYPE(self)),
numfields, numfields == 1 ? "" : "s");
self, numfields, numfields == 1 ? "" : "s");
res = -1;
goto cleanup;
}
Expand Down Expand Up @@ -1748,7 +1747,7 @@ def visitModule(self, mod):
static int obj2ast_identifier(struct ast_state *state, PyObject* obj, PyObject** out, const char* field, PyArena* arena)
{
if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string object", field);
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string object, got %T", field, obj);
return -1;
}
return obj2ast_object(state, obj, out, field, arena);
Expand All @@ -1757,7 +1756,7 @@ def visitModule(self, mod):
static int obj2ast_string(struct ast_state *state, PyObject* obj, PyObject** out, const char* field, PyArena* arena)
{
if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string or bytes object", field);
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string or bytes object, got %T", field, obj);
return -1;
}
return obj2ast_object(state, obj, out, field, arena);
Expand Down Expand Up @@ -2144,8 +2143,8 @@ class PartingShots(StaticVisitor):
return -1;
}
if (!isinstance) {
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
req_name[mode], _PyType_Name(Py_TYPE(ast)));
PyErr_Format(PyExc_TypeError, "expected %s node, got %T",
req_name[mode], ast);
return -1;
}
return 0;
Expand Down
Loading
Loading