Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86da2a3

Browse files
committed
gh-111696: Add %T format to PyUnicode_FromFormat()
1 parentb9f814c commit86da2a3

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

‎Doc/c-api/unicode.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,11 @@ APIs:
518518
- :c:expr:`PyObject*`
519519
- The result of calling :c:func:`PyObject_Repr`.
520520
521+
* - ``T``
522+
- :c:expr:`PyObject*`
523+
- Get the name of a type (``type.__name__``): the result of calling
524+
``PyType_GetName(type)``.
525+
521526
.. note::
522527
The width formatter unit is number of characters rather than bytes.
523528
The precision formatter unit is number of bytes or :c:type:`wchar_t`
@@ -553,6 +558,9 @@ APIs:
553558
In previous versions it caused all the rest of the format string to be
554559
copied as-is to the result string, and any extra arguments discarded.
555560
561+
.. versionchanged:: 3.13
562+
Support for ``"%T"`` added.
563+
556564
557565
.. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
558566

‎Doc/whatsnew/3.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,10 @@ New Features
11641164
:c:func:`PyErr_WriteUnraisable`, but allow to customize the warning mesage.
11651165
(Contributed by Serhiy Storchaka in:gh:`108082`.)
11661166

1167+
* Add support for ``"%T"`` format to:c:func:`PyUnicode_FromFormat`: format
1168+
the name of a type (``type.__name__``).
1169+
(Contributed by Victor Stinner in:gh:`111696`.)
1170+
11671171

11681172
Porting to Python 3.13
11691173
----------------------

‎Lib/test/test_capi/test_unicode.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,23 @@ def check_format(expected, format, *args):
609609
check_format('xyz',
610610
b'%V',None,b'xyz')
611611

612+
# test %T and %#T
613+
obj='abc'
614+
obj_type=type(obj)
615+
check_format('type: str',
616+
b'type: %T',py_object(obj_type))
617+
classLocalType:
618+
pass
619+
obj=LocalType()
620+
obj_type=type(obj)
621+
name='LocalType'
622+
check_format(f'type:{name}',
623+
b'type: %T',py_object(obj_type))
624+
check_format(f'type:{name[:3]}',
625+
b'type: %.3T',py_object(obj_type))
626+
check_format(f'type:{name.rjust(20)}',
627+
b'type: %20T',py_object(obj_type))
628+
612629
# test %ls
613630
check_format('abc',b'%ls',c_wchar_p('abc'))
614631
check_format('\u4eba\u6c11',b'%ls',c_wchar_p('\u4eba\u6c11'))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for ``"%T"`` format to:c:func:`PyUnicode_FromFormat`: format the
2+
name of a type (``type.__name__``). Patch by Victor Stinner.

‎Objects/unicodeobject.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,31 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
27872787
break;
27882788
}
27892789

2790+
case'T':
2791+
{
2792+
PyObject*type_raw=va_arg(*vargs,PyObject*);
2793+
assert(type_raw!=NULL);
2794+
2795+
if (!PyType_Check(type_raw)) {
2796+
PyErr_SetString(PyExc_TypeError,
2797+
"%T argument must be a type; did you forget Py_TYPE()?");
2798+
returnNULL;
2799+
}
2800+
PyTypeObject*type= (PyTypeObject*)type_raw;
2801+
2802+
PyObject*type_name=PyType_GetName(type);
2803+
if (!type_name) {
2804+
returnNULL;
2805+
}
2806+
if (unicode_fromformat_write_str(writer,type_name,
2807+
width,precision,flags)==-1) {
2808+
Py_DECREF(type_name);
2809+
returnNULL;
2810+
}
2811+
Py_DECREF(type_name);
2812+
break;
2813+
}
2814+
27902815
default:
27912816
invalid_format:
27922817
PyErr_Format(PyExc_SystemError,"invalid format string: %s",p);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp