bpo-34595: Add %T format to PyUnicode_FromFormatV() (GH-9080) · python/cpython@886483e (original) (raw)
`@@ -768,8 +768,7 @@ ensure_unicode(PyObject *obj)
`
768
768
`{
`
769
769
`if (!PyUnicode_Check(obj)) {
`
770
770
`PyErr_Format(PyExc_TypeError,
`
771
``
`-
"must be str, not %.100s",
`
772
``
`-
Py_TYPE(obj)->tp_name);
`
``
771
`+
"must be str, not %T", obj);
`
773
772
`return -1;
`
774
773
` }
`
775
774
`return PyUnicode_READY(obj);
`
`@@ -2530,7 +2529,7 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,
`
2530
2529
`}
`
2531
2530
``
2532
2531
`static int
`
2533
``
`-
unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str,
`
``
2532
`+
unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str,
`
2534
2533
`Py_ssize_t width, Py_ssize_t precision)
`
2535
2534
`{
`
2536
2535
`/* UTF-8 */
`
`@@ -2747,7 +2746,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
`
2747
2746
` {
`
2748
2747
`/* UTF-8 */
`
2749
2748
`const char *s = va_arg(vargs, const char);
`
2750
``
`-
if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0)
`
``
2749
`+
if (unicode_fromformat_write_utf8(writer, s, width, precision) < 0)
`
2751
2750
`return NULL;
`
2752
2751
`break;
`
2753
2752
` }
`
`@@ -2773,7 +2772,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
`
2773
2772
` }
`
2774
2773
`else {
`
2775
2774
`assert(str != NULL);
`
2776
``
`-
if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0)
`
``
2775
`+
if (unicode_fromformat_write_utf8(writer, str, width, precision) < 0)
`
2777
2776
`return NULL;
`
2778
2777
` }
`
2779
2778
`break;
`
`@@ -2827,6 +2826,17 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
`
2827
2826
`break;
`
2828
2827
` }
`
2829
2828
``
``
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
`+
}
`
2830
2840
`case '%':
`
2831
2841
`if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)
`
2832
2842
`return NULL;
`
`@@ -3024,8 +3034,7 @@ PyUnicode_FromObject(PyObject *obj)
`
3024
3034
`return _PyUnicode_Copy(obj);
`
3025
3035
` }
`
3026
3036
`PyErr_Format(PyExc_TypeError,
`
3027
``
`-
"Can't convert '%.100s' object to str implicitly",
`
3028
``
`-
Py_TYPE(obj)->tp_name);
`
``
3037
`+
"Can't convert '%T' object to str implicitly", obj);
`
3029
3038
`return NULL;
`
3030
3039
`}
`
3031
3040
``
`@@ -3061,8 +3070,8 @@ PyUnicode_FromEncodedObject(PyObject *obj,
`
3061
3070
`/* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
`
3062
3071
`if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
`
3063
3072
`PyErr_Format(PyExc_TypeError,
`
3064
``
`-
"decoding to str: need a bytes-like object, %.80s found",
`
3065
``
`-
Py_TYPE(obj)->tp_name);
`
``
3073
`+
"decoding to str: need a bytes-like object, %T found",
`
``
3074
`+
obj);
`
3066
3075
`return NULL;
`
3067
3076
` }
`
3068
3077
``
`@@ -3192,10 +3201,9 @@ PyUnicode_Decode(const char *s,
`
3192
3201
`goto onError;
`
3193
3202
`if (!PyUnicode_Check(unicode)) {
`
3194
3203
`PyErr_Format(PyExc_TypeError,
`
3195
``
`-
"'%.400s' decoder returned '%.400s' instead of 'str'; "
`
``
3204
`+
"'%.400s' decoder returned '%T' instead of 'str'; "
`
3196
3205
`"use codecs.decode() to decode to arbitrary types",
`
3197
``
`-
encoding,
`
3198
``
`-
Py_TYPE(unicode)->tp_name);
`
``
3206
`+
encoding, unicode);
`
3199
3207
`Py_DECREF(unicode);
`
3200
3208
` goto onError;
`
3201
3209
` }
`
`@@ -3255,10 +3263,9 @@ PyUnicode_AsDecodedUnicode(PyObject *unicode,
`
3255
3263
` goto onError;
`
3256
3264
`if (!PyUnicode_Check(v)) {
`
3257
3265
`PyErr_Format(PyExc_TypeError,
`
3258
``
`-
"'%.400s' decoder returned '%.400s' instead of 'str'; "
`
``
3266
`+
"'%.400s' decoder returned '%T' instead of 'str'; "
`
3259
3267
`"use codecs.decode() to decode to arbitrary types",
`
3260
``
`-
encoding,
`
3261
``
`-
Py_TYPE(unicode)->tp_name);
`
``
3268
`+
encoding, unicode);
`
3262
3269
`Py_DECREF(v);
`
3263
3270
` goto onError;
`
3264
3271
` }
`
`@@ -3489,10 +3496,9 @@ PyUnicode_AsEncodedString(PyObject *unicode,
`
3489
3496
` }
`
3490
3497
``
3491
3498
`PyErr_Format(PyExc_TypeError,
`
3492
``
`-
"'%.400s' encoder returned '%.400s' instead of 'bytes'; "
`
``
3499
`+
"'%.400s' encoder returned '%T' instead of 'bytes'; "
`
3493
3500
`"use codecs.encode() to encode to arbitrary types",
`
3494
``
`-
encoding,
`
3495
``
`-
Py_TYPE(v)->tp_name);
`
``
3501
`+
encoding, v);
`
3496
3502
`Py_DECREF(v);
`
3497
3503
`return NULL;
`
3498
3504
`}
`
`@@ -3523,10 +3529,9 @@ PyUnicode_AsEncodedUnicode(PyObject *unicode,
`
3523
3529
` goto onError;
`
3524
3530
`if (!PyUnicode_Check(v)) {
`
3525
3531
`PyErr_Format(PyExc_TypeError,
`
3526
``
`-
"'%.400s' encoder returned '%.400s' instead of 'str'; "
`
``
3532
`+
"'%.400s' encoder returned '%T' instead of 'str'; "
`
3527
3533
`"use codecs.encode() to encode to arbitrary types",
`
3528
``
`-
encoding,
`
3529
``
`-
Py_TYPE(v)->tp_name);
`
``
3534
`+
encoding, v);
`
3530
3535
`Py_DECREF(v);
`
3531
3536
` goto onError;
`
3532
3537
` }
`
`@@ -3698,9 +3703,11 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
`
3698
3703
``
3699
3704
`if (!PyBytes_Check(path) &&
`
3700
3705
`PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
`
3701
``
`-
"path should be string, bytes, or os.PathLike, not %.200s",
`
3702
``
`-
Py_TYPE(arg)->tp_name)) {
`
3703
``
`-
Py_DECREF(path);
`
``
3706
`+
"path should be string, bytes, "
`
``
3707
`+
"or os.PathLike, not %T",
`
``
3708
`+
arg))
`
``
3709
`+
{
`
``
3710
`+
Py_DECREF(path);
`
3704
3711
`return 0;
`
3705
3712
` }
`
3706
3713
`path_bytes = PyBytes_FromObject(path);
`
`@@ -3717,8 +3724,8 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
`
3717
3724
` }
`
3718
3725
`else {
`
3719
3726
`PyErr_Format(PyExc_TypeError,
`
3720
``
`-
"path should be string, bytes, or os.PathLike, not %.200s",
`
3721
``
`-
Py_TYPE(arg)->tp_name);
`
``
3727
`+
"path should be string, bytes, or os.PathLike, not %T",
`
``
3728
`+
arg);
`
3722
3729
`Py_DECREF(path);
`
3723
3730
`return 0;
`
3724
3731
` }
`
`@@ -9886,9 +9893,8 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
`
9886
9893
`else {
`
9887
9894
`if (!PyUnicode_Check(separator)) {
`
9888
9895
`PyErr_Format(PyExc_TypeError,
`
9889
``
`-
"separator: expected str instance,"
`
9890
``
`-
" %.80s found",
`
9891
``
`-
Py_TYPE(separator)->tp_name);
`
``
9896
`+
"separator: expected str instance, %T found",
`
``
9897
`+
separator);
`
9892
9898
` goto onError;
`
9893
9899
` }
`
9894
9900
`if (PyUnicode_READY(separator))
`
`@@ -9919,9 +9925,8 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
`
9919
9925
`item = items[i];
`
9920
9926
`if (!PyUnicode_Check(item)) {
`
9921
9927
`PyErr_Format(PyExc_TypeError,
`
9922
``
`-
"sequence item %zd: expected str instance,"
`
9923
``
`-
" %.80s found",
`
9924
``
`-
i, Py_TYPE(item)->tp_name);
`
``
9928
`+
"sequence item %zd: expected str instance, %T found",
`
``
9929
`+
i, item);
`
9925
9930
` goto onError;
`
9926
9931
` }
`
9927
9932
`if (PyUnicode_READY(item) == -1)
`
`@@ -10736,7 +10741,7 @@ convert_uc(PyObject *obj, void *addr)
`
10736
10741
`if (!PyUnicode_Check(obj)) {
`
10737
10742
`PyErr_Format(PyExc_TypeError,
`
10738
10743
`"The fill character must be a unicode character, "
`
10739
``
`-
"not %.100s", Py_TYPE(obj)->tp_name);
`
``
10744
`+
"not %T", obj);
`
10740
10745
`return 0;
`
10741
10746
` }
`
10742
10747
`if (PyUnicode_READY(obj) < 0)
`
`@@ -11142,8 +11147,8 @@ PyUnicode_Contains(PyObject *str, PyObject *substr)
`
11142
11147
``
11143
11148
`if (!PyUnicode_Check(substr)) {
`
11144
11149
`PyErr_Format(PyExc_TypeError,
`
11145
``
`-
"'in ' requires string as left operand, not %.100s",
`
11146
``
`-
Py_TYPE(substr)->tp_name);
`
``
11150
`+
"'in ' requires string as left operand, not %T",
`
``
11151
`+
substr);
`
11147
11152
`return -1;
`
11148
11153
` }
`
11149
11154
`if (PyUnicode_READY(substr) == -1)
`
`@@ -12848,9 +12853,7 @@ unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)
`
12848
12853
`if (PyUnicode_Check(sep))
`
12849
12854
`return split(self, sep, maxsplit);
`
12850
12855
``
12851
``
`-
PyErr_Format(PyExc_TypeError,
`
12852
``
`-
"must be str or None, not %.100s",
`
12853
``
`-
Py_TYPE(sep)->tp_name);
`
``
12856
`+
PyErr_Format(PyExc_TypeError, "must be str or None, not %T", sep);
`
12854
12857
`return NULL;
`
12855
12858
`}
`
12856
12859
``
`@@ -13036,9 +13039,7 @@ unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)
`
13036
13039
`if (PyUnicode_Check(sep))
`
13037
13040
`return rsplit(self, sep, maxsplit);
`
13038
13041
``
13039
``
`-
PyErr_Format(PyExc_TypeError,
`
13040
``
`-
"must be str or None, not %.100s",
`
13041
``
`-
Py_TYPE(sep)->tp_name);
`
``
13042
`+
PyErr_Format(PyExc_TypeError, "must be str or None, not %T", sep);
`
13042
13043
`return NULL;
`
13043
13044
`}
`
13044
13045
``
`@@ -13333,8 +13334,8 @@ unicode_startswith(PyObject *self,
`
13333
13334
`if (!PyUnicode_Check(substring)) {
`
13334
13335
`PyErr_Format(PyExc_TypeError,
`
13335
13336
`"tuple for startswith must only contain str, "
`
13336
``
`-
"not %.100s",
`
13337
``
`-
Py_TYPE(substring)->tp_name);
`
``
13337
`+
"not %T",
`
``
13338
`+
substring);
`
13338
13339
`return NULL;
`
13339
13340
` }
`
13340
13341
`result = tailmatch(self, substring, start, end, -1);
`
`@@ -13350,7 +13351,7 @@ unicode_startswith(PyObject *self,
`
13350
13351
`if (!PyUnicode_Check(subobj)) {
`
13351
13352
`PyErr_Format(PyExc_TypeError,
`
13352
13353
`"startswith first arg must be str or "
`
13353
``
`-
"a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
`
``
13354
`+
"a tuple of str, not %T", subobj);
`
13354
13355
`return NULL;
`
13355
13356
` }
`
13356
13357
`result = tailmatch(self, subobj, start, end, -1);
`
`@@ -13387,8 +13388,8 @@ unicode_endswith(PyObject *self,
`
13387
13388
`if (!PyUnicode_Check(substring)) {
`
13388
13389
`PyErr_Format(PyExc_TypeError,
`
13389
13390
`"tuple for endswith must only contain str, "
`
13390
``
`-
"not %.100s",
`
13391
``
`-
Py_TYPE(substring)->tp_name);
`
``
13391
`+
"not %T",
`
``
13392
`+
substring);
`
13392
13393
`return NULL;
`
13393
13394
` }
`
13394
13395
`result = tailmatch(self, substring, start, end, +1);
`
`@@ -13403,7 +13404,7 @@ unicode_endswith(PyObject *self,
`
13403
13404
`if (!PyUnicode_Check(subobj)) {
`
13404
13405
`PyErr_Format(PyExc_TypeError,
`
13405
13406
`"endswith first arg must be str or "
`
13406
``
`-
"a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
`
``
13407
`+
"a tuple of str, not %T", subobj);
`
13407
13408
`return NULL;
`
13408
13409
` }
`
13409
13410
`result = tailmatch(self, subobj, start, end, +1);
`
`@@ -14313,15 +14314,13 @@ mainformatlong(PyObject *v,
`
14313
14314
`case 'x':
`
14314
14315
`case 'X':
`
14315
14316
`PyErr_Format(PyExc_TypeError,
`
14316
``
`-
"%%%c format: an integer is required, "
`
14317
``
`-
"not %.200s",
`
14318
``
`-
type, Py_TYPE(v)->tp_name);
`
``
14317
`+
"%%%c format: an integer is required, not %T",
`
``
14318
`+
type, v);
`
14319
14319
`break;
`
14320
14320
`default:
`
14321
14321
`PyErr_Format(PyExc_TypeError,
`
14322
``
`-
"%%%c format: a number is required, "
`
14323
``
`-
"not %.200s",
`
14324
``
`-
type, Py_TYPE(v)->tp_name);
`
``
14322
`+
"%%%c format: a number is required, not %T",
`
``
14323
`+
type, v);
`
14325
14324
`break;
`
14326
14325
` }
`
14327
14326
`return -1;
`