Skip to content

Commit 998b806

Browse files
authored
Revert "bpo-34595: Add %T format to PyUnicode_FromFormatV() (GH-9080)" (GH-9187)
This reverts commit 886483e.
1 parent acd282f commit 998b806

File tree

4 files changed

+53
-66
lines changed

4 files changed

+53
-66
lines changed

Doc/c-api/unicode.rst

-6
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,6 @@ APIs:
519519
| :attr:`%R` | PyObject\* | The result of calling |
520520
| | | :c:func:`PyObject_Repr`. |
521521
+-------------------+---------------------+--------------------------------+
522-
| :attr:`%T` | PyObject\* | Object type name, equivalent |
523-
| | | to ``Py_TYPE(op)->tp_name``. |
524-
+-------------------+---------------------+--------------------------------+
525522
526523
An unrecognized format character causes all the rest of the format string to be
527524
copied as-is to the result string, and any extra arguments discarded.
@@ -546,9 +543,6 @@ APIs:
546543
Support width and precision formatter for ``"%s"``, ``"%A"``, ``"%U"``,
547544
``"%V"``, ``"%S"``, ``"%R"`` added.
548545
549-
.. versionchanged:: 3.7
550-
Support for ``"%T"`` (object type name) added.
551-
552546
553547
.. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
554548

Lib/test/test_unicode.py

-4
Original file line numberDiff line numberDiff line change
@@ -2655,10 +2655,6 @@ def check_format(expected, format, *args):
26552655
check_format(r"%A:'abc\xe9\uabcd\U0010ffff'",
26562656
b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
26572657

2658-
# test %T (object type name)
2659-
check_format(r"type name: str",
2660-
b'type name: %T', 'text')
2661-
26622658
# test %V
26632659
check_format('repr=abc',
26642660
b'repr=%V', 'abc', b'xyz')

Misc/NEWS.d/next/C API/2018-09-06-11-17-49.bpo-34595.Hkz62y.rst

-4
This file was deleted.

Objects/unicodeobject.c

+53-52
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ ensure_unicode(PyObject *obj)
768768
{
769769
if (!PyUnicode_Check(obj)) {
770770
PyErr_Format(PyExc_TypeError,
771-
"must be str, not %T", obj);
771+
"must be str, not %.100s",
772+
Py_TYPE(obj)->tp_name);
772773
return -1;
773774
}
774775
return PyUnicode_READY(obj);
@@ -2529,7 +2530,7 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
25292530
}
25302531

25312532
static int
2532-
unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str,
2533+
unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str,
25332534
Py_ssize_t width, Py_ssize_t precision)
25342535
{
25352536
/* UTF-8 */
@@ -2746,7 +2747,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
27462747
{
27472748
/* UTF-8 */
27482749
const char *s = va_arg(*vargs, const char*);
2749-
if (unicode_fromformat_write_utf8(writer, s, width, precision) < 0)
2750+
if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0)
27502751
return NULL;
27512752
break;
27522753
}
@@ -2772,7 +2773,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
27722773
}
27732774
else {
27742775
assert(str != NULL);
2775-
if (unicode_fromformat_write_utf8(writer, str, width, precision) < 0)
2776+
if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0)
27762777
return NULL;
27772778
}
27782779
break;
@@ -2826,17 +2827,6 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
28262827
break;
28272828
}
28282829

2829-
case 'T':
2830-
{
2831-
/* Object type name (tp_name) */
2832-
PyObject *obj = va_arg(*vargs, PyObject *);
2833-
PyTypeObject *type = Py_TYPE(obj);
2834-
const char *type_name = type->tp_name;
2835-
if (unicode_fromformat_write_utf8(writer, type_name, -1, -1) < 0) {
2836-
return NULL;
2837-
}
2838-
break;
2839-
}
28402830
case '%':
28412831
if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)
28422832
return NULL;
@@ -3034,7 +3024,8 @@ PyUnicode_FromObject(PyObject *obj)
30343024
return _PyUnicode_Copy(obj);
30353025
}
30363026
PyErr_Format(PyExc_TypeError,
3037-
"Can't convert '%T' object to str implicitly", obj);
3027+
"Can't convert '%.100s' object to str implicitly",
3028+
Py_TYPE(obj)->tp_name);
30383029
return NULL;
30393030
}
30403031

@@ -3070,8 +3061,8 @@ PyUnicode_FromEncodedObject(PyObject *obj,
30703061
/* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
30713062
if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
30723063
PyErr_Format(PyExc_TypeError,
3073-
"decoding to str: need a bytes-like object, %T found",
3074-
obj);
3064+
"decoding to str: need a bytes-like object, %.80s found",
3065+
Py_TYPE(obj)->tp_name);
30753066
return NULL;
30763067
}
30773068

@@ -3201,9 +3192,10 @@ PyUnicode_Decode(const char *s,
32013192
goto onError;
32023193
if (!PyUnicode_Check(unicode)) {
32033194
PyErr_Format(PyExc_TypeError,
3204-
"'%.400s' decoder returned '%T' instead of 'str'; "
3195+
"'%.400s' decoder returned '%.400s' instead of 'str'; "
32053196
"use codecs.decode() to decode to arbitrary types",
3206-
encoding, unicode);
3197+
encoding,
3198+
Py_TYPE(unicode)->tp_name);
32073199
Py_DECREF(unicode);
32083200
goto onError;
32093201
}
@@ -3263,9 +3255,10 @@ PyUnicode_AsDecodedUnicode(PyObject *unicode,
32633255
goto onError;
32643256
if (!PyUnicode_Check(v)) {
32653257
PyErr_Format(PyExc_TypeError,
3266-
"'%.400s' decoder returned '%T' instead of 'str'; "
3258+
"'%.400s' decoder returned '%.400s' instead of 'str'; "
32673259
"use codecs.decode() to decode to arbitrary types",
3268-
encoding, unicode);
3260+
encoding,
3261+
Py_TYPE(unicode)->tp_name);
32693262
Py_DECREF(v);
32703263
goto onError;
32713264
}
@@ -3496,9 +3489,10 @@ PyUnicode_AsEncodedString(PyObject *unicode,
34963489
}
34973490

34983491
PyErr_Format(PyExc_TypeError,
3499-
"'%.400s' encoder returned '%T' instead of 'bytes'; "
3492+
"'%.400s' encoder returned '%.400s' instead of 'bytes'; "
35003493
"use codecs.encode() to encode to arbitrary types",
3501-
encoding, v);
3494+
encoding,
3495+
Py_TYPE(v)->tp_name);
35023496
Py_DECREF(v);
35033497
return NULL;
35043498
}
@@ -3529,9 +3523,10 @@ PyUnicode_AsEncodedUnicode(PyObject *unicode,
35293523
goto onError;
35303524
if (!PyUnicode_Check(v)) {
35313525
PyErr_Format(PyExc_TypeError,
3532-
"'%.400s' encoder returned '%T' instead of 'str'; "
3526+
"'%.400s' encoder returned '%.400s' instead of 'str'; "
35333527
"use codecs.encode() to encode to arbitrary types",
3534-
encoding, v);
3528+
encoding,
3529+
Py_TYPE(v)->tp_name);
35353530
Py_DECREF(v);
35363531
goto onError;
35373532
}
@@ -3703,11 +3698,9 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
37033698

37043699
if (!PyBytes_Check(path) &&
37053700
PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
3706-
"path should be string, bytes, "
3707-
"or os.PathLike, not %T",
3708-
arg))
3709-
{
3710-
Py_DECREF(path);
3701+
"path should be string, bytes, or os.PathLike, not %.200s",
3702+
Py_TYPE(arg)->tp_name)) {
3703+
Py_DECREF(path);
37113704
return 0;
37123705
}
37133706
path_bytes = PyBytes_FromObject(path);
@@ -3724,8 +3717,8 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
37243717
}
37253718
else {
37263719
PyErr_Format(PyExc_TypeError,
3727-
"path should be string, bytes, or os.PathLike, not %T",
3728-
arg);
3720+
"path should be string, bytes, or os.PathLike, not %.200s",
3721+
Py_TYPE(arg)->tp_name);
37293722
Py_DECREF(path);
37303723
return 0;
37313724
}
@@ -9893,8 +9886,9 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
98939886
else {
98949887
if (!PyUnicode_Check(separator)) {
98959888
PyErr_Format(PyExc_TypeError,
9896-
"separator: expected str instance, %T found",
9897-
separator);
9889+
"separator: expected str instance,"
9890+
" %.80s found",
9891+
Py_TYPE(separator)->tp_name);
98989892
goto onError;
98999893
}
99009894
if (PyUnicode_READY(separator))
@@ -9925,8 +9919,9 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
99259919
item = items[i];
99269920
if (!PyUnicode_Check(item)) {
99279921
PyErr_Format(PyExc_TypeError,
9928-
"sequence item %zd: expected str instance, %T found",
9929-
i, item);
9922+
"sequence item %zd: expected str instance,"
9923+
" %.80s found",
9924+
i, Py_TYPE(item)->tp_name);
99309925
goto onError;
99319926
}
99329927
if (PyUnicode_READY(item) == -1)
@@ -10741,7 +10736,7 @@ convert_uc(PyObject *obj, void *addr)
1074110736
if (!PyUnicode_Check(obj)) {
1074210737
PyErr_Format(PyExc_TypeError,
1074310738
"The fill character must be a unicode character, "
10744-
"not %T", obj);
10739+
"not %.100s", Py_TYPE(obj)->tp_name);
1074510740
return 0;
1074610741
}
1074710742
if (PyUnicode_READY(obj) < 0)
@@ -11147,8 +11142,8 @@ PyUnicode_Contains(PyObject *str, PyObject *substr)
1114711142

1114811143
if (!PyUnicode_Check(substr)) {
1114911144
PyErr_Format(PyExc_TypeError,
11150-
"'in <string>' requires string as left operand, not %T",
11151-
substr);
11145+
"'in <string>' requires string as left operand, not %.100s",
11146+
Py_TYPE(substr)->tp_name);
1115211147
return -1;
1115311148
}
1115411149
if (PyUnicode_READY(substr) == -1)
@@ -12853,7 +12848,9 @@ unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)
1285312848
if (PyUnicode_Check(sep))
1285412849
return split(self, sep, maxsplit);
1285512850

12856-
PyErr_Format(PyExc_TypeError, "must be str or None, not %T", sep);
12851+
PyErr_Format(PyExc_TypeError,
12852+
"must be str or None, not %.100s",
12853+
Py_TYPE(sep)->tp_name);
1285712854
return NULL;
1285812855
}
1285912856

@@ -13039,7 +13036,9 @@ unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)
1303913036
if (PyUnicode_Check(sep))
1304013037
return rsplit(self, sep, maxsplit);
1304113038

13042-
PyErr_Format(PyExc_TypeError, "must be str or None, not %T", sep);
13039+
PyErr_Format(PyExc_TypeError,
13040+
"must be str or None, not %.100s",
13041+
Py_TYPE(sep)->tp_name);
1304313042
return NULL;
1304413043
}
1304513044

@@ -13334,8 +13333,8 @@ unicode_startswith(PyObject *self,
1333413333
if (!PyUnicode_Check(substring)) {
1333513334
PyErr_Format(PyExc_TypeError,
1333613335
"tuple for startswith must only contain str, "
13337-
"not %T",
13338-
substring);
13336+
"not %.100s",
13337+
Py_TYPE(substring)->tp_name);
1333913338
return NULL;
1334013339
}
1334113340
result = tailmatch(self, substring, start, end, -1);
@@ -13351,7 +13350,7 @@ unicode_startswith(PyObject *self,
1335113350
if (!PyUnicode_Check(subobj)) {
1335213351
PyErr_Format(PyExc_TypeError,
1335313352
"startswith first arg must be str or "
13354-
"a tuple of str, not %T", subobj);
13353+
"a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
1335513354
return NULL;
1335613355
}
1335713356
result = tailmatch(self, subobj, start, end, -1);
@@ -13388,8 +13387,8 @@ unicode_endswith(PyObject *self,
1338813387
if (!PyUnicode_Check(substring)) {
1338913388
PyErr_Format(PyExc_TypeError,
1339013389
"tuple for endswith must only contain str, "
13391-
"not %T",
13392-
substring);
13390+
"not %.100s",
13391+
Py_TYPE(substring)->tp_name);
1339313392
return NULL;
1339413393
}
1339513394
result = tailmatch(self, substring, start, end, +1);
@@ -13404,7 +13403,7 @@ unicode_endswith(PyObject *self,
1340413403
if (!PyUnicode_Check(subobj)) {
1340513404
PyErr_Format(PyExc_TypeError,
1340613405
"endswith first arg must be str or "
13407-
"a tuple of str, not %T", subobj);
13406+
"a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
1340813407
return NULL;
1340913408
}
1341013409
result = tailmatch(self, subobj, start, end, +1);
@@ -14314,13 +14313,15 @@ mainformatlong(PyObject *v,
1431414313
case 'x':
1431514314
case 'X':
1431614315
PyErr_Format(PyExc_TypeError,
14317-
"%%%c format: an integer is required, not %T",
14318-
type, v);
14316+
"%%%c format: an integer is required, "
14317+
"not %.200s",
14318+
type, Py_TYPE(v)->tp_name);
1431914319
break;
1432014320
default:
1432114321
PyErr_Format(PyExc_TypeError,
14322-
"%%%c format: a number is required, not %T",
14323-
type, v);
14322+
"%%%c format: a number is required, "
14323+
"not %.200s",
14324+
type, Py_TYPE(v)->tp_name);
1432414325
break;
1432514326
}
1432614327
return -1;

0 commit comments

Comments
 (0)