Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | #include "structmember.h" |
| 4 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +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> |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +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 | |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 12 | /* reduce() *************************************************************/ |
| 13 | |
| 14 | static PyObject * |
| 15 | functools_reduce(PyObject *self, PyObject *args) |
| 16 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 17 | PyObject *seq, *func, *result = NULL, *it; |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 18 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 19 | if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) |
| 20 | return NULL; |
| 21 | if (result != NULL) |
| 22 | Py_INCREF(result); |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 23 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 24 | it = PyObject_GetIter(seq); |
| 25 | if (it == NULL) { |
| 26 | PyErr_SetString(PyExc_TypeError, |
| 27 | "reduce() arg 2 must support iteration"); |
| 28 | Py_XDECREF(result); |
| 29 | return NULL; |
| 30 | } |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 31 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 32 | if ((args = PyTuple_New(2)) == NULL) |
| 33 | goto Fail; |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 35 | for (;;) { |
| 36 | PyObject *op2; |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 37 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 38 | if (args->ob_refcnt > 1) { |
| 39 | Py_DECREF(args); |
| 40 | if ((args = PyTuple_New(2)) == NULL) |
| 41 | goto Fail; |
| 42 | } |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 43 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 44 | op2 = PyIter_Next(it); |
| 45 | if (op2 == NULL) { |
| 46 | if (PyErr_Occurred()) |
| 47 | goto Fail; |
| 48 | break; |
| 49 | } |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 50 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 51 | if (result == NULL) |
| 52 | result = op2; |
| 53 | else { |
| 54 | PyTuple_SetItem(args, 0, result); |
| 55 | PyTuple_SetItem(args, 1, op2); |
| 56 | if ((result = PyEval_CallObject(func, args)) == NULL) |
| 57 | goto Fail; |
| 58 | } |
| 59 | } |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 61 | Py_DECREF(args); |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 62 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 63 | if (result == NULL) |
| 64 | PyErr_SetString(PyExc_TypeError, |
| 65 | "reduce() of empty sequence with no initial value"); |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 67 | Py_DECREF(it); |
| 68 | return result; |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 69 | |
| 70 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 71 | Py_XDECREF(args); |
| 72 | Py_XDECREF(result); |
| 73 | Py_DECREF(it); |
| 74 | return NULL; |
Brett Cannon | 83e8184 | 2008-08-09 23:30:55 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | PyDoc_STRVAR(reduce_doc, |
| 78 | "reduce(function, sequence[, initial]) -> value\n\ |
| 79 | \n\ |
| 80 | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
| 81 | from left to right, so as to reduce the sequence to a single value.\n\ |
| 82 | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
| 83 | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
| 84 | of the sequence in the calculation, and serves as a default when the\n\ |
| 85 | sequence is empty."); |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 90 | /* partial object **********************************************************/ |
| 91 | |
| 92 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 93 | PyObject_HEAD |
| 94 | PyObject *fn; |
| 95 | PyObject *args; |
| 96 | PyObject *kw; |
| 97 | PyObject *dict; |
| 98 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 99 | } partialobject; |
| 100 | |
| 101 | static PyTypeObject partial_type; |
| 102 | |
| 103 | static PyObject * |
| 104 | partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 105 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 106 | PyObject *func; |
| 107 | partialobject *pto; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 109 | if (PyTuple_GET_SIZE(args) < 1) { |
| 110 | PyErr_SetString(PyExc_TypeError, |
| 111 | "type 'partial' takes at least one argument"); |
| 112 | return NULL; |
| 113 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 115 | func = PyTuple_GET_ITEM(args, 0); |
| 116 | if (!PyCallable_Check(func)) { |
| 117 | PyErr_SetString(PyExc_TypeError, |
| 118 | "the first argument must be callable"); |
| 119 | return NULL; |
| 120 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 122 | /* create partialobject structure */ |
| 123 | pto = (partialobject *)type->tp_alloc(type, 0); |
| 124 | if (pto == NULL) |
| 125 | return NULL; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 127 | pto->fn = func; |
| 128 | Py_INCREF(func); |
| 129 | pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX); |
| 130 | if (pto->args == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 131 | Py_DECREF(pto); |
| 132 | return NULL; |
| 133 | } |
Benjamin Peterson | 72c0141 | 2015-05-09 00:23:41 -0400 | [diff] [blame] | 134 | pto->kw = (kw != NULL) ? PyDict_Copy(kw) : PyDict_New(); |
| 135 | if (pto->kw == NULL) { |
| 136 | Py_DECREF(pto); |
| 137 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 138 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 140 | return (PyObject *)pto; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void |
| 144 | partial_dealloc(partialobject *pto) |
| 145 | { |
INADA Naoki | 4cde4bd | 2017-09-04 12:31:41 +0900 | [diff] [blame] | 146 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 147 | PyObject_GC_UnTrack(pto); |
| 148 | if (pto->weakreflist != NULL) |
| 149 | PyObject_ClearWeakRefs((PyObject *) pto); |
| 150 | Py_XDECREF(pto->fn); |
| 151 | Py_XDECREF(pto->args); |
| 152 | Py_XDECREF(pto->kw); |
| 153 | Py_XDECREF(pto->dict); |
| 154 | Py_TYPE(pto)->tp_free(pto); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static PyObject * |
| 158 | partial_call(partialobject *pto, PyObject *args, PyObject *kw) |
| 159 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 160 | PyObject *ret; |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 161 | PyObject *argappl, *kwappl; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 163 | assert (PyCallable_Check(pto->fn)); |
| 164 | assert (PyTuple_Check(pto->args)); |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 165 | assert (PyDict_Check(pto->kw)); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 167 | if (PyTuple_GET_SIZE(pto->args) == 0) { |
| 168 | argappl = args; |
| 169 | Py_INCREF(args); |
| 170 | } else if (PyTuple_GET_SIZE(args) == 0) { |
| 171 | argappl = pto->args; |
| 172 | Py_INCREF(pto->args); |
| 173 | } else { |
| 174 | argappl = PySequence_Concat(pto->args, args); |
| 175 | if (argappl == NULL) |
| 176 | return NULL; |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 177 | assert(PyTuple_Check(argappl)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 178 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 179 | |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 180 | if (PyDict_Size(pto->kw) == 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 181 | kwappl = kw; |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 182 | Py_XINCREF(kwappl); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 183 | } else { |
| 184 | kwappl = PyDict_Copy(pto->kw); |
| 185 | if (kwappl == NULL) { |
| 186 | Py_DECREF(argappl); |
| 187 | return NULL; |
| 188 | } |
| 189 | if (kw != NULL) { |
| 190 | if (PyDict_Merge(kwappl, kw, 1) != 0) { |
| 191 | Py_DECREF(argappl); |
| 192 | Py_DECREF(kwappl); |
| 193 | return NULL; |
| 194 | } |
| 195 | } |
| 196 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 197 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 198 | ret = PyObject_Call(pto->fn, argappl, kwappl); |
| 199 | Py_DECREF(argappl); |
| 200 | Py_XDECREF(kwappl); |
| 201 | return ret; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int |
| 205 | partial_traverse(partialobject *pto, visitproc visit, void *arg) |
| 206 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 207 | Py_VISIT(pto->fn); |
| 208 | Py_VISIT(pto->args); |
| 209 | Py_VISIT(pto->kw); |
| 210 | Py_VISIT(pto->dict); |
| 211 | return 0; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | PyDoc_STRVAR(partial_doc, |
| 215 | "partial(func, *args, **keywords) - new function with partial application\n\ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 216 | of the given arguments and keywords.\n"); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 217 | |
| 218 | #define OFF(x) offsetof(partialobject, x) |
| 219 | static PyMemberDef partial_memberlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 220 | {"func", T_OBJECT, OFF(fn), READONLY, |
| 221 | "function object to use in future partial calls"}, |
| 222 | {"args", T_OBJECT, OFF(args), READONLY, |
| 223 | "tuple of arguments to future partial calls"}, |
| 224 | {"keywords", T_OBJECT, OFF(kw), READONLY, |
| 225 | "dictionary of keyword arguments to future partial calls"}, |
| 226 | {NULL} /* Sentinel */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 229 | static PyObject * |
| 230 | partial_get_dict(partialobject *pto) |
| 231 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 232 | if (pto->dict == NULL) { |
| 233 | pto->dict = PyDict_New(); |
| 234 | if (pto->dict == NULL) |
| 235 | return NULL; |
| 236 | } |
| 237 | Py_INCREF(pto->dict); |
| 238 | return pto->dict; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int |
| 242 | partial_set_dict(partialobject *pto, PyObject *value) |
| 243 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 244 | PyObject *tmp; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 246 | /* It is illegal to del p.__dict__ */ |
| 247 | if (value == NULL) { |
| 248 | PyErr_SetString(PyExc_TypeError, |
| 249 | "a partial object's dictionary may not be deleted"); |
| 250 | return -1; |
| 251 | } |
| 252 | /* Can only set __dict__ to a dictionary */ |
| 253 | if (!PyDict_Check(value)) { |
| 254 | PyErr_SetString(PyExc_TypeError, |
| 255 | "setting partial object's dictionary to a non-dict"); |
| 256 | return -1; |
| 257 | } |
| 258 | tmp = pto->dict; |
| 259 | Py_INCREF(value); |
| 260 | pto->dict = value; |
| 261 | Py_XDECREF(tmp); |
| 262 | return 0; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Georg Brandl | c2fb6c7 | 2006-02-21 17:49:57 +0000 | [diff] [blame] | 265 | static PyGetSetDef partial_getsetlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 266 | {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict}, |
| 267 | {NULL} /* Sentinel */ |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 270 | /* Pickle strategy: |
| 271 | __reduce__ by itself doesn't support getting kwargs in the unpickle |
| 272 | operation so we define a __setstate__ that replaces all the information |
| 273 | about the partial. If we only replaced part of it someone would use |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 274 | it as a hook to do strange things. |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 275 | */ |
| 276 | |
| 277 | PyObject * |
| 278 | partial_reduce(partialobject *pto, PyObject *unused) |
| 279 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 280 | return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn, |
| 281 | pto->args, pto->kw, |
| 282 | pto->dict ? pto->dict : Py_None); |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | PyObject * |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 286 | partial_setstate(partialobject *pto, PyObject *state) |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 288 | PyObject *fn, *fnargs, *kw, *dict; |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 289 | |
| 290 | if (!PyTuple_Check(state) || |
| 291 | !PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) || |
| 292 | !PyCallable_Check(fn) || |
| 293 | !PyTuple_Check(fnargs) || |
| 294 | (kw != Py_None && !PyDict_Check(kw))) |
| 295 | { |
| 296 | PyErr_SetString(PyExc_TypeError, "invalid partial state"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 297 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | } |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 299 | |
| 300 | if(!PyTuple_CheckExact(fnargs)) |
| 301 | fnargs = PySequence_Tuple(fnargs); |
| 302 | else |
| 303 | Py_INCREF(fnargs); |
| 304 | if (fnargs == NULL) |
| 305 | return NULL; |
| 306 | |
| 307 | if (kw == Py_None) |
| 308 | kw = PyDict_New(); |
| 309 | else if(!PyDict_CheckExact(kw)) |
| 310 | kw = PyDict_Copy(kw); |
| 311 | else |
| 312 | Py_INCREF(kw); |
| 313 | if (kw == NULL) { |
| 314 | Py_DECREF(fnargs); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 318 | Py_INCREF(fn); |
Serhiy Storchaka | 71b7176 | 2016-02-02 18:45:59 +0200 | [diff] [blame] | 319 | if (dict == Py_None) |
| 320 | dict = NULL; |
| 321 | else |
| 322 | Py_INCREF(dict); |
| 323 | |
Serhiy Storchaka | bb65063 | 2016-04-11 09:53:37 +0300 | [diff] [blame] | 324 | Py_SETREF(pto->fn, fn); |
| 325 | Py_SETREF(pto->args, fnargs); |
| 326 | Py_SETREF(pto->kw, kw); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 327 | Py_XSETREF(pto->dict, dict); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 328 | Py_RETURN_NONE; |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static PyMethodDef partial_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 332 | {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS}, |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 333 | {"__setstate__", (PyCFunction)partial_setstate, METH_O}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | {NULL, NULL} /* sentinel */ |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 337 | static PyTypeObject partial_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 338 | PyVarObject_HEAD_INIT(NULL, 0) |
| 339 | "functools.partial", /* tp_name */ |
| 340 | sizeof(partialobject), /* tp_basicsize */ |
| 341 | 0, /* tp_itemsize */ |
| 342 | /* methods */ |
| 343 | (destructor)partial_dealloc, /* tp_dealloc */ |
| 344 | 0, /* tp_print */ |
| 345 | 0, /* tp_getattr */ |
| 346 | 0, /* tp_setattr */ |
| 347 | 0, /* tp_compare */ |
| 348 | 0, /* tp_repr */ |
| 349 | 0, /* tp_as_number */ |
| 350 | 0, /* tp_as_sequence */ |
| 351 | 0, /* tp_as_mapping */ |
| 352 | 0, /* tp_hash */ |
| 353 | (ternaryfunc)partial_call, /* tp_call */ |
| 354 | 0, /* tp_str */ |
| 355 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 356 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 357 | 0, /* tp_as_buffer */ |
| 358 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 359 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 360 | partial_doc, /* tp_doc */ |
| 361 | (traverseproc)partial_traverse, /* tp_traverse */ |
| 362 | 0, /* tp_clear */ |
| 363 | 0, /* tp_richcompare */ |
| 364 | offsetof(partialobject, weakreflist), /* tp_weaklistoffset */ |
| 365 | 0, /* tp_iter */ |
| 366 | 0, /* tp_iternext */ |
| 367 | partial_methods, /* tp_methods */ |
| 368 | partial_memberlist, /* tp_members */ |
| 369 | partial_getsetlist, /* tp_getset */ |
| 370 | 0, /* tp_base */ |
| 371 | 0, /* tp_dict */ |
| 372 | 0, /* tp_descr_get */ |
| 373 | 0, /* tp_descr_set */ |
| 374 | offsetof(partialobject, dict), /* tp_dictoffset */ |
| 375 | 0, /* tp_init */ |
| 376 | 0, /* tp_alloc */ |
| 377 | partial_new, /* tp_new */ |
| 378 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | |
| 382 | /* module level code ********************************************************/ |
| 383 | |
| 384 | PyDoc_STRVAR(module_doc, |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 385 | "Tools that operate on functions."); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 386 | |
| 387 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 388 | {"reduce", functools_reduce, METH_VARARGS, reduce_doc}, |
| 389 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | PyMODINIT_FUNC |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 393 | init_functools(void) |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 394 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 395 | int i; |
| 396 | PyObject *m; |
| 397 | char *name; |
| 398 | PyTypeObject *typelist[] = { |
| 399 | &partial_type, |
| 400 | NULL |
| 401 | }; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 403 | m = Py_InitModule3("_functools", module_methods, module_doc); |
| 404 | if (m == NULL) |
| 405 | return; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 406 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 407 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 408 | if (PyType_Ready(typelist[i]) < 0) |
| 409 | return; |
| 410 | name = strchr(typelist[i]->tp_name, '.'); |
| 411 | assert (name != NULL); |
| 412 | Py_INCREF(typelist[i]); |
| 413 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
| 414 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 415 | } |