Mercurial > cpython-lru_cache-weakref
diff Objects/object.c @ 41769:b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
PyString_Concat() and PyString_ConcatAndDel() (the name PyUnicode_Concat()
was already taken).
Change PyObject_Repr() to always return a unicode object.
Update all repr implementations to return unicode objects.
Add a function PyObject_ReprStr8() that calls PyObject_Repr() and converts
the result to an 8bit string.
Use PyObject_ReprStr8() where using PyObject_Repr() can't be done
straightforward.
| author | Walter Dörwald <walter@livinglogic.de> |
|---|---|
| date | Fri, 18 May 2007 17:15:44 +0000 |
| parents | ddc4be2786fd |
| children | cd125fe83051 |
line wrap: on
line diff
--- a/Objects/object.c Fri May 18 17:04:42 2007 +0000 +++ b/Objects/object.c Fri May 18 17:15:44 2007 +0000 @@ -284,7 +284,7 @@ if (flags & Py_PRINT_RAW) s = PyObject_Str(op); else - s = PyObject_Repr(op); + s = PyObject_ReprStr8(op); if (s == NULL) ret = -1; else { @@ -343,6 +343,7 @@ PyObject * PyObject_Repr(PyObject *v) { + PyObject *ress, *resu; if (PyErr_CheckSignals()) return NULL; #ifdef USE_STACKCHECK @@ -352,29 +353,48 @@ } #endif if (v == NULL) - return PyString_FromString("<NULL>"); + return PyUnicode_FromString("<NULL>"); else if (v->ob_type->tp_repr == NULL) - return PyString_FromFormat("<%s object at %p>", - v->ob_type->tp_name, v); + return PyUnicode_FromFormat("<%s object at %p>", v->ob_type->tp_name, v); else { - PyObject *res; - res = (*v->ob_type->tp_repr)(v); - if (res == NULL) + ress = (*v->ob_type->tp_repr)(v); + if (!ress) return NULL; - if (PyUnicode_Check(res)) - return res; - if (!PyString_Check(res)) { + if (PyUnicode_Check(ress)) + return ress; + if (!PyString_Check(ress)) { PyErr_Format(PyExc_TypeError, "__repr__ returned non-string (type %.200s)", - res->ob_type->tp_name); - Py_DECREF(res); + ress->ob_type->tp_name); + Py_DECREF(ress); return NULL; } - return res; + resu = PyUnicode_FromObject(ress); + Py_DECREF(ress); + return resu; } } PyObject * +PyObject_ReprStr8(PyObject *v) +{ + PyObject *resu = PyObject_Repr(v); + if (resu) { + PyObject *resb = PyUnicode_AsEncodedString(resu, NULL, NULL); + Py_DECREF(resu); + if (resb) { + PyObject *ress = PyString_FromStringAndSize( + PyBytes_AS_STRING(resb), + PyBytes_GET_SIZE(resb) + ); + Py_DECREF(resb); + return ress; + } + } + return NULL; +} + +PyObject * _PyObject_Str(PyObject *v) { PyObject *res; @@ -1509,7 +1529,7 @@ static PyObject * none_repr(PyObject *op) { - return PyString_FromString("None"); + return PyUnicode_FromString("None"); } /* ARGUSED */ @@ -1551,7 +1571,7 @@ static PyObject * NotImplemented_repr(PyObject *op) { - return PyString_FromString("NotImplemented"); + return PyUnicode_FromString("NotImplemented"); } static PyTypeObject PyNotImplemented_Type = {
