diff Objects/sliceobject.c @ 41782:bee8d01ef42c

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).
author Walter Dörwald <walter@livinglogic.de>
date Sat, 19 May 2007 21:49:49 +0000
parents b29d71d43c80
children e70cdce0e8b2
line wrap: on
line diff
--- a/Objects/sliceobject.c	Fri May 18 22:12:08 2007 +0000
+++ b/Objects/sliceobject.c	Sat May 19 21:49:49 2007 +0000
@@ -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[] = {