Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | #include "structmember.h" |
| 4 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5 | /* _functools module written and maintained |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 6 | by Hye-Shik Chang <perky@FreeBSD.org> |
| 7 | with adaptations by Raymond Hettinger <python@rcn.com> |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 8 | Copyright (c) 2004, 2005, 2006 Python Software Foundation. |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 9 | All rights reserved. |
| 10 | */ |
| 11 | |
| 12 | /* partial object **********************************************************/ |
| 13 | |
| 14 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | PyObject_HEAD |
| 16 | PyObject *fn; |
| 17 | PyObject *args; |
| 18 | PyObject *kw; |
| 19 | PyObject *dict; |
| 20 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 21 | } partialobject; |
| 22 | |
| 23 | static PyTypeObject partial_type; |
| 24 | |
| 25 | static PyObject * |
| 26 | partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 27 | { |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 28 | PyObject *func, *pargs, *nargs, *pkw; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | partialobject *pto; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 30 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | if (PyTuple_GET_SIZE(args) < 1) { |
| 32 | PyErr_SetString(PyExc_TypeError, |
| 33 | "type 'partial' takes at least one argument"); |
| 34 | return NULL; |
| 35 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 36 | |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 37 | pargs = pkw = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | func = PyTuple_GET_ITEM(args, 0); |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 39 | if (Py_TYPE(func) == &partial_type && type == &partial_type) { |
| 40 | partialobject *part = (partialobject *)func; |
| 41 | if (part->dict == NULL) { |
| 42 | pargs = part->args; |
| 43 | pkw = part->kw; |
| 44 | func = part->fn; |
| 45 | } |
| 46 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | if (!PyCallable_Check(func)) { |
| 48 | PyErr_SetString(PyExc_TypeError, |
| 49 | "the first argument must be callable"); |
| 50 | return NULL; |
| 51 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 52 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | /* create partialobject structure */ |
| 54 | pto = (partialobject *)type->tp_alloc(type, 0); |
| 55 | if (pto == NULL) |
| 56 | return NULL; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | pto->fn = func; |
| 59 | Py_INCREF(func); |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 60 | |
| 61 | nargs = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX); |
| 62 | if (nargs == NULL) { |
| 63 | pto->args = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | pto->kw = NULL; |
| 65 | Py_DECREF(pto); |
| 66 | return NULL; |
| 67 | } |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 68 | if (pargs == Py_None || PyTuple_GET_SIZE(pargs) == 0) { |
| 69 | pto->args = nargs; |
| 70 | Py_INCREF(nargs); |
| 71 | } |
| 72 | else if (PyTuple_GET_SIZE(nargs) == 0) { |
| 73 | pto->args = pargs; |
| 74 | Py_INCREF(pargs); |
| 75 | } |
| 76 | else { |
| 77 | pto->args = PySequence_Concat(pargs, nargs); |
| 78 | if (pto->args == NULL) { |
| 79 | pto->kw = NULL; |
| 80 | Py_DECREF(pto); |
| 81 | return NULL; |
| 82 | } |
| 83 | } |
| 84 | Py_DECREF(nargs); |
| 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | if (kw != NULL) { |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 87 | if (pkw == Py_None) { |
| 88 | pto->kw = PyDict_Copy(kw); |
| 89 | } |
| 90 | else { |
| 91 | pto->kw = PyDict_Copy(pkw); |
| 92 | if (pto->kw != NULL) { |
| 93 | if (PyDict_Merge(pto->kw, kw, 1) != 0) { |
| 94 | Py_DECREF(pto); |
| 95 | return NULL; |
| 96 | } |
| 97 | } |
| 98 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | if (pto->kw == NULL) { |
| 100 | Py_DECREF(pto); |
| 101 | return NULL; |
| 102 | } |
Alexander Belopolsky | e49af34 | 2015-03-01 15:08:17 -0500 | [diff] [blame^] | 103 | } |
| 104 | else { |
| 105 | pto->kw = pkw; |
| 106 | Py_INCREF(pkw); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | pto->weakreflist = NULL; |
| 110 | pto->dict = NULL; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | return (PyObject *)pto; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void |
| 116 | partial_dealloc(partialobject *pto) |
| 117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | PyObject_GC_UnTrack(pto); |
| 119 | if (pto->weakreflist != NULL) |
| 120 | PyObject_ClearWeakRefs((PyObject *) pto); |
| 121 | Py_XDECREF(pto->fn); |
| 122 | Py_XDECREF(pto->args); |
| 123 | Py_XDECREF(pto->kw); |
| 124 | Py_XDECREF(pto->dict); |
| 125 | Py_TYPE(pto)->tp_free(pto); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static PyObject * |
| 129 | partial_call(partialobject *pto, PyObject *args, PyObject *kw) |
| 130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | PyObject *ret; |
| 132 | PyObject *argappl = NULL, *kwappl = NULL; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | assert (PyCallable_Check(pto->fn)); |
| 135 | assert (PyTuple_Check(pto->args)); |
| 136 | assert (pto->kw == Py_None || PyDict_Check(pto->kw)); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | if (PyTuple_GET_SIZE(pto->args) == 0) { |
| 139 | argappl = args; |
| 140 | Py_INCREF(args); |
| 141 | } else if (PyTuple_GET_SIZE(args) == 0) { |
| 142 | argappl = pto->args; |
| 143 | Py_INCREF(pto->args); |
| 144 | } else { |
| 145 | argappl = PySequence_Concat(pto->args, args); |
| 146 | if (argappl == NULL) |
| 147 | return NULL; |
| 148 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | if (pto->kw == Py_None) { |
| 151 | kwappl = kw; |
| 152 | Py_XINCREF(kw); |
| 153 | } else { |
| 154 | kwappl = PyDict_Copy(pto->kw); |
| 155 | if (kwappl == NULL) { |
| 156 | Py_DECREF(argappl); |
| 157 | return NULL; |
| 158 | } |
| 159 | if (kw != NULL) { |
| 160 | if (PyDict_Merge(kwappl, kw, 1) != 0) { |
| 161 | Py_DECREF(argappl); |
| 162 | Py_DECREF(kwappl); |
| 163 | return NULL; |
| 164 | } |
| 165 | } |
| 166 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | ret = PyObject_Call(pto->fn, argappl, kwappl); |
| 169 | Py_DECREF(argappl); |
| 170 | Py_XDECREF(kwappl); |
| 171 | return ret; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static int |
| 175 | partial_traverse(partialobject *pto, visitproc visit, void *arg) |
| 176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | Py_VISIT(pto->fn); |
| 178 | Py_VISIT(pto->args); |
| 179 | Py_VISIT(pto->kw); |
| 180 | Py_VISIT(pto->dict); |
| 181 | return 0; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | PyDoc_STRVAR(partial_doc, |
| 185 | "partial(func, *args, **keywords) - new function with partial application\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | of the given arguments and keywords.\n"); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 187 | |
| 188 | #define OFF(x) offsetof(partialobject, x) |
| 189 | static PyMemberDef partial_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | {"func", T_OBJECT, OFF(fn), READONLY, |
| 191 | "function object to use in future partial calls"}, |
| 192 | {"args", T_OBJECT, OFF(args), READONLY, |
| 193 | "tuple of arguments to future partial calls"}, |
| 194 | {"keywords", T_OBJECT, OFF(kw), READONLY, |
| 195 | "dictionary of keyword arguments to future partial calls"}, |
| 196 | {NULL} /* Sentinel */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Georg Brandl | c2fb6c7 | 2006-02-21 17:49:57 +0000 | [diff] [blame] | 199 | static PyGetSetDef partial_getsetlist[] = { |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 200 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | {NULL} /* Sentinel */ |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Alexander Belopolsky | 41e422a | 2010-12-01 20:05:49 +0000 | [diff] [blame] | 204 | static PyObject * |
| 205 | partial_repr(partialobject *pto) |
| 206 | { |
| 207 | PyObject *result; |
| 208 | PyObject *arglist; |
| 209 | PyObject *tmp; |
| 210 | Py_ssize_t i, n; |
| 211 | |
| 212 | arglist = PyUnicode_FromString(""); |
| 213 | if (arglist == NULL) { |
| 214 | return NULL; |
| 215 | } |
| 216 | /* Pack positional arguments */ |
| 217 | assert (PyTuple_Check(pto->args)); |
| 218 | n = PyTuple_GET_SIZE(pto->args); |
| 219 | for (i = 0; i < n; i++) { |
| 220 | tmp = PyUnicode_FromFormat("%U, %R", arglist, |
| 221 | PyTuple_GET_ITEM(pto->args, i)); |
| 222 | Py_DECREF(arglist); |
| 223 | if (tmp == NULL) |
| 224 | return NULL; |
| 225 | arglist = tmp; |
| 226 | } |
| 227 | /* Pack keyword arguments */ |
| 228 | assert (pto->kw == Py_None || PyDict_Check(pto->kw)); |
| 229 | if (pto->kw != Py_None) { |
| 230 | PyObject *key, *value; |
| 231 | for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) { |
| 232 | tmp = PyUnicode_FromFormat("%U, %U=%R", arglist, |
| 233 | key, value); |
| 234 | Py_DECREF(arglist); |
| 235 | if (tmp == NULL) |
| 236 | return NULL; |
| 237 | arglist = tmp; |
| 238 | } |
| 239 | } |
| 240 | result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name, |
| 241 | pto->fn, arglist); |
| 242 | Py_DECREF(arglist); |
| 243 | return result; |
| 244 | } |
| 245 | |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 246 | /* Pickle strategy: |
| 247 | __reduce__ by itself doesn't support getting kwargs in the unpickle |
| 248 | operation so we define a __setstate__ that replaces all the information |
| 249 | about the partial. If we only replaced part of it someone would use |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 250 | it as a hook to do strange things. |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 251 | */ |
| 252 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 253 | static PyObject * |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 254 | partial_reduce(partialobject *pto, PyObject *unused) |
| 255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn, |
| 257 | pto->args, pto->kw, |
| 258 | pto->dict ? pto->dict : Py_None); |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 261 | static PyObject * |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 262 | partial_setstate(partialobject *pto, PyObject *state) |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | PyObject *fn, *fnargs, *kw, *dict; |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 265 | if (!PyArg_ParseTuple(state, "OOOO", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | &fn, &fnargs, &kw, &dict)) |
| 267 | return NULL; |
| 268 | Py_XDECREF(pto->fn); |
| 269 | Py_XDECREF(pto->args); |
| 270 | Py_XDECREF(pto->kw); |
| 271 | Py_XDECREF(pto->dict); |
| 272 | pto->fn = fn; |
| 273 | pto->args = fnargs; |
| 274 | pto->kw = kw; |
| 275 | if (dict != Py_None) { |
| 276 | pto->dict = dict; |
| 277 | Py_INCREF(dict); |
| 278 | } else { |
| 279 | pto->dict = NULL; |
| 280 | } |
| 281 | Py_INCREF(fn); |
| 282 | Py_INCREF(fnargs); |
| 283 | Py_INCREF(kw); |
| 284 | Py_RETURN_NONE; |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static PyMethodDef partial_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS}, |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 289 | {"__setstate__", (PyCFunction)partial_setstate, METH_O}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | {NULL, NULL} /* sentinel */ |
Jack Diederich | e0cbd69 | 2009-04-01 04:27:09 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 293 | static PyTypeObject partial_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | PyVarObject_HEAD_INIT(NULL, 0) |
| 295 | "functools.partial", /* tp_name */ |
| 296 | sizeof(partialobject), /* tp_basicsize */ |
| 297 | 0, /* tp_itemsize */ |
| 298 | /* methods */ |
| 299 | (destructor)partial_dealloc, /* tp_dealloc */ |
| 300 | 0, /* tp_print */ |
| 301 | 0, /* tp_getattr */ |
| 302 | 0, /* tp_setattr */ |
| 303 | 0, /* tp_reserved */ |
Alexander Belopolsky | 41e422a | 2010-12-01 20:05:49 +0000 | [diff] [blame] | 304 | (reprfunc)partial_repr, /* tp_repr */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | 0, /* tp_as_number */ |
| 306 | 0, /* tp_as_sequence */ |
| 307 | 0, /* tp_as_mapping */ |
| 308 | 0, /* tp_hash */ |
| 309 | (ternaryfunc)partial_call, /* tp_call */ |
| 310 | 0, /* tp_str */ |
| 311 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 312 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 313 | 0, /* tp_as_buffer */ |
| 314 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 315 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 316 | partial_doc, /* tp_doc */ |
| 317 | (traverseproc)partial_traverse, /* tp_traverse */ |
| 318 | 0, /* tp_clear */ |
| 319 | 0, /* tp_richcompare */ |
| 320 | offsetof(partialobject, weakreflist), /* tp_weaklistoffset */ |
| 321 | 0, /* tp_iter */ |
| 322 | 0, /* tp_iternext */ |
| 323 | partial_methods, /* tp_methods */ |
| 324 | partial_memberlist, /* tp_members */ |
| 325 | partial_getsetlist, /* tp_getset */ |
| 326 | 0, /* tp_base */ |
| 327 | 0, /* tp_dict */ |
| 328 | 0, /* tp_descr_get */ |
| 329 | 0, /* tp_descr_set */ |
| 330 | offsetof(partialobject, dict), /* tp_dictoffset */ |
| 331 | 0, /* tp_init */ |
| 332 | 0, /* tp_alloc */ |
| 333 | partial_new, /* tp_new */ |
| 334 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 338 | /* cmp_to_key ***************************************************************/ |
| 339 | |
| 340 | typedef struct { |
Victor Stinner | 446c8d5 | 2011-04-05 12:21:35 +0200 | [diff] [blame] | 341 | PyObject_HEAD |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 342 | PyObject *cmp; |
| 343 | PyObject *object; |
| 344 | } keyobject; |
| 345 | |
| 346 | static void |
| 347 | keyobject_dealloc(keyobject *ko) |
| 348 | { |
| 349 | Py_DECREF(ko->cmp); |
| 350 | Py_XDECREF(ko->object); |
| 351 | PyObject_FREE(ko); |
| 352 | } |
| 353 | |
| 354 | static int |
| 355 | keyobject_traverse(keyobject *ko, visitproc visit, void *arg) |
| 356 | { |
| 357 | Py_VISIT(ko->cmp); |
| 358 | if (ko->object) |
| 359 | Py_VISIT(ko->object); |
| 360 | return 0; |
| 361 | } |
| 362 | |
Benjamin Peterson | 3bd9729 | 2011-04-05 17:25:14 -0500 | [diff] [blame] | 363 | static int |
| 364 | keyobject_clear(keyobject *ko) |
| 365 | { |
| 366 | Py_CLEAR(ko->cmp); |
| 367 | if (ko->object) |
| 368 | Py_CLEAR(ko->object); |
| 369 | return 0; |
| 370 | } |
| 371 | |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 372 | static PyMemberDef keyobject_members[] = { |
| 373 | {"obj", T_OBJECT, |
| 374 | offsetof(keyobject, object), 0, |
| 375 | PyDoc_STR("Value wrapped by a key function.")}, |
| 376 | {NULL} |
| 377 | }; |
| 378 | |
| 379 | static PyObject * |
Raymond Hettinger | a563286 | 2011-04-09 12:57:00 -0700 | [diff] [blame] | 380 | keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds); |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 381 | |
| 382 | static PyObject * |
| 383 | keyobject_richcompare(PyObject *ko, PyObject *other, int op); |
| 384 | |
| 385 | static PyTypeObject keyobject_type = { |
| 386 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 387 | "functools.KeyWrapper", /* tp_name */ |
| 388 | sizeof(keyobject), /* tp_basicsize */ |
| 389 | 0, /* tp_itemsize */ |
| 390 | /* methods */ |
| 391 | (destructor)keyobject_dealloc, /* tp_dealloc */ |
| 392 | 0, /* tp_print */ |
| 393 | 0, /* tp_getattr */ |
| 394 | 0, /* tp_setattr */ |
| 395 | 0, /* tp_reserved */ |
| 396 | 0, /* tp_repr */ |
| 397 | 0, /* tp_as_number */ |
| 398 | 0, /* tp_as_sequence */ |
| 399 | 0, /* tp_as_mapping */ |
| 400 | 0, /* tp_hash */ |
| 401 | (ternaryfunc)keyobject_call, /* tp_call */ |
| 402 | 0, /* tp_str */ |
| 403 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 404 | 0, /* tp_setattro */ |
| 405 | 0, /* tp_as_buffer */ |
| 406 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 407 | 0, /* tp_doc */ |
| 408 | (traverseproc)keyobject_traverse, /* tp_traverse */ |
Benjamin Peterson | 3bd9729 | 2011-04-05 17:25:14 -0500 | [diff] [blame] | 409 | (inquiry)keyobject_clear, /* tp_clear */ |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 410 | keyobject_richcompare, /* tp_richcompare */ |
| 411 | 0, /* tp_weaklistoffset */ |
| 412 | 0, /* tp_iter */ |
| 413 | 0, /* tp_iternext */ |
| 414 | 0, /* tp_methods */ |
| 415 | keyobject_members, /* tp_members */ |
| 416 | 0, /* tp_getset */ |
| 417 | }; |
| 418 | |
| 419 | static PyObject * |
| 420 | keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds) |
| 421 | { |
| 422 | PyObject *object; |
| 423 | keyobject *result; |
| 424 | static char *kwargs[] = {"obj", NULL}; |
| 425 | |
| 426 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwargs, &object)) |
| 427 | return NULL; |
| 428 | result = PyObject_New(keyobject, &keyobject_type); |
| 429 | if (!result) |
| 430 | return NULL; |
| 431 | Py_INCREF(ko->cmp); |
| 432 | result->cmp = ko->cmp; |
| 433 | Py_INCREF(object); |
| 434 | result->object = object; |
| 435 | return (PyObject *)result; |
| 436 | } |
| 437 | |
| 438 | static PyObject * |
| 439 | keyobject_richcompare(PyObject *ko, PyObject *other, int op) |
| 440 | { |
| 441 | PyObject *res; |
| 442 | PyObject *args; |
| 443 | PyObject *x; |
| 444 | PyObject *y; |
| 445 | PyObject *compare; |
| 446 | PyObject *answer; |
| 447 | static PyObject *zero; |
| 448 | |
| 449 | if (zero == NULL) { |
| 450 | zero = PyLong_FromLong(0); |
| 451 | if (!zero) |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | if (Py_TYPE(other) != &keyobject_type){ |
| 456 | PyErr_Format(PyExc_TypeError, "other argument must be K instance"); |
| 457 | return NULL; |
| 458 | } |
| 459 | compare = ((keyobject *) ko)->cmp; |
| 460 | assert(compare != NULL); |
| 461 | x = ((keyobject *) ko)->object; |
| 462 | y = ((keyobject *) other)->object; |
| 463 | if (!x || !y){ |
| 464 | PyErr_Format(PyExc_AttributeError, "object"); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | /* Call the user's comparison function and translate the 3-way |
| 469 | * result into true or false (or error). |
| 470 | */ |
| 471 | args = PyTuple_New(2); |
| 472 | if (args == NULL) |
| 473 | return NULL; |
| 474 | Py_INCREF(x); |
| 475 | Py_INCREF(y); |
| 476 | PyTuple_SET_ITEM(args, 0, x); |
| 477 | PyTuple_SET_ITEM(args, 1, y); |
| 478 | res = PyObject_Call(compare, args, NULL); |
| 479 | Py_DECREF(args); |
| 480 | if (res == NULL) |
| 481 | return NULL; |
| 482 | answer = PyObject_RichCompare(res, zero, op); |
| 483 | Py_DECREF(res); |
| 484 | return answer; |
| 485 | } |
| 486 | |
| 487 | static PyObject * |
Victor Stinner | 446c8d5 | 2011-04-05 12:21:35 +0200 | [diff] [blame] | 488 | functools_cmp_to_key(PyObject *self, PyObject *args, PyObject *kwds) |
| 489 | { |
| 490 | PyObject *cmp; |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 491 | static char *kwargs[] = {"mycmp", NULL}; |
Victor Stinner | 446c8d5 | 2011-04-05 12:21:35 +0200 | [diff] [blame] | 492 | keyobject *object; |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 493 | |
| 494 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwargs, &cmp)) |
| 495 | return NULL; |
Victor Stinner | 446c8d5 | 2011-04-05 12:21:35 +0200 | [diff] [blame] | 496 | object = PyObject_New(keyobject, &keyobject_type); |
Raymond Hettinger | 7ab9e22 | 2011-04-05 02:33:54 -0700 | [diff] [blame] | 497 | if (!object) |
| 498 | return NULL; |
| 499 | Py_INCREF(cmp); |
| 500 | object->cmp = cmp; |
| 501 | object->object = NULL; |
| 502 | return (PyObject *)object; |
| 503 | } |
| 504 | |
| 505 | PyDoc_STRVAR(functools_cmp_to_key_doc, |
| 506 | "Convert a cmp= function into a key= function."); |
| 507 | |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 508 | /* reduce (used to be a builtin) ********************************************/ |
| 509 | |
| 510 | static PyObject * |
| 511 | functools_reduce(PyObject *self, PyObject *args) |
| 512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | PyObject *seq, *func, *result = NULL, *it; |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) |
| 516 | return NULL; |
| 517 | if (result != NULL) |
| 518 | Py_INCREF(result); |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | it = PyObject_GetIter(seq); |
| 521 | if (it == NULL) { |
Alexander Belopolsky | e29e6bf | 2010-08-16 18:55:46 +0000 | [diff] [blame] | 522 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 523 | PyErr_SetString(PyExc_TypeError, |
| 524 | "reduce() arg 2 must support iteration"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | Py_XDECREF(result); |
| 526 | return NULL; |
| 527 | } |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | if ((args = PyTuple_New(2)) == NULL) |
| 530 | goto Fail; |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | for (;;) { |
| 533 | PyObject *op2; |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | if (args->ob_refcnt > 1) { |
| 536 | Py_DECREF(args); |
| 537 | if ((args = PyTuple_New(2)) == NULL) |
| 538 | goto Fail; |
| 539 | } |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | op2 = PyIter_Next(it); |
| 542 | if (op2 == NULL) { |
| 543 | if (PyErr_Occurred()) |
| 544 | goto Fail; |
| 545 | break; |
| 546 | } |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | if (result == NULL) |
| 549 | result = op2; |
| 550 | else { |
| 551 | PyTuple_SetItem(args, 0, result); |
| 552 | PyTuple_SetItem(args, 1, op2); |
| 553 | if ((result = PyEval_CallObject(func, args)) == NULL) |
| 554 | goto Fail; |
| 555 | } |
| 556 | } |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | Py_DECREF(args); |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | if (result == NULL) |
| 561 | PyErr_SetString(PyExc_TypeError, |
| 562 | "reduce() of empty sequence with no initial value"); |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | Py_DECREF(it); |
| 565 | return result; |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 566 | |
| 567 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | Py_XDECREF(args); |
| 569 | Py_XDECREF(result); |
| 570 | Py_DECREF(it); |
| 571 | return NULL; |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | PyDoc_STRVAR(functools_reduce_doc, |
| 575 | "reduce(function, sequence[, initial]) -> value\n\ |
| 576 | \n\ |
| 577 | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
| 578 | from left to right, so as to reduce the sequence to a single value.\n\ |
| 579 | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
| 580 | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
| 581 | of the sequence in the calculation, and serves as a default when the\n\ |
| 582 | sequence is empty."); |
| 583 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 584 | /* module level code ********************************************************/ |
| 585 | |
| 586 | PyDoc_STRVAR(module_doc, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 587 | "Tools that operate on functions."); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 588 | |
| 589 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc}, |
Victor Stinner | 446c8d5 | 2011-04-05 12:21:35 +0200 | [diff] [blame] | 591 | {"cmp_to_key", (PyCFunction)functools_cmp_to_key, |
| 592 | METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 594 | }; |
| 595 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 596 | |
| 597 | static struct PyModuleDef _functoolsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | PyModuleDef_HEAD_INIT, |
| 599 | "_functools", |
| 600 | module_doc, |
| 601 | -1, |
| 602 | module_methods, |
| 603 | NULL, |
| 604 | NULL, |
| 605 | NULL, |
| 606 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 607 | }; |
| 608 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 609 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 610 | PyInit__functools(void) |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | int i; |
| 613 | PyObject *m; |
| 614 | char *name; |
| 615 | PyTypeObject *typelist[] = { |
| 616 | &partial_type, |
| 617 | NULL |
| 618 | }; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | m = PyModule_Create(&_functoolsmodule); |
| 621 | if (m == NULL) |
| 622 | return NULL; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 625 | if (PyType_Ready(typelist[i]) < 0) { |
| 626 | Py_DECREF(m); |
| 627 | return NULL; |
| 628 | } |
| 629 | name = strchr(typelist[i]->tp_name, '.'); |
| 630 | assert (name != NULL); |
| 631 | Py_INCREF(typelist[i]); |
| 632 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
| 633 | } |
| 634 | return m; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 635 | } |