mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	Revert "bpo-34595: Add %T format to PyUnicode_FromFormatV() (GH-9080)" (GH-9187)
This reverts commit 886483e2b9.
			
			
This commit is contained in:
		
							parent
							
								
									acd282fd5b
								
							
						
					
					
						commit
						998b806366
					
				
					 4 changed files with 53 additions and 66 deletions
				
			
		| 
						 | 
				
			
			@ -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 <string>' requires string as left operand, not %T",
 | 
			
		||||
                     substr);
 | 
			
		||||
                     "'in <string>' 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 @@ mainformatlong(PyObject *v,
 | 
			
		|||
        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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue