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/setobject.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/setobject.c')
-rw-r--r-- | Objects/setobject.c | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index 91f44d5..795efc5 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -613,18 +613,21 @@ static PyObject * set_repr(PySetObject *so) { PyObject *keys, *result=NULL, *listrepr; + int newsize; + Py_UNICODE *u; + const char *s; int status = Py_ReprEnter((PyObject*)so); if (status != 0) { if (status < 0) return NULL; - return PyString_FromFormat("%s(...)", so->ob_type->tp_name); + return PyUnicode_FromFormat("%s(...)", so->ob_type->tp_name); } /* shortcut for the empty set */ if (!so->used) { Py_ReprLeave((PyObject*)so); - return PyString_FromFormat("%s()", so->ob_type->tp_name); + return PyUnicode_FromFormat("%s()", so->ob_type->tp_name); } keys = PySequence_List((PyObject *)so); @@ -635,14 +638,28 @@ set_repr(PySetObject *so) if (listrepr == NULL) goto done; - if (so->ob_type == &PySet_Type) { - char *s = PyString_AS_STRING(listrepr); - s += 1; - s[strlen(s)-1] = 0; - result = PyString_FromFormat("{%s}", s); - } else { - result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, - PyString_AS_STRING(listrepr)); + newsize = PyUnicode_GET_SIZE(listrepr); + if (so->ob_type != &PySet_Type) + newsize += strlen(so->ob_type->tp_name)+2; + result = PyUnicode_FromUnicode(NULL, newsize); + if (result) { + u = PyUnicode_AS_UNICODE(result); + if (so->ob_type != &PySet_Type) { + for (s = so->ob_type->tp_name; *s;) + *u++ = *s++; + *u++ = '('; + Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr), + PyUnicode_GET_SIZE(listrepr)); + u += PyUnicode_GET_SIZE(listrepr); + *u++ = ')'; + } else { + *u++ = '{'; + /* Omit the brackets from the listrepr */ + Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1, + PyUnicode_GET_SIZE(listrepr)-2); + u += PyUnicode_GET_SIZE(listrepr)-2; + *u++ = '}'; + } } Py_DECREF(listrepr); done: |