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/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 1311d4d..fc4ef15 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -627,14 +627,7 @@
 		return NULL;
 	}
 
-	result = PyUnicode_FromString("deque(");
-	if (result == NULL) {
-		Py_DECREF(aslist);
-		Py_ReprLeave(deque);
-		return NULL;
-	}
-	PyUnicode_AppendAndDel(&result, PyObject_Repr(aslist));
-	PyUnicode_AppendAndDel(&result, PyUnicode_FromString(")"));
+	result = PyUnicode_FromFormat("deque(%R)", aslist);
 	Py_DECREF(aslist);
 	Py_ReprLeave(deque);
 	return result;
@@ -1208,25 +1201,18 @@
 static PyObject *
 defdict_repr(defdictobject *dd)
 {
-	PyObject *defrepr;
 	PyObject *baserepr;
+	PyObject *def;
 	PyObject *result;
 	baserepr = PyDict_Type.tp_repr((PyObject *)dd);
 	if (baserepr == NULL)
 		return NULL;
 	if (dd->default_factory == NULL)
-		defrepr = PyUnicode_FromString("None");
+		def = Py_None;
 	else
-		defrepr = PyObject_Repr(dd->default_factory);
-	if (defrepr == NULL) {
-		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(")"));
+		def = dd->default_factory;
+	result = PyUnicode_FromFormat("defaultdict(%R, %U)", def, baserepr);
+	Py_DECREF(baserepr);
 	return result;
 }
 
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 442ab83..2ec2332 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -1118,17 +1118,7 @@
 static PyObject*
 element_repr(ElementObject* self)
 {
-    PyObject* repr;
-    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;
+    return PyUnicode_FromFormat("<Element %R at %p>", self->tag, self);
 }
 
 static PyObject*
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index fe568fa..790a1be 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -809,10 +809,8 @@
 static PyObject *
 PyTclObject_repr(PyTclObject *self)
 {
-	char buf[50];
-	PyOS_snprintf(buf, 50, "<%s object at %p>",
-		      self->value->typePtr->name, self->value);
-	return PyUnicode_FromString(buf);
+	return PyUnicode_FromFormat("<%s object at %p>",
+	                            self->value->typePtr->name, self->value);
 }
 
 static int
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index d61b1ae..7349527 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1567,29 +1567,23 @@
 array_repr(arrayobject *a)
 {
 	char buf[256], typecode;
-	PyObject *s, *t, *v = NULL;
+	PyObject *s, *v = NULL;
 	Py_ssize_t len;
 
 	len = a->ob_size;
 	typecode = a->ob_descr->typecode;
 	if (len == 0) {
-		PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
-		return PyUnicode_FromString(buf);
+		return PyUnicode_FromFormat("array('%c')", typecode);
 	}
-		
 	if (typecode == 'c')
 		v = array_tostring(a, NULL);
 	else if (typecode == 'u')
 		v = array_tounicode(a, NULL);
 	else
 		v = array_tolist(a, NULL);
-	t = PyObject_Repr(v);
-	Py_XDECREF(v);
 
-	PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
-	s = PyUnicode_FromString(buf);
-	PyUnicode_AppendAndDel(&s, t);
-	PyUnicode_AppendAndDel(&s, PyUnicode_FromString(")"));
+	s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
+	Py_DECREF(v);
 	return s;
 }
 
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 5d3c679..e03148c 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -1973,19 +1973,19 @@
 {
 	if (GET_TD_MICROSECONDS(self) != 0)
 		return PyUnicode_FromFormat("%s(%d, %d, %d)",
-					   self->ob_type->tp_name,
-					   GET_TD_DAYS(self),
-					   GET_TD_SECONDS(self),
-					   GET_TD_MICROSECONDS(self));
+		                            self->ob_type->tp_name,
+		                            GET_TD_DAYS(self),
+		                            GET_TD_SECONDS(self),
+		                            GET_TD_MICROSECONDS(self));
 	if (GET_TD_SECONDS(self) != 0)
 		return PyUnicode_FromFormat("%s(%d, %d)",
-					   self->ob_type->tp_name,
-					   GET_TD_DAYS(self),
-					   GET_TD_SECONDS(self));
+		                            self->ob_type->tp_name,
+		                            GET_TD_DAYS(self),
+		                            GET_TD_SECONDS(self));
 
 	return PyUnicode_FromFormat("%s(%d)",
-				   self->ob_type->tp_name,
-				   GET_TD_DAYS(self));
+	                            self->ob_type->tp_name,
+	                            GET_TD_DAYS(self));
 }
 
 static PyObject *
@@ -2402,15 +2402,9 @@
 static PyObject *
 date_repr(PyDateTime_Date *self)
 {
-	char buffer[1028];
-	const char *type_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));
-
-	return PyUnicode_FromString(buffer);
+	return PyUnicode_FromFormat("%s(%d, %d, %d)",
+	                            self->ob_type->tp_name,
+	                            GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
 }
 
 static PyObject *
@@ -3114,7 +3108,6 @@
 static PyObject *
 time_repr(PyDateTime_Time *self)
 {
-	char buffer[100];
 	const char *type_name = self->ob_type->tp_name;
 	int h = TIME_GET_HOUR(self);
 	int m = TIME_GET_MINUTE(self);
@@ -3123,15 +3116,13 @@
 	PyObject *result = NULL;
 
 	if (us)
-		PyOS_snprintf(buffer, sizeof(buffer),
-			      "%s(%d, %d, %d, %d)", type_name, h, m, s, us);
+		result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)",
+		                              type_name, h, m, s, us);
 	else if (s)
-		PyOS_snprintf(buffer, sizeof(buffer),
-			      "%s(%d, %d, %d)", type_name, h, m, s);
+		result = PyUnicode_FromFormat("%s(%d, %d, %d)",
+		                              type_name, h, m, s);
 	else
-		PyOS_snprintf(buffer, sizeof(buffer),
-			      "%s(%d, %d)", type_name, h, m);
-	result = PyUnicode_FromString(buffer);
+		result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m);
 	if (result != NULL && HASTZINFO(self))
 		result = append_keyword_tzinfo(result, self->tzinfo);
 	return result;
@@ -4020,7 +4011,7 @@
 	PyObject *baserepr;
 
 	if (DATE_GET_MICROSECOND(self)) {
-		PyOS_snprintf(buffer, sizeof(buffer),
+		baserepr = PyUnicode_FromFormat(
 			      "%s(%d, %d, %d, %d, %d, %d, %d)",
 			      type_name,
 			      GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
@@ -4029,7 +4020,7 @@
 			      DATE_GET_MICROSECOND(self));
 	}
 	else if (DATE_GET_SECOND(self)) {
-		PyOS_snprintf(buffer, sizeof(buffer),
+		baserepr = PyUnicode_FromFormat(
 			      "%s(%d, %d, %d, %d, %d, %d)",
 			      type_name,
 			      GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
@@ -4037,13 +4028,12 @@
 			      DATE_GET_SECOND(self));
 	}
 	else {
-		PyOS_snprintf(buffer, sizeof(buffer),
+		baserepr = PyUnicode_FromFormat(
 			      "%s(%d, %d, %d, %d, %d)",
 			      type_name,
 			      GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
 			      DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
 	}
-	baserepr = PyUnicode_FromString(buffer);
 	if (baserepr == NULL || ! HASTZINFO(self))
 		return baserepr;
 	return append_keyword_tzinfo(baserepr, self->tzinfo);
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index c3f198e..2685ca4 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2389,20 +2389,10 @@
 static PyObject *
 repeat_repr(repeatobject *ro)
 {
-	PyObject *result, *objrepr;
-
-	objrepr = PyObject_Repr(ro->element);
-	if (objrepr == NULL)
-		return NULL;
-
 	if (ro->cnt == -1)
-		result = PyUnicode_FromFormat("repeat(%U)",
-			objrepr);
+		return PyUnicode_FromFormat("repeat(%R)", ro->element);
 	else
-		result = PyUnicode_FromFormat("repeat(%U, %zd)",
-			objrepr, ro->cnt);
-	Py_DECREF(objrepr);
-	return result;
+		return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt);
 }	
 
 static PyObject *