Skip to content

bpo-34595: Format string using %T in Python/ #9103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,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",
Py_TYPE(self)->tp_name,
self,
numfields, numfields == 1 ? "" : "s");
res = -1;
goto cleanup;
Expand Down Expand Up @@ -1227,8 +1227,8 @@ class PartingShots(StaticVisitor):
if (isinstance == -1)
return NULL;
if (!isinstance) {
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
req_name[mode], Py_TYPE(ast)->tp_name);
PyErr_Format(PyExc_TypeError, "expected %s node, got %T",
req_name[mode], ast);
return NULL;
}
if (obj2ast_mod(ast, &res, arena) != 0)
Expand Down
8 changes: 4 additions & 4 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ get_once_registry(void)
if (!PyDict_Check(registry)) {
PyErr_Format(PyExc_TypeError,
MODULE_NAME ".onceregistry must be a dict, "
"not '%.200s'",
Py_TYPE(registry)->tp_name);
"not '%T'",
registry);
Py_DECREF(registry);
return NULL;
}
Expand All @@ -135,8 +135,8 @@ get_default_action(void)
if (!PyUnicode_Check(default_action)) {
PyErr_Format(PyExc_TypeError,
MODULE_NAME ".defaultaction must be a string, "
"not '%.200s'",
Py_TYPE(default_action)->tp_name);
"not '%T'",
default_action);
Py_DECREF(default_action);
return NULL;
}
Expand Down Expand Up @@ -194,8 +194,8 @@ get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,

if (!PyUnicode_Check(action)) {
PyErr_Format(PyExc_TypeError,
"action must be a string, not '%.200s'",
Py_TYPE(action)->tp_name);
"action must be a string, not '%T'",
action);
Py_DECREF(tmp_item);
return NULL;
}
Expand Down Expand Up @@ -759,8 +759,8 @@ get_category(PyObject *message, PyObject *category)
PyObject_IsSubclass raised an error */
if (rc == -1 || rc == 0) {
PyErr_Format(PyExc_TypeError,
"category must be a Warning subclass, not '%s'",
Py_TYPE(category)->tp_name);
"category must be a Warning subclass, not '%T'",
category);
return NULL;
}

Expand Down Expand Up @@ -891,8 +891,8 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
if (module_globals && module_globals != Py_None) {
if (!PyDict_Check(module_globals)) {
PyErr_Format(PyExc_TypeError,
"module_globals must be a dict, not '%.200s'",
Py_TYPE(module_globals)->tp_name);
"module_globals must be a dict, not '%T'",
module_globals);
return NULL;
}

Expand Down
9 changes: 4 additions & 5 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
case Constant_kind:
if (!validate_constant(exp->v.Constant.value)) {
PyErr_Format(PyExc_TypeError,
"got an invalid type in Constant: %s",
Py_TYPE(exp->v.Constant.value)->tp_name);
"got an invalid type in Constant: %T",
exp->v.Constant.value);
return 0;
}
return 1;
Expand Down Expand Up @@ -655,9 +655,8 @@ new_identifier(const char *n, struct compiling *c)
return NULL;
if (!PyUnicode_Check(id2)) {
PyErr_Format(PyExc_TypeError,
"unicodedata.normalize() must return a string, not "
"%.200s",
Py_TYPE(id2)->tp_name);
"unicodedata.normalize() must return a string, "
"not %T", id2);
Py_DECREF(id2);
return NULL;
}
Expand Down
8 changes: 4 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
}
if (!PyMapping_Check(ns)) {
PyErr_Format(PyExc_TypeError,
"%.200s.__prepare__() must return a mapping, not %.200s",
"%.200s.__prepare__() must return a mapping, not %T",
isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
Py_TYPE(ns)->tp_name);
ns);
goto error;
}
cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
Expand Down Expand Up @@ -2160,8 +2160,8 @@ builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
if (round == NULL) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
"type %.100s doesn't define __round__ method",
Py_TYPE(number)->tp_name);
"type %T doesn't define __round__ method",
number);
return NULL;
}

Expand Down
16 changes: 8 additions & 8 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1687,8 +1687,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
PyErr_Format(
PyExc_TypeError,
"'async for' received an object from __aiter__ "
"that does not implement __anext__: %.100s",
Py_TYPE(iter)->tp_name);
"that does not implement __anext__: %T",
iter);
Py_DECREF(iter);
goto error;
}
Expand Down Expand Up @@ -1734,8 +1734,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
_PyErr_FormatFromCause(
PyExc_TypeError,
"'async for' received an invalid object "
"from __anext__: %.100s",
Py_TYPE(next_iter)->tp_name);
"from __anext__: %T",
next_iter);

Py_DECREF(next_iter);
goto error;
Expand Down Expand Up @@ -4911,16 +4911,16 @@ import_all_from(PyObject *locals, PyObject *v)
}
if (!PyUnicode_Check(modname)) {
PyErr_Format(PyExc_TypeError,
"module __name__ must be a string, not %.100s",
Py_TYPE(modname)->tp_name);
"module __name__ must be a string, not %T",
modname);
}
else {
PyErr_Format(PyExc_TypeError,
"%s in %U.%s must be str, not %.100s",
"%s in %U.%s must be str, not %T",
skip_leading_underscores ? "Key" : "Item",
modname,
skip_leading_underscores ? "__dict__" : "__all__",
Py_TYPE(name)->tp_name);
name);
}
Py_DECREF(modname);
Py_DECREF(name);
Expand Down
4 changes: 2 additions & 2 deletions Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ _Py_convert_optional_to_ssize_t(PyObject *obj, void *result)
}
else {
PyErr_Format(PyExc_TypeError,
"argument should be integer or None, not '%.200s'",
Py_TYPE(obj)->tp_name);
"argument should be integer or None, not '%T'",
obj);
return 0;
}
*((Py_ssize_t *)result) = limit;
Expand Down
3 changes: 1 addition & 2 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
_PyTime_t t;

if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s",
Py_TYPE(obj)->tp_name);
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
return -1;
}

Expand Down
16 changes: 7 additions & 9 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,7 @@ sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper)

if (wrapper != Py_None) {
if (!PyCallable_Check(wrapper)) {
PyErr_Format(PyExc_TypeError,
"callable expected, got %.50s",
Py_TYPE(wrapper)->tp_name);
PyErr_Format(PyExc_TypeError, "callable expected, got %T", wrapper);
return NULL;
}
_PyEval_SetCoroutineWrapper(wrapper);
Expand Down Expand Up @@ -860,8 +858,8 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
if (finalizer && finalizer != Py_None) {
if (!PyCallable_Check(finalizer)) {
PyErr_Format(PyExc_TypeError,
"callable finalizer expected, got %.50s",
Py_TYPE(finalizer)->tp_name);
"callable finalizer expected, got %T",
finalizer);
return NULL;
}
_PyEval_SetAsyncGenFinalizer(finalizer);
Expand All @@ -873,8 +871,8 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
if (firstiter && firstiter != Py_None) {
if (!PyCallable_Check(firstiter)) {
PyErr_Format(PyExc_TypeError,
"callable firstiter expected, got %.50s",
Py_TYPE(firstiter)->tp_name);
"callable firstiter expected, got %T",
firstiter);
return NULL;
}
_PyEval_SetAsyncGenFirstiter(firstiter);
Expand Down Expand Up @@ -1242,8 +1240,8 @@ _PySys_GetSizeOf(PyObject *o)
if (method == NULL) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
"Type %.100s doesn't define __sizeof__",
Py_TYPE(o)->tp_name);
"Type %T doesn't define __sizeof__",
o);
}
else {
res = _PyObject_CallNoArg(method);
Expand Down
8 changes: 4 additions & 4 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ tb_new_impl(PyTypeObject *type, PyObject *tb_next, PyFrameObject *tb_frame,
tb_next = NULL;
} else if (!PyTraceBack_Check(tb_next)) {
return PyErr_Format(PyExc_TypeError,
"expected traceback object or None, got '%s'",
Py_TYPE(tb_next)->tp_name);
"expected traceback object or None, got '%T'",
tb_next);
}

return tb_create_raw((PyTracebackObject *)tb_next, tb_frame, tb_lasti,
Expand Down Expand Up @@ -118,8 +118,8 @@ tb_next_set(PyTracebackObject *self, PyObject *new_next, void *Py_UNUSED(_))
new_next = NULL;
} else if (!PyTraceBack_Check(new_next)) {
PyErr_Format(PyExc_TypeError,
"expected traceback object, got '%s'",
Py_TYPE(new_next)->tp_name);
"expected traceback object, got '%T'",
new_next);
return -1;
}

Expand Down