diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-05-18 17:15:44 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-05-18 17:15:44 (GMT) |
commit | 1ab833082738ced53318aca05901e596d5ede683 (patch) | |
tree | 0ff2b4c1fcbab3233e012f04bce801cadfd6d7f9 /Objects/object.c | |
parent | 14176a56d3fe36388115688d0b5acae0c759c044 (diff) | |
download | cpython-1ab833082738ced53318aca05901e596d5ede683.zip cpython-1ab833082738ced53318aca05901e596d5ede683.tar.gz cpython-1ab833082738ced53318aca05901e596d5ede683.tar.bz2 |
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.
Diffstat (limited to 'Objects/object.c')
-rw-r--r-- | Objects/object.c | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/Objects/object.c b/Objects/object.c index 4b210f1..be7d501 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -284,7 +284,7 @@ internal_print(PyObject *op, FILE *fp, int flags, int nesting) 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_Dump(PyObject* op) PyObject * PyObject_Repr(PyObject *v) { + PyObject *ress, *resu; if (PyErr_CheckSignals()) return NULL; #ifdef USE_STACKCHECK @@ -352,29 +353,48 @@ PyObject_Repr(PyObject *v) } #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 @@ so there is exactly one (which is indestructible, by the way). static PyObject * none_repr(PyObject *op) { - return PyString_FromString("None"); + return PyUnicode_FromString("None"); } /* ARGUSED */ @@ -1551,7 +1571,7 @@ PyObject _Py_NoneStruct = { static PyObject * NotImplemented_repr(PyObject *op) { - return PyString_FromString("NotImplemented"); + return PyUnicode_FromString("NotImplemented"); } static PyTypeObject PyNotImplemented_Type = { |