Commit 7569dfe1 authored by Walter Dörwald's avatar Walter Dörwald

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).
parent 94b59bb1
...@@ -627,14 +627,7 @@ deque_repr(PyObject *deque) ...@@ -627,14 +627,7 @@ deque_repr(PyObject *deque)
return NULL; return NULL;
} }
result = PyUnicode_FromString("deque("); result = PyUnicode_FromFormat("deque(%R)", aslist);
if (result == NULL) {
Py_DECREF(aslist);
Py_ReprLeave(deque);
return NULL;
}
PyUnicode_AppendAndDel(&result, PyObject_Repr(aslist));
PyUnicode_AppendAndDel(&result, PyUnicode_FromString(")"));
Py_DECREF(aslist); Py_DECREF(aslist);
Py_ReprLeave(deque); Py_ReprLeave(deque);
return result; return result;
...@@ -1208,25 +1201,18 @@ defdict_print(defdictobject *dd, FILE *fp, int flags) ...@@ -1208,25 +1201,18 @@ defdict_print(defdictobject *dd, FILE *fp, int flags)
static PyObject * static PyObject *
defdict_repr(defdictobject *dd) defdict_repr(defdictobject *dd)
{ {
PyObject *defrepr;
PyObject *baserepr; PyObject *baserepr;
PyObject *def;
PyObject *result; PyObject *result;
baserepr = PyDict_Type.tp_repr((PyObject *)dd); baserepr = PyDict_Type.tp_repr((PyObject *)dd);
if (baserepr == NULL) if (baserepr == NULL)
return NULL; return NULL;
if (dd->default_factory == NULL) if (dd->default_factory == NULL)
defrepr = PyUnicode_FromString("None"); def = Py_None;
else else
defrepr = PyObject_Repr(dd->default_factory); def = dd->default_factory;
if (defrepr == NULL) { result = PyUnicode_FromFormat("defaultdict(%R, %U)", def, baserepr);
Py_DECREF(baserepr); Py_DECREF(baserepr);
return NULL;
}
result = PyUnicode_FromString("defaultdict(");
PyUnicode_AppendAndDel(&result, defrepr);
PyUnicode_AppendAndDel(&result, PyUnicode_FromString(", "));
PyUnicode_AppendAndDel(&result, baserepr);
PyUnicode_AppendAndDel(&result, PyUnicode_FromString(")"));
return result; return result;
} }
......
...@@ -1118,17 +1118,7 @@ element_remove(ElementObject* self, PyObject* args) ...@@ -1118,17 +1118,7 @@ element_remove(ElementObject* self, PyObject* args)
static PyObject* static PyObject*
element_repr(ElementObject* self) element_repr(ElementObject* self)
{ {
PyObject* repr; return PyUnicode_FromFormat("<Element %R at %p>", self->tag, self);
char buffer[100];
repr = PyUnicode_FromString("<Element ");
PyUnicode_AppendAndDel(&repr, PyObject_Repr(self->tag));
sprintf(buffer, " at %p>", self);
PyUnicode_AppendAndDel(&repr, PyUnicode_FromString(buffer));
return repr;
} }
static PyObject* static PyObject*
......
...@@ -809,10 +809,8 @@ PyTclObject_unicode(PyTclObject *self, void *ignored) ...@@ -809,10 +809,8 @@ PyTclObject_unicode(PyTclObject *self, void *ignored)
static PyObject * static PyObject *
PyTclObject_repr(PyTclObject *self) PyTclObject_repr(PyTclObject *self)
{ {
char buf[50]; return PyUnicode_FromFormat("<%s object at %p>",
PyOS_snprintf(buf, 50, "<%s object at %p>",
self->value->typePtr->name, self->value); self->value->typePtr->name, self->value);
return PyUnicode_FromString(buf);
} }
static int static int
......
...@@ -1567,29 +1567,23 @@ static PyObject * ...@@ -1567,29 +1567,23 @@ static PyObject *
array_repr(arrayobject *a) array_repr(arrayobject *a)
{ {
char buf[256], typecode; char buf[256], typecode;
PyObject *s, *t, *v = NULL; PyObject *s, *v = NULL;
Py_ssize_t len; Py_ssize_t len;
len = a->ob_size; len = a->ob_size;
typecode = a->ob_descr->typecode; typecode = a->ob_descr->typecode;
if (len == 0) { if (len == 0) {
PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); return PyUnicode_FromFormat("array('%c')", typecode);
return PyUnicode_FromString(buf);
} }
if (typecode == 'c') if (typecode == 'c')
v = array_tostring(a, NULL); v = array_tostring(a, NULL);
else if (typecode == 'u') else if (typecode == 'u')
v = array_tounicode(a, NULL); v = array_tounicode(a, NULL);
else else
v = array_tolist(a, NULL); v = array_tolist(a, NULL);
t = PyObject_Repr(v);
Py_XDECREF(v);
PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
s = PyUnicode_FromString(buf); Py_DECREF(v);
PyUnicode_AppendAndDel(&s, t);
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(")"));
return s; return s;
} }
......
...@@ -2402,15 +2402,9 @@ date_subtract(PyObject *left, PyObject *right) ...@@ -2402,15 +2402,9 @@ date_subtract(PyObject *left, PyObject *right)
static PyObject * static PyObject *
date_repr(PyDateTime_Date *self) date_repr(PyDateTime_Date *self)
{ {
char buffer[1028]; return PyUnicode_FromFormat("%s(%d, %d, %d)",
const char *type_name; self->ob_type->tp_name,
type_name = self->ob_type->tp_name;
PyOS_snprintf(buffer, sizeof(buffer), "%s(%d, %d, %d)",
type_name,
GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
return PyUnicode_FromString(buffer);
} }
static PyObject * static PyObject *
...@@ -3114,7 +3108,6 @@ time_tzname(PyDateTime_Time *self, PyObject *unused) { ...@@ -3114,7 +3108,6 @@ time_tzname(PyDateTime_Time *self, PyObject *unused) {
static PyObject * static PyObject *
time_repr(PyDateTime_Time *self) time_repr(PyDateTime_Time *self)
{ {
char buffer[100];
const char *type_name = self->ob_type->tp_name; const char *type_name = self->ob_type->tp_name;
int h = TIME_GET_HOUR(self); int h = TIME_GET_HOUR(self);
int m = TIME_GET_MINUTE(self); int m = TIME_GET_MINUTE(self);
...@@ -3123,15 +3116,13 @@ time_repr(PyDateTime_Time *self) ...@@ -3123,15 +3116,13 @@ time_repr(PyDateTime_Time *self)
PyObject *result = NULL; PyObject *result = NULL;
if (us) if (us)
PyOS_snprintf(buffer, sizeof(buffer), result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)",
"%s(%d, %d, %d, %d)", type_name, h, m, s, us); type_name, h, m, s, us);
else if (s) else if (s)
PyOS_snprintf(buffer, sizeof(buffer), result = PyUnicode_FromFormat("%s(%d, %d, %d)",
"%s(%d, %d, %d)", type_name, h, m, s); type_name, h, m, s);
else else
PyOS_snprintf(buffer, sizeof(buffer), result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m);
"%s(%d, %d)", type_name, h, m);
result = PyUnicode_FromString(buffer);
if (result != NULL && HASTZINFO(self)) if (result != NULL && HASTZINFO(self))
result = append_keyword_tzinfo(result, self->tzinfo); result = append_keyword_tzinfo(result, self->tzinfo);
return result; return result;
...@@ -4020,7 +4011,7 @@ datetime_repr(PyDateTime_DateTime *self) ...@@ -4020,7 +4011,7 @@ datetime_repr(PyDateTime_DateTime *self)
PyObject *baserepr; PyObject *baserepr;
if (DATE_GET_MICROSECOND(self)) { if (DATE_GET_MICROSECOND(self)) {
PyOS_snprintf(buffer, sizeof(buffer), baserepr = PyUnicode_FromFormat(
"%s(%d, %d, %d, %d, %d, %d, %d)", "%s(%d, %d, %d, %d, %d, %d, %d)",
type_name, type_name,
GET_YEAR(self), GET_MONTH(self), GET_DAY(self), GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
...@@ -4029,7 +4020,7 @@ datetime_repr(PyDateTime_DateTime *self) ...@@ -4029,7 +4020,7 @@ datetime_repr(PyDateTime_DateTime *self)
DATE_GET_MICROSECOND(self)); DATE_GET_MICROSECOND(self));
} }
else if (DATE_GET_SECOND(self)) { else if (DATE_GET_SECOND(self)) {
PyOS_snprintf(buffer, sizeof(buffer), baserepr = PyUnicode_FromFormat(
"%s(%d, %d, %d, %d, %d, %d)", "%s(%d, %d, %d, %d, %d, %d)",
type_name, type_name,
GET_YEAR(self), GET_MONTH(self), GET_DAY(self), GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
...@@ -4037,13 +4028,12 @@ datetime_repr(PyDateTime_DateTime *self) ...@@ -4037,13 +4028,12 @@ datetime_repr(PyDateTime_DateTime *self)
DATE_GET_SECOND(self)); DATE_GET_SECOND(self));
} }
else { else {
PyOS_snprintf(buffer, sizeof(buffer), baserepr = PyUnicode_FromFormat(
"%s(%d, %d, %d, %d, %d)", "%s(%d, %d, %d, %d, %d)",
type_name, type_name,
GET_YEAR(self), GET_MONTH(self), GET_DAY(self), GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
} }
baserepr = PyUnicode_FromString(buffer);
if (baserepr == NULL || ! HASTZINFO(self)) if (baserepr == NULL || ! HASTZINFO(self))
return baserepr; return baserepr;
return append_keyword_tzinfo(baserepr, self->tzinfo); return append_keyword_tzinfo(baserepr, self->tzinfo);
......
...@@ -2389,20 +2389,10 @@ repeat_next(repeatobject *ro) ...@@ -2389,20 +2389,10 @@ repeat_next(repeatobject *ro)
static PyObject * static PyObject *
repeat_repr(repeatobject *ro) repeat_repr(repeatobject *ro)
{ {
PyObject *result, *objrepr;
objrepr = PyObject_Repr(ro->element);
if (objrepr == NULL)
return NULL;
if (ro->cnt == -1) if (ro->cnt == -1)
result = PyUnicode_FromFormat("repeat(%U)", return PyUnicode_FromFormat("repeat(%R)", ro->element);
objrepr);
else else
result = PyUnicode_FromFormat("repeat(%U, %zd)", return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt);
objrepr, ro->cnt);
Py_DECREF(objrepr);
return result;
} }
static PyObject * static PyObject *
......
...@@ -261,11 +261,9 @@ method_repr(PyMethodObject *a) ...@@ -261,11 +261,9 @@ method_repr(PyMethodObject *a)
result = PyUnicode_FromFormat("<unbound method %s.%s>", result = PyUnicode_FromFormat("<unbound method %s.%s>",
sklassname, sfuncname); sklassname, sfuncname);
else { else {
result = PyUnicode_FromFormat("<bound method %s.%s of ", /* XXX Shouldn't use repr()/%R here! */
sklassname, sfuncname); result = PyUnicode_FromFormat("<bound method %s.%s of %R>",
/* XXX Shouldn't use repr() here! */ sklassname, sfuncname, self);
PyUnicode_AppendAndDel(&result, PyObject_Repr(self));
PyUnicode_AppendAndDel(&result, PyUnicode_FromString(">"));
} }
Py_XDECREF(funcname); Py_XDECREF(funcname);
Py_XDECREF(klassname); Py_XDECREF(klassname);
......
...@@ -98,27 +98,14 @@ BaseException_str(PyBaseExceptionObject *self) ...@@ -98,27 +98,14 @@ BaseException_str(PyBaseExceptionObject *self)
static PyObject * static PyObject *
BaseException_repr(PyBaseExceptionObject *self) BaseException_repr(PyBaseExceptionObject *self)
{ {
PyObject *repr_suffix;
PyObject *repr;
char *name; char *name;
char *dot; char *dot;
repr_suffix = PyObject_Repr(self->args);
if (!repr_suffix)
return NULL;
name = (char *)self->ob_type->tp_name; name = (char *)self->ob_type->tp_name;
dot = strrchr(name, '.'); dot = strrchr(name, '.');
if (dot != NULL) name = dot+1; if (dot != NULL) name = dot+1;
repr = PyUnicode_FromString(name); return PyUnicode_FromFormat("%s%R", name, self->args);
if (!repr) {
Py_DECREF(repr_suffix);
return NULL;
}
PyUnicode_AppendAndDel(&repr, repr_suffix);
return repr;
} }
/* Pickling support */ /* Pickling support */
......
...@@ -431,9 +431,7 @@ int_print(PyIntObject *v, FILE *fp, int flags) ...@@ -431,9 +431,7 @@ int_print(PyIntObject *v, FILE *fp, int flags)
static PyObject * static PyObject *
int_repr(PyIntObject *v) int_repr(PyIntObject *v)
{ {
char buf[64]; return PyUnicode_FromFormat("%ld", v->ob_ival);
PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
return PyUnicode_FromString(buf);
} }
static int static int
......
...@@ -612,10 +612,8 @@ set_tp_print(PySetObject *so, FILE *fp, int flags) ...@@ -612,10 +612,8 @@ set_tp_print(PySetObject *so, FILE *fp, int flags)
static PyObject * static PyObject *
set_repr(PySetObject *so) set_repr(PySetObject *so)
{ {
PyObject *keys, *result=NULL, *listrepr; PyObject *keys, *result=NULL;
int newsize;
Py_UNICODE *u; Py_UNICODE *u;
const char *s;
int status = Py_ReprEnter((PyObject*)so); int status = Py_ReprEnter((PyObject*)so);
if (status != 0) { if (status != 0) {
...@@ -633,35 +631,32 @@ set_repr(PySetObject *so) ...@@ -633,35 +631,32 @@ set_repr(PySetObject *so)
keys = PySequence_List((PyObject *)so); keys = PySequence_List((PyObject *)so);
if (keys == NULL) if (keys == NULL)
goto done; goto done;
listrepr = PyObject_Repr(keys);
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); Py_DECREF(keys);
if (listrepr == NULL)
goto done; goto done;
}
newsize = PyUnicode_GET_SIZE(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); result = PyUnicode_FromUnicode(NULL, newsize);
if (result) { if (result) {
u = PyUnicode_AS_UNICODE(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++ = '{'; *u++ = '{';
/* Omit the brackets from the listrepr */ /* Omit the brackets from the listrepr */
Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1, Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
PyUnicode_GET_SIZE(listrepr)-2); PyUnicode_GET_SIZE(listrepr)-2);
u += PyUnicode_GET_SIZE(listrepr)-2; u += newsize-2;
*u++ = '}'; *u++ = '}';
} }
}
Py_DECREF(listrepr); Py_DECREF(listrepr);
}
done: done:
Py_ReprLeave((PyObject*)so); Py_ReprLeave((PyObject*)so);
return result; return result;
......
...@@ -226,18 +226,7 @@ slice_dealloc(PySliceObject *r) ...@@ -226,18 +226,7 @@ slice_dealloc(PySliceObject *r)
static PyObject * static PyObject *
slice_repr(PySliceObject *r) slice_repr(PySliceObject *r)
{ {
PyObject *s, *comma; return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
s = PyUnicode_FromString("slice(");
comma = PyUnicode_FromString(", ");
PyUnicode_AppendAndDel(&s, PyObject_Repr(r->start));
PyUnicode_Append(&s, comma);
PyUnicode_AppendAndDel(&s, PyObject_Repr(r->stop));
PyUnicode_Append(&s, comma);
PyUnicode_AppendAndDel(&s, PyObject_Repr(r->step));
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(")"));
Py_DECREF(comma);
return s;
} }
static PyMemberDef slice_members[] = { static PyMemberDef slice_members[] = {
......
...@@ -484,6 +484,9 @@ PyObject * ...@@ -484,6 +484,9 @@ PyObject *
PyUnicode_FromFormatV(const char *format, va_list vargs) PyUnicode_FromFormatV(const char *format, va_list vargs)
{ {
va_list count; va_list count;
Py_ssize_t callcount = 0;
PyObject **callresults = NULL;
PyObject **callresult;
Py_ssize_t n = 0; Py_ssize_t n = 0;
const char* f; const char* f;
Py_UNICODE *s; Py_UNICODE *s;
...@@ -501,7 +504,23 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -501,7 +504,23 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
count = vargs; count = vargs;
#endif #endif
#endif #endif
/* step 1: figure out how large a buffer we need */ /* step 1: count the number of %R format specifications
* (we call PyObject_Repr() for these objects once during step 3
* and put the result in an array) */
for (f = format; *f; f++) {
if (*f == '%' && *(f+1)=='R')
++callcount;
}
/* step 2: allocate memory for the results of PyObject_Repr() calls */
if (callcount) {
callresults = PyMem_Malloc(sizeof(PyObject *)*callcount);
if (!callresults) {
PyErr_NoMemory();
return NULL;
}
callresult = callresults;
}
/* step 3: figure out how large a buffer we need */
for (f = format; *f; f++) { for (f = format; *f; f++) {
if (*f == '%') { if (*f == '%') {
const char* p = f; const char* p = f;
...@@ -539,6 +558,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -539,6 +558,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
n += PyUnicode_GET_SIZE(obj); n += PyUnicode_GET_SIZE(obj);
break; break;
} }
case 'R':
{
PyObject *obj = va_arg(count, PyObject *);
PyObject *repr;
assert(obj);
repr = PyObject_Repr(obj);
if (!repr)
goto fail;
n += PyUnicode_GET_SIZE(repr);
/* Remember the repr and switch to the next slot */
*callresult++ = repr;
break;
}
case 'p': case 'p':
(void) va_arg(count, int); (void) va_arg(count, int);
/* maximum 64-bit pointer representation: /* maximum 64-bit pointer representation:
...@@ -562,14 +594,16 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -562,14 +594,16 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
n++; n++;
} }
expand: expand:
/* step 2: fill the buffer */ /* step 4: fill the buffer */
/* Since we've analyzed how much space we need for the worst case, /* Since we've analyzed how much space we need for the worst case,
we don't have to resize the string. */ we don't have to resize the string.
There can be no errors beyond this point. */
string = PyUnicode_FromUnicode(NULL, n); string = PyUnicode_FromUnicode(NULL, n);
if (!string) if (!string)
return NULL; return NULL;
s = PyUnicode_AS_UNICODE(string); s = PyUnicode_AS_UNICODE(string);
callresult = callresults;
for (f = format; *f; f++) { for (f = format; *f; f++) {
if (*f == '%') { if (*f == '%') {
...@@ -649,6 +683,21 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -649,6 +683,21 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
*s++ = ucopy[upos++]; *s++ = ucopy[upos++];
break; break;
} }
case 'R':
{
/* unused, since we already have the result */
(void) va_arg(vargs, PyObject *);
Py_UNICODE *ucopy = PyUnicode_AS_UNICODE(*callresult);
Py_ssize_t usize = PyUnicode_GET_SIZE(*callresult);
Py_ssize_t upos;
for (upos = 0; upos<usize;)
*s++ = ucopy[upos++];
/* We're done with the repr() => forget it */
Py_DECREF(*callresult);
/* switch to next repr() result */
++callresult;
break;
}
case 'p': case 'p':
sprintf(buffer, "%p", va_arg(vargs, void*)); sprintf(buffer, "%p", va_arg(vargs, void*));
/* %p is ill-defined: ensure leading 0x. */ /* %p is ill-defined: ensure leading 0x. */
...@@ -673,8 +722,20 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -673,8 +722,20 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
} }
end: end:
if (callresults)
PyMem_Free(callresults);
_PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));
return string; return string;
fail:
if (callresults) {
PyObject **callresult2 = callresults;
while (callresult2 <= callresult) {
Py_DECREF(*callresult2);
++callresult2;
}
PyMem_Free(callresults);
}
return NULL;
} }
#undef appendstring #undef appendstring
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment