From 998b80636690ffbdb0a278810d9c031fad38631d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 12 Sep 2018 00:23:25 +0200 Subject: Revert "bpo-34595: Add %T format to PyUnicode_FromFormatV() (GH-9080)" (GH-9187) This reverts commit 886483e2b9bbabf60ab769683269b873381dd5ee. --- Doc/c-api/unicode.rst | 6 -- Lib/test/test_unicode.py | 4 - .../C API/2018-09-06-11-17-49.bpo-34595.Hkz62y.rst | 4 - Objects/unicodeobject.c | 105 +++++++++++---------- 4 files changed, 53 insertions(+), 66 deletions(-) delete mode 100644 Misc/NEWS.d/next/C API/2018-09-06-11-17-49.bpo-34595.Hkz62y.rst diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst index 66b1efc..92e22b1 100644 --- a/Doc/c-api/unicode.rst +++ b/Doc/c-api/unicode.rst @@ -519,9 +519,6 @@ 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. @@ -546,9 +543,6 @@ 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) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 73111f1..fb7bb2d 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -2655,10 +2655,6 @@ 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') diff --git a/Misc/NEWS.d/next/C API/2018-09-06-11-17-49.bpo-34595.Hkz62y.rst b/Misc/NEWS.d/next/C API/2018-09-06-11-17-49.bpo-34595.Hkz62y.rst deleted file mode 100644 index c054a8e..0000000 --- a/Misc/NEWS.d/next/C API/2018-09-06-11-17-49.bpo-34595.Hkz62y.rst +++ /dev/null @@ -1,4 +0,0 @@ -: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``. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 3e61c9c..a797f83 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -768,7 +768,8 @@ ensure_unicode(PyObject *obj) { if (!PyUnicode_Check(obj)) { PyErr_Format(PyExc_TypeError, - "must be str, not %T", obj); + "must be str, not %.100s", + Py_TYPE(obj)->tp_name); return -1; } return PyUnicode_READY(obj); @@ -2529,7 +2530,7 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, } static int -unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str, +unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t width, Py_ssize_t precision) { /* UTF-8 */ @@ -2746,7 +2747,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, { /* UTF-8 */ const char *s = va_arg(*vargs, const char*); - if (unicode_fromformat_write_utf8(writer, s, width, precision) < 0) + if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) return NULL; break; } @@ -2772,7 +2773,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, } else { assert(str != NULL); - if (unicode_fromformat_write_utf8(writer, str, width, precision) < 0) + if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) return NULL; } break; @@ -2826,17 +2827,6 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, break; } - case 'T': - { - /* Object type name (tp_name) */ - PyObject *obj = va_arg(*vargs, PyObject *); - PyTypeObject *type = Py_TYPE(obj); - const char *type_name = type->tp_name; - if (unicode_fromformat_write_utf8(writer, type_name, -1, -1) < 0) { - return NULL; - } - break; - } case '%': if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) return NULL; @@ -3034,7 +3024,8 @@ PyUnicode_FromObject(PyObject *obj) return _PyUnicode_Copy(obj); } PyErr_Format(PyExc_TypeError, - "Can't convert '%T' object to str implicitly", obj); + "Can't convert '%.100s' object to str implicitly", + Py_TYPE(obj)->tp_name); return NULL; } @@ -3070,8 +3061,8 @@ PyUnicode_FromEncodedObject(PyObject *obj, /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { PyErr_Format(PyExc_TypeError, - "decoding to str: need a bytes-like object, %T found", - obj); + "decoding to str: need a bytes-like object, %.80s found", + Py_TYPE(obj)->tp_name); return NULL; } @@ -3201,9 +3192,10 @@ PyUnicode_Decode(const char *s, goto onError; if (!PyUnicode_Check(unicode)) { PyErr_Format(PyExc_TypeError, - "'%.400s' decoder returned '%T' instead of 'str'; " + "'%.400s' decoder returned '%.400s' instead of 'str'; " "use codecs.decode() to decode to arbitrary types", - encoding, unicode); + encoding, + Py_TYPE(unicode)->tp_name); Py_DECREF(unicode); goto onError; } @@ -3263,9 +3255,10 @@ PyUnicode_AsDecodedUnicode(PyObject *unicode, goto onError; if (!PyUnicode_Check(v)) { PyErr_Format(PyExc_TypeError, - "'%.400s' decoder returned '%T' instead of 'str'; " + "'%.400s' decoder returned '%.400s' instead of 'str'; " "use codecs.decode() to decode to arbitrary types", - encoding, unicode); + encoding, + Py_TYPE(unicode)->tp_name); Py_DECREF(v); goto onError; } @@ -3496,9 +3489,10 @@ PyUnicode_AsEncodedString(PyObject *unicode, } PyErr_Format(PyExc_TypeError, - "'%.400s' encoder returned '%T' instead of 'bytes'; " + "'%.400s' encoder returned '%.400s' instead of 'bytes'; " "use codecs.encode() to encode to arbitrary types", - encoding, v); + encoding, + Py_TYPE(v)->tp_name); Py_DECREF(v); return NULL; } @@ -3529,9 +3523,10 @@ PyUnicode_AsEncodedUnicode(PyObject *unicode, goto onError; if (!PyUnicode_Check(v)) { PyErr_Format(PyExc_TypeError, - "'%.400s' encoder returned '%T' instead of 'str'; " + "'%.400s' encoder returned '%.400s' instead of 'str'; " "use codecs.encode() to encode to arbitrary types", - encoding, v); + encoding, + Py_TYPE(v)->tp_name); Py_DECREF(v); goto onError; } @@ -3703,11 +3698,9 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) if (!PyBytes_Check(path) && PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "path should be string, bytes, " - "or os.PathLike, not %T", - arg)) - { - Py_DECREF(path); + "path should be string, bytes, or os.PathLike, not %.200s", + Py_TYPE(arg)->tp_name)) { + Py_DECREF(path); return 0; } path_bytes = PyBytes_FromObject(path); @@ -3724,8 +3717,8 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) } else { PyErr_Format(PyExc_TypeError, - "path should be string, bytes, or os.PathLike, not %T", - arg); + "path should be string, bytes, or os.PathLike, not %.200s", + Py_TYPE(arg)->tp_name); Py_DECREF(path); return 0; } @@ -9893,8 +9886,9 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq else { if (!PyUnicode_Check(separator)) { PyErr_Format(PyExc_TypeError, - "separator: expected str instance, %T found", - separator); + "separator: expected str instance," + " %.80s found", + Py_TYPE(separator)->tp_name); goto onError; } if (PyUnicode_READY(separator)) @@ -9925,8 +9919,9 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq item = items[i]; if (!PyUnicode_Check(item)) { PyErr_Format(PyExc_TypeError, - "sequence item %zd: expected str instance, %T found", - i, item); + "sequence item %zd: expected str instance," + " %.80s found", + i, Py_TYPE(item)->tp_name); goto onError; } if (PyUnicode_READY(item) == -1) @@ -10741,7 +10736,7 @@ convert_uc(PyObject *obj, void *addr) if (!PyUnicode_Check(obj)) { PyErr_Format(PyExc_TypeError, "The fill character must be a unicode character, " - "not %T", obj); + "not %.100s", Py_TYPE(obj)->tp_name); return 0; } if (PyUnicode_READY(obj) < 0) @@ -11147,8 +11142,8 @@ PyUnicode_Contains(PyObject *str, PyObject *substr) if (!PyUnicode_Check(substr)) { PyErr_Format(PyExc_TypeError, - "'in ' requires string as left operand, not %T", - substr); + "'in ' requires string as left operand, not %.100s", + Py_TYPE(substr)->tp_name); return -1; } if (PyUnicode_READY(substr) == -1) @@ -12853,7 +12848,9 @@ unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit) if (PyUnicode_Check(sep)) return split(self, sep, maxsplit); - PyErr_Format(PyExc_TypeError, "must be str or None, not %T", sep); + PyErr_Format(PyExc_TypeError, + "must be str or None, not %.100s", + Py_TYPE(sep)->tp_name); return NULL; } @@ -13039,7 +13036,9 @@ unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit) if (PyUnicode_Check(sep)) return rsplit(self, sep, maxsplit); - PyErr_Format(PyExc_TypeError, "must be str or None, not %T", sep); + PyErr_Format(PyExc_TypeError, + "must be str or None, not %.100s", + Py_TYPE(sep)->tp_name); return NULL; } @@ -13334,8 +13333,8 @@ unicode_startswith(PyObject *self, if (!PyUnicode_Check(substring)) { PyErr_Format(PyExc_TypeError, "tuple for startswith must only contain str, " - "not %T", - substring); + "not %.100s", + Py_TYPE(substring)->tp_name); return NULL; } result = tailmatch(self, substring, start, end, -1); @@ -13351,7 +13350,7 @@ unicode_startswith(PyObject *self, if (!PyUnicode_Check(subobj)) { PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " - "a tuple of str, not %T", subobj); + "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name); return NULL; } result = tailmatch(self, subobj, start, end, -1); @@ -13388,8 +13387,8 @@ unicode_endswith(PyObject *self, if (!PyUnicode_Check(substring)) { PyErr_Format(PyExc_TypeError, "tuple for endswith must only contain str, " - "not %T", - substring); + "not %.100s", + Py_TYPE(substring)->tp_name); return NULL; } result = tailmatch(self, substring, start, end, +1); @@ -13404,7 +13403,7 @@ unicode_endswith(PyObject *self, if (!PyUnicode_Check(subobj)) { PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " - "a tuple of str, not %T", subobj); + "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name); return NULL; } result = tailmatch(self, subobj, start, end, +1); @@ -14314,13 +14313,15 @@ wrongtype: case 'x': case 'X': PyErr_Format(PyExc_TypeError, - "%%%c format: an integer is required, not %T", - type, v); + "%%%c format: an integer is required, " + "not %.200s", + type, Py_TYPE(v)->tp_name); break; default: PyErr_Format(PyExc_TypeError, - "%%%c format: a number is required, not %T", - type, v); + "%%%c format: a number is required, " + "not %.200s", + type, Py_TYPE(v)->tp_name); break; } return -1; -- cgit v0.12