mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-111696, PEP 737: Add %T and %N to PyUnicode_FromFormat() (#116839)
This commit is contained in:
parent
5f52d20a93
commit
7bbb9b57e6
7 changed files with 135 additions and 2 deletions
|
|
@ -1208,7 +1208,7 @@ type_set_module(PyTypeObject *type, PyObject *value, void *context)
|
|||
|
||||
|
||||
PyObject *
|
||||
PyType_GetFullyQualifiedName(PyTypeObject *type)
|
||||
_PyType_GetFullyQualifiedName(PyTypeObject *type, char sep)
|
||||
{
|
||||
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
|
||||
return PyUnicode_FromString(type->tp_name);
|
||||
|
|
@ -1230,7 +1230,7 @@ PyType_GetFullyQualifiedName(PyTypeObject *type)
|
|||
&& !_PyUnicode_Equal(module, &_Py_ID(builtins))
|
||||
&& !_PyUnicode_Equal(module, &_Py_ID(__main__)))
|
||||
{
|
||||
result = PyUnicode_FromFormat("%U.%U", module, qualname);
|
||||
result = PyUnicode_FromFormat("%U%c%U", module, sep, qualname);
|
||||
}
|
||||
else {
|
||||
result = Py_NewRef(qualname);
|
||||
|
|
@ -1240,6 +1240,12 @@ PyType_GetFullyQualifiedName(PyTypeObject *type)
|
|||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyType_GetFullyQualifiedName(PyTypeObject *type)
|
||||
{
|
||||
return _PyType_GetFullyQualifiedName(type, '.');
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
type_abstractmethods(PyTypeObject *type, void *context)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue