diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-05-19 21:49:49 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-05-19 21:49:49 (GMT) |
commit | 7569dfe11d51a11bfb11002d31245b889916fb11 (patch) | |
tree | 7e4cbbb7a27a366ea0cdc8eeacc05a23f649e4cd /Modules/_collectionsmodule.c | |
parent | 94b59bb14474b6fe5a272a2c60b65f8375e827b3 (diff) | |
download | cpython-7569dfe11d51a11bfb11002d31245b889916fb11.zip cpython-7569dfe11d51a11bfb11002d31245b889916fb11.tar.gz cpython-7569dfe11d51a11bfb11002d31245b889916fb11.tar.bz2 |
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).
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r-- | Modules/_collectionsmodule.c | 26 |
1 files changed, 6 insertions, 20 deletions
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 @@ deque_repr(PyObject *deque) 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 @@ defdict_print(defdictobject *dd, FILE *fp, int flags) 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; } |