Commit 886483e2 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-34595: Add %T format to PyUnicode_FromFormatV() (GH-9080)

* Add %T format to PyUnicode_FromFormatV(), and so to
  PyUnicode_FromFormat() and PyErr_Format(), to format an object type
  name: equivalent to "%s" with Py_TYPE(obj)->tp_name.
* Replace Py_TYPE(obj)->tp_name with %T format in unicodeobject.c.
* Add unit test on %T format.
* Rename unicode_fromformat_write_cstr() to
  unicode_fromformat_write_utf8(), to make the intent more explicit.
parent 254a4663
......@@ -519,6 +519,9 @@ APIs:
| :attr:`%R` | PyObject\* | The result of calling |
| | | :c:func:`PyObject_Repr`. |
+-------------------+---------------------+--------------------------------+
| :attr:`%T` | PyObject\* | Object type name, equivalent |
| | | to ``Py_TYPE(op)->tp_name``. |
+-------------------+---------------------+--------------------------------+
An unrecognized format character causes all the rest of the format string to be
copied as-is to the result string, and any extra arguments discarded.
......@@ -543,6 +546,9 @@ APIs:
Support width and precision formatter for ``"%s"``, ``"%A"``, ``"%U"``,
``"%V"``, ``"%S"``, ``"%R"`` added.
.. versionchanged:: 3.7
Support for ``"%T"`` (object type name) added.
.. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
......
......@@ -2655,6 +2655,10 @@ class CAPITest(unittest.TestCase):
check_format(r"%A:'abc\xe9\uabcd\U0010ffff'",
b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
# test %T (object type name)
check_format(r"type name: str",
b'type name: %T', 'text')
# test %V
check_format('repr=abc',
b'repr=%V', 'abc', b'xyz')
......
:c:func:`PyUnicode_FromFormatV`: add ``%T`` format to
:c:func:`PyUnicode_FromFormatV`, and so to :c:func:`PyUnicode_FromFormat`
and :c:func:`PyErr_Format`, to format an object type name: equivalent to
"%s" with ``Py_TYPE(obj)->tp_name``.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment