diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-05-19 21:49:49 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-05-19 21:49:49 (GMT) |
commit | 7569dfe11d51a11bfb11002d31245b889916fb11 (patch) | |
tree | 7e4cbbb7a27a366ea0cdc8eeacc05a23f649e4cd /Objects/setobject.c | |
parent | 94b59bb14474b6fe5a272a2c60b65f8375e827b3 (diff) | |
download | cpython-7569dfe11d51a11bfb11002d31245b889916fb11.zip cpython-7569dfe11d51a11bfb11002d31245b889916fb11.tar.gz cpython-7569dfe11d51a11bfb11002d31245b889916fb11.tar.bz2 |
Add a format specifier %R to PyUnicode_FromFormat(), which embeds
the result of a call to PyObject_Repr() into the string. This makes
it possible to simplify many repr implementations.
PyUnicode_FromFormat() uses two steps to create the final string: A first
pass through the format string determines the size of the final string and
a second pass creates the string. To avoid calling PyObject_Repr() twice
for each %R specifier, PyObject_Repr() is called during the size
calculation step and the results are stored in an array (whose size is
determined at the start by counting %R specifiers).
Diffstat (limited to 'Objects/setobject.c')
-rw-r--r-- | Objects/setobject.c | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index 795efc5..8d4291b 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -612,10 +612,8 @@ set_tp_print(PySetObject *so, FILE *fp, int flags) static PyObject * set_repr(PySetObject *so) { - PyObject *keys, *result=NULL, *listrepr; - int newsize; + PyObject *keys, *result=NULL; Py_UNICODE *u; - const char *s; int status = Py_ReprEnter((PyObject*)so); if (status != 0) { @@ -633,35 +631,32 @@ set_repr(PySetObject *so) keys = PySequence_List((PyObject *)so); if (keys == NULL) goto done; - listrepr = PyObject_Repr(keys); - Py_DECREF(keys); - if (listrepr == NULL) - goto done; - 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 { + if (so->ob_type != &PySet_Type) { + result = PyUnicode_FromFormat("%s(%R)", so->ob_type->tp_name, keys); + Py_DECREF(keys); + } + else { + PyObject *listrepr = PyObject_Repr(keys); + Py_ssize_t newsize; + Py_DECREF(keys); + if (listrepr == NULL) { + Py_DECREF(keys); + goto done; + } + newsize = PyUnicode_GET_SIZE(listrepr); + result = PyUnicode_FromUnicode(NULL, newsize); + if (result) { + u = PyUnicode_AS_UNICODE(result); *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; + PyUnicode_GET_SIZE(listrepr)-2); + u += newsize-2; *u++ = '}'; } + Py_DECREF(listrepr); } - Py_DECREF(listrepr); done: Py_ReprLeave((PyObject*)so); return result; |