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/classobject.c b/Objects/classobject.c
index e4687a3..b7711d5 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -261,11 +261,9 @@
 		result = PyUnicode_FromFormat("<unbound method %s.%s>",
 					     sklassname, sfuncname);
 	else {
-		result = PyUnicode_FromFormat("<bound method %s.%s of ",
-		                              sklassname, sfuncname);
-		/* XXX Shouldn't use repr() here! */
-		PyUnicode_AppendAndDel(&result, PyObject_Repr(self));
-		PyUnicode_AppendAndDel(&result, PyUnicode_FromString(">"));
+		/* XXX Shouldn't use repr()/%R here! */
+		result = PyUnicode_FromFormat("<bound method %s.%s of %R>",
+		                              sklassname, sfuncname, self);
 	}
 	Py_XDECREF(funcname);
 	Py_XDECREF(klassname);
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 */
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 6c9745a..ab64f79 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -431,9 +431,7 @@
 static PyObject *
 int_repr(PyIntObject *v)
 {
-	char buf[64];
-	PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
-	return PyUnicode_FromString(buf);
+	return PyUnicode_FromFormat("%ld", v->ob_ival);
 }
 
 static int
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 @@
 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 @@
 	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;
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index d526a07..a30edef 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -226,18 +226,7 @@
 static PyObject *
 slice_repr(PySliceObject *r)
 {
-	PyObject *s, *comma;
-
-	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;
+	return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
 }
 
 static PyMemberDef slice_members[] = {
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index b46093e..f2c5afa 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -484,6 +484,9 @@
 PyUnicode_FromFormatV(const char *format, va_list vargs)
 {
 	va_list count;
+	Py_ssize_t callcount = 0;
+	PyObject **callresults = NULL;
+	PyObject **callresult;
 	Py_ssize_t n = 0;
 	const char* f;
 	Py_UNICODE *s;
@@ -501,7 +504,23 @@
 	count = vargs;
 #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++) {
 		if (*f == '%') {
 			const char* p = f;
@@ -539,6 +558,19 @@
 				n += PyUnicode_GET_SIZE(obj);
 				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':
 				(void) va_arg(count, int);
 				/* maximum 64-bit pointer representation:
@@ -562,14 +594,16 @@
 			n++;
 	}
  expand:
-	/* step 2: fill the buffer */
+	/* step 4: fill the buffer */
 	/* 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);
 	if (!string)
 		return NULL;
 
 	s = PyUnicode_AS_UNICODE(string);
+	callresult = callresults;
 
 	for (f = format; *f; f++) {
 		if (*f == '%') {
@@ -649,6 +683,21 @@
 					*s++ = ucopy[upos++];
 				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':
 				sprintf(buffer, "%p", va_arg(vargs, void*));
 				/* %p is ill-defined:  ensure leading 0x. */
@@ -673,8 +722,20 @@
 	}
 
  end:
+	if (callresults)
+		PyMem_Free(callresults);
 	_PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));
 	return string;
+ fail:
+	if (callresults) {
+		PyObject **callresult2 = callresults;
+		while (callresult2 <= callresult) {
+			Py_DECREF(*callresult2);
+			++callresult2;
+		}
+		PyMem_Free(callresults);
+	}
+	return NULL;
 }
 
 #undef appendstring