diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-12-01 20:05:49 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-12-01 20:05:49 (GMT) |
commit | 41e422a4ffd2c9ce778b1d03180b4aa758dc6fc9 (patch) | |
tree | b24f8dd2394482f6d8dc491d1afb908b792b3354 /Modules/_functoolsmodule.c | |
parent | 419e3de3734edfec1aa04a3123e53199193a0bed (diff) | |
download | cpython-41e422a4ffd2c9ce778b1d03180b4aa758dc6fc9.zip cpython-41e422a4ffd2c9ce778b1d03180b4aa758dc6fc9.tar.gz cpython-41e422a4ffd2c9ce778b1d03180b4aa758dc6fc9.tar.bz2 |
Issue #4113: Added custom __repr__ method to functools.partial.
Diffstat (limited to 'Modules/_functoolsmodule.c')
-rw-r--r-- | Modules/_functoolsmodule.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 3437353..bb2f37b 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -196,6 +196,48 @@ static PyGetSetDef partial_getsetlist[] = { {NULL} /* Sentinel */ }; +static PyObject * +partial_repr(partialobject *pto) +{ + PyObject *result; + PyObject *arglist; + PyObject *tmp; + Py_ssize_t i, n; + + arglist = PyUnicode_FromString(""); + if (arglist == NULL) { + return NULL; + } + /* Pack positional arguments */ + assert (PyTuple_Check(pto->args)); + n = PyTuple_GET_SIZE(pto->args); + for (i = 0; i < n; i++) { + tmp = PyUnicode_FromFormat("%U, %R", arglist, + PyTuple_GET_ITEM(pto->args, i)); + Py_DECREF(arglist); + if (tmp == NULL) + return NULL; + arglist = tmp; + } + /* Pack keyword arguments */ + assert (pto->kw == Py_None || PyDict_Check(pto->kw)); + if (pto->kw != Py_None) { + PyObject *key, *value; + for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) { + tmp = PyUnicode_FromFormat("%U, %U=%R", arglist, + key, value); + Py_DECREF(arglist); + if (tmp == NULL) + return NULL; + arglist = tmp; + } + } + result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name, + pto->fn, arglist); + Py_DECREF(arglist); + return result; +} + /* Pickle strategy: __reduce__ by itself doesn't support getting kwargs in the unpickle operation so we define a __setstate__ that replaces all the information @@ -254,7 +296,7 @@ static PyTypeObject partial_type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ - 0, /* tp_repr */ + (reprfunc)partial_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ |