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