Skip to content

Commit fa74c3e

Browse files
gh-82312: Further improvement of error messages for generating AST nodes
This is a follow up of GH-17715. * Use %T instead of _PyType_Name(). * Add the wrong type name we got for identifier and string.
1 parent 3ff2117 commit fa74c3e

3 files changed

Lines changed: 105 additions & 100 deletions

File tree

Lib/test/test_ast/test_ast.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,17 @@ def test_required_field_messages(self):
10981098
):
10991099
compile(expr_with_wrong_body, "<test>", "eval")
11001100

1101+
variable = ast.parse("test", mode="eval")
1102+
variable.body.id = b'test'
1103+
with self.assertRaisesRegex(TypeError,
1104+
"field 'id' was expecting a string object, got bytes"
1105+
):
1106+
compile(variable, "<test>", "eval")
1107+
11011108
constant = ast.parse("u'test'", mode="eval")
11021109
constant.body.kind = 0xFF
1103-
with self.assertRaisesRegex(
1104-
TypeError, "field 'kind' was expecting a string or bytes object"
1110+
with self.assertRaisesRegex(TypeError,
1111+
"field 'kind' was expecting a string or bytes object, got int"
11051112
):
11061113
compile(constant, "<test>", "eval")
11071114

Parser/asdl_c.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ def typeCheck(self, name):
554554
self.emit("return 1;", 2)
555555
self.emit("}", 1)
556556
self.emit("if (!isinstance && field != NULL) {", 1)
557-
error = "field '%%s' was expecting node of type '%s', got '%%s'" % name
558-
self.emit("PyErr_Format(PyExc_TypeError, \"%s\", field, _PyType_Name(Py_TYPE(obj)));" % error, 2, reflow=False)
557+
error = "field '%%s' was expecting node of type '%s', got '%%T'" % name
558+
self.emit("PyErr_Format(PyExc_TypeError, \"%s\", field, obj);" % error, 2, reflow=False)
559559
self.emit("return 1;", 2)
560560
self.emit("}", 1)
561561

@@ -692,7 +692,7 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
692692
self.emit("Py_ssize_t i;", depth+1)
693693
self.emit("if (!PyList_Check(tmp)) {", depth+1)
694694
self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
695-
"be a list, not a %%.200s\", _PyType_Name(Py_TYPE(tmp)));" %
695+
"be a list, not a %%T\", tmp);" %
696696
(name, field.name),
697697
depth+2, reflow=False)
698698
self.emit("goto failed;", depth+2)
@@ -991,10 +991,9 @@ def visitModule(self, mod):
991991
992992
res = 0; /* if no error occurs, this stays 0 to the end */
993993
if (numfields < PyTuple_GET_SIZE(args)) {
994-
PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
994+
PyErr_Format(PyExc_TypeError, "%T constructor takes at most "
995995
"%zd positional argument%s",
996-
_PyType_Name(Py_TYPE(self)),
997-
numfields, numfields == 1 ? "" : "s");
996+
self, numfields, numfields == 1 ? "" : "s");
998997
res = -1;
999998
goto cleanup;
1000999
}
@@ -1748,7 +1747,7 @@ def visitModule(self, mod):
17481747
static int obj2ast_identifier(struct ast_state *state, PyObject* obj, PyObject** out, const char* field, PyArena* arena)
17491748
{
17501749
if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
1751-
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string object", field);
1750+
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string object, got %T", field, obj);
17521751
return -1;
17531752
}
17541753
return obj2ast_object(state, obj, out, field, arena);
@@ -1757,7 +1756,7 @@ def visitModule(self, mod):
17571756
static int obj2ast_string(struct ast_state *state, PyObject* obj, PyObject** out, const char* field, PyArena* arena)
17581757
{
17591758
if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
1760-
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string or bytes object", field);
1759+
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string or bytes object, got %T", field, obj);
17611760
return -1;
17621761
}
17631762
return obj2ast_object(state, obj, out, field, arena);
@@ -2144,8 +2143,8 @@ class PartingShots(StaticVisitor):
21442143
return -1;
21452144
}
21462145
if (!isinstance) {
2147-
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
2148-
req_name[mode], _PyType_Name(Py_TYPE(ast)));
2146+
PyErr_Format(PyExc_TypeError, "expected %s node, got %T",
2147+
req_name[mode], ast);
21492148
return -1;
21502149
}
21512150
return 0;

0 commit comments

Comments
 (0)