Revert "bpo-34595: Add %T format to PyUnicode_FromFormatV() (GH-9080)… · python/cpython@998b806 (original) (raw)

`@@ -768,7 +768,8 @@ ensure_unicode(PyObject *obj)

`

768

768

`{

`

769

769

`if (!PyUnicode_Check(obj)) {

`

770

770

`PyErr_Format(PyExc_TypeError,

`

771

``

`-

"must be str, not %T", obj);

`

``

771

`+

"must be str, not %.100s",

`

``

772

`+

Py_TYPE(obj)->tp_name);

`

772

773

`return -1;

`

773

774

` }

`

774

775

`return PyUnicode_READY(obj);

`

`@@ -2529,7 +2530,7 @@ unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str,

`

2529

2530

`}

`

2530

2531

``

2531

2532

`static int

`

2532

``

`-

unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str,

`

``

2533

`+

unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str,

`

2533

2534

`Py_ssize_t width, Py_ssize_t precision)

`

2534

2535

`{

`

2535

2536

`/* UTF-8 */

`

`@@ -2746,7 +2747,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,

`

2746

2747

` {

`

2747

2748

`/* UTF-8 */

`

2748

2749

`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)

`

2750

2751

`return NULL;

`

2751

2752

`break;

`

2752

2753

` }

`

`@@ -2772,7 +2773,7 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,

`

2772

2773

` }

`

2773

2774

`else {

`

2774

2775

`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)

`

2776

2777

`return NULL;

`

2777

2778

` }

`

2778

2779

`break;

`

`@@ -2826,17 +2827,6 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,

`

2826

2827

`break;

`

2827

2828

` }

`

2828

2829

``

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

``

`-

}

`

2840

2830

`case '%':

`

2841

2831

`if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)

`

2842

2832

`return NULL;

`

`@@ -3034,7 +3024,8 @@ PyUnicode_FromObject(PyObject *obj)

`

3034

3024

`return _PyUnicode_Copy(obj);

`

3035

3025

` }

`

3036

3026

`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);

`

3038

3029

`return NULL;

`

3039

3030

`}

`

3040

3031

``

`@@ -3070,8 +3061,8 @@ PyUnicode_FromEncodedObject(PyObject *obj,

`

3070

3061

`/* Retrieve a bytes buffer view through the PEP 3118 buffer interface */

`

3071

3062

`if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {

`

3072

3063

`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);

`

3075

3066

`return NULL;

`

3076

3067

` }

`

3077

3068

``

`@@ -3201,9 +3192,10 @@ PyUnicode_Decode(const char *s,

`

3201

3192

`goto onError;

`

3202

3193

`if (!PyUnicode_Check(unicode)) {

`

3203

3194

`PyErr_Format(PyExc_TypeError,

`

3204

``

`-

"'%.400s' decoder returned '%T' instead of 'str'; "

`

``

3195

`+

"'%.400s' decoder returned '%.400s' instead of 'str'; "

`

3205

3196

`"use codecs.decode() to decode to arbitrary types",

`

3206

``

`-

encoding, unicode);

`

``

3197

`+

encoding,

`

``

3198

`+

Py_TYPE(unicode)->tp_name);

`

3207

3199

`Py_DECREF(unicode);

`

3208

3200

` goto onError;

`

3209

3201

` }

`

`@@ -3263,9 +3255,10 @@ PyUnicode_AsDecodedUnicode(PyObject *unicode,

`

3263

3255

` goto onError;

`

3264

3256

`if (!PyUnicode_Check(v)) {

`

3265

3257

`PyErr_Format(PyExc_TypeError,

`

3266

``

`-

"'%.400s' decoder returned '%T' instead of 'str'; "

`

``

3258

`+

"'%.400s' decoder returned '%.400s' instead of 'str'; "

`

3267

3259

`"use codecs.decode() to decode to arbitrary types",

`

3268

``

`-

encoding, unicode);

`

``

3260

`+

encoding,

`

``

3261

`+

Py_TYPE(unicode)->tp_name);

`

3269

3262

`Py_DECREF(v);

`

3270

3263

` goto onError;

`

3271

3264

` }

`

`@@ -3496,9 +3489,10 @@ PyUnicode_AsEncodedString(PyObject *unicode,

`

3496

3489

` }

`

3497

3490

``

3498

3491

`PyErr_Format(PyExc_TypeError,

`

3499

``

`-

"'%.400s' encoder returned '%T' instead of 'bytes'; "

`

``

3492

`+

"'%.400s' encoder returned '%.400s' instead of 'bytes'; "

`

3500

3493

`"use codecs.encode() to encode to arbitrary types",

`

3501

``

`-

encoding, v);

`

``

3494

`+

encoding,

`

``

3495

`+

Py_TYPE(v)->tp_name);

`

3502

3496

`Py_DECREF(v);

`

3503

3497

`return NULL;

`

3504

3498

`}

`

`@@ -3529,9 +3523,10 @@ PyUnicode_AsEncodedUnicode(PyObject *unicode,

`

3529

3523

` goto onError;

`

3530

3524

`if (!PyUnicode_Check(v)) {

`

3531

3525

`PyErr_Format(PyExc_TypeError,

`

3532

``

`-

"'%.400s' encoder returned '%T' instead of 'str'; "

`

``

3526

`+

"'%.400s' encoder returned '%.400s' instead of 'str'; "

`

3533

3527

`"use codecs.encode() to encode to arbitrary types",

`

3534

``

`-

encoding, v);

`

``

3528

`+

encoding,

`

``

3529

`+

Py_TYPE(v)->tp_name);

`

3535

3530

`Py_DECREF(v);

`

3536

3531

` goto onError;

`

3537

3532

` }

`

`@@ -3703,11 +3698,9 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)

`

3703

3698

``

3704

3699

`if (!PyBytes_Check(path) &&

`

3705

3700

`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);

`

3711

3704

`return 0;

`

3712

3705

` }

`

3713

3706

`path_bytes = PyBytes_FromObject(path);

`

`@@ -3724,8 +3717,8 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)

`

3724

3717

` }

`

3725

3718

`else {

`

3726

3719

`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);

`

3729

3722

`Py_DECREF(path);

`

3730

3723

`return 0;

`

3731

3724

` }

`

`@@ -9893,8 +9886,9 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq

`

9893

9886

`else {

`

9894

9887

`if (!PyUnicode_Check(separator)) {

`

9895

9888

`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);

`

9898

9892

` goto onError;

`

9899

9893

` }

`

9900

9894

`if (PyUnicode_READY(separator))

`

`@@ -9925,8 +9919,9 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq

`

9925

9919

`item = items[i];

`

9926

9920

`if (!PyUnicode_Check(item)) {

`

9927

9921

`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);

`

9930

9925

` goto onError;

`

9931

9926

` }

`

9932

9927

`if (PyUnicode_READY(item) == -1)

`

`@@ -10741,7 +10736,7 @@ convert_uc(PyObject *obj, void *addr)

`

10741

10736

`if (!PyUnicode_Check(obj)) {

`

10742

10737

`PyErr_Format(PyExc_TypeError,

`

10743

10738

`"The fill character must be a unicode character, "

`

10744

``

`-

"not %T", obj);

`

``

10739

`+

"not %.100s", Py_TYPE(obj)->tp_name);

`

10745

10740

`return 0;

`

10746

10741

` }

`

10747

10742

`if (PyUnicode_READY(obj) < 0)

`

`@@ -11147,8 +11142,8 @@ PyUnicode_Contains(PyObject *str, PyObject *substr)

`

11147

11142

``

11148

11143

`if (!PyUnicode_Check(substr)) {

`

11149

11144

`PyErr_Format(PyExc_TypeError,

`

11150

``

`-

"'in ' requires string as left operand, not %T",

`

11151

``

`-

substr);

`

``

11145

`+

"'in ' requires string as left operand, not %.100s",

`

``

11146

`+

Py_TYPE(substr)->tp_name);

`

11152

11147

`return -1;

`

11153

11148

` }

`

11154

11149

`if (PyUnicode_READY(substr) == -1)

`

`@@ -12853,7 +12848,9 @@ unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)

`

12853

12848

`if (PyUnicode_Check(sep))

`

12854

12849

`return split(self, sep, maxsplit);

`

12855

12850

``

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);

`

12857

12854

`return NULL;

`

12858

12855

`}

`

12859

12856

``

`@@ -13039,7 +13036,9 @@ unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit)

`

13039

13036

`if (PyUnicode_Check(sep))

`

13040

13037

`return rsplit(self, sep, maxsplit);

`

13041

13038

``

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);

`

13043

13042

`return NULL;

`

13044

13043

`}

`

13045

13044

``

`@@ -13334,8 +13333,8 @@ unicode_startswith(PyObject *self,

`

13334

13333

`if (!PyUnicode_Check(substring)) {

`

13335

13334

`PyErr_Format(PyExc_TypeError,

`

13336

13335

`"tuple for startswith must only contain str, "

`

13337

``

`-

"not %T",

`

13338

``

`-

substring);

`

``

13336

`+

"not %.100s",

`

``

13337

`+

Py_TYPE(substring)->tp_name);

`

13339

13338

`return NULL;

`

13340

13339

` }

`

13341

13340

`result = tailmatch(self, substring, start, end, -1);

`

`@@ -13351,7 +13350,7 @@ unicode_startswith(PyObject *self,

`

13351

13350

`if (!PyUnicode_Check(subobj)) {

`

13352

13351

`PyErr_Format(PyExc_TypeError,

`

13353

13352

`"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);

`

13355

13354

`return NULL;

`

13356

13355

` }

`

13357

13356

`result = tailmatch(self, subobj, start, end, -1);

`

`@@ -13388,8 +13387,8 @@ unicode_endswith(PyObject *self,

`

13388

13387

`if (!PyUnicode_Check(substring)) {

`

13389

13388

`PyErr_Format(PyExc_TypeError,

`

13390

13389

`"tuple for endswith must only contain str, "

`

13391

``

`-

"not %T",

`

13392

``

`-

substring);

`

``

13390

`+

"not %.100s",

`

``

13391

`+

Py_TYPE(substring)->tp_name);

`

13393

13392

`return NULL;

`

13394

13393

` }

`

13395

13394

`result = tailmatch(self, substring, start, end, +1);

`

`@@ -13404,7 +13403,7 @@ unicode_endswith(PyObject *self,

`

13404

13403

`if (!PyUnicode_Check(subobj)) {

`

13405

13404

`PyErr_Format(PyExc_TypeError,

`

13406

13405

`"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);

`

13408

13407

`return NULL;

`

13409

13408

` }

`

13410

13409

`result = tailmatch(self, subobj, start, end, +1);

`

`@@ -14314,13 +14313,15 @@ mainformatlong(PyObject *v,

`

14314

14313

`case 'x':

`

14315

14314

`case 'X':

`

14316

14315

`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);

`

14319

14319

`break;

`

14320

14320

`default:

`

14321

14321

`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);

`

14324

14325

`break;

`

14325

14326

` }

`

14326

14327

`return -1;

`