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).
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index f4c265a..f7189e2 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -98,27 +98,14 @@
static PyObject *
BaseException_repr(PyBaseExceptionObject *self)
{
- PyObject *repr_suffix;
- PyObject *repr;
char *name;
char *dot;
- repr_suffix = PyObject_Repr(self->args);
- if (!repr_suffix)
- return NULL;
-
name = (char *)self->ob_type->tp_name;
dot = strrchr(name, '.');
if (dot != NULL) name = dot+1;
- repr = PyUnicode_FromString(name);
- if (!repr) {
- Py_DECREF(repr_suffix);
- return NULL;
- }
-
- PyUnicode_AppendAndDel(&repr, repr_suffix);
- return repr;
+ return PyUnicode_FromFormat("%s%R", name, self->args);
}
/* Pickling support */