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).
This commit is contained in:
Walter Dörwald 2007-05-19 21:49:49 +00:00
parent 94b59bb144
commit 7569dfe11d
12 changed files with 125 additions and 149 deletions

View file

@ -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;