gh-111696, PEP 737: Add %T and %N to PyUnicode_FromFormat() (#116839)

This commit is contained in:
Victor Stinner 2024-03-14 23:23:00 +01:00 committed by GitHub
parent 5f52d20a93
commit 7bbb9b57e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 135 additions and 2 deletions

View file

@ -2791,6 +2791,64 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
break;
}
case 'T':
{
PyObject *obj = va_arg(*vargs, PyObject *);
PyTypeObject *type = (PyTypeObject *)Py_NewRef(Py_TYPE(obj));
PyObject *type_name;
if (f[1] == '#') {
type_name = _PyType_GetFullyQualifiedName(type, ':');
f++;
}
else {
type_name = PyType_GetFullyQualifiedName(type);
}
Py_DECREF(type);
if (!type_name) {
return NULL;
}
if (unicode_fromformat_write_str(writer, type_name,
width, precision, flags) == -1) {
Py_DECREF(type_name);
return NULL;
}
Py_DECREF(type_name);
break;
}
case 'N':
{
PyObject *type_raw = va_arg(*vargs, PyObject *);
assert(type_raw != NULL);
if (!PyType_Check(type_raw)) {
PyErr_SetString(PyExc_TypeError, "%N argument must be a type");
return NULL;
}
PyTypeObject *type = (PyTypeObject*)type_raw;
PyObject *type_name;
if (f[1] == '#') {
type_name = _PyType_GetFullyQualifiedName(type, ':');
f++;
}
else {
type_name = PyType_GetFullyQualifiedName(type);
}
if (!type_name) {
return NULL;
}
if (unicode_fromformat_write_str(writer, type_name,
width, precision, flags) == -1) {
Py_DECREF(type_name);
return NULL;
}
Py_DECREF(type_name);
break;
}
default:
invalid_format:
PyErr_Format(PyExc_SystemError, "invalid format string: %s", p);