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) { |
| 131 | pto->kw = NULL; |
| 132 | Py_DECREF(pto); |
| 133 | return NULL; |
| 134 | } |
Benjamin Peterson | 72c0141 | 2015-05-09 00:23:41 -0400 | [diff] [blame] | 135 | pto->kw = (kw != NULL) ? PyDict_Copy(kw) : PyDict_New(); |
| 136 | if (pto->kw == NULL) { |
| 137 | Py_DECREF(pto); |
| 138 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 139 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 140 | |
Benjamin Peterson | 72c0141 | 2015-05-09 00:23:41 -0400 | [diff] [blame] | 141 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 142 | pto->weakreflist = NULL; |
| 143 | pto->dict = NULL; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 145 | return (PyObject *)pto; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void |
| 149 | partial_dealloc(partialobject *pto) |
| 150 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 151 | PyObject_GC_UnTrack(pto); |
| 152 | if (pto->weakreflist != NULL) |
| 153 | PyObject_ClearWeakRefs((PyObject *) pto); |
| 154 | Py_XDECREF(pto->fn); |
| 155 | Py_XDECREF(pto->args); |
| 156 | Py_XDECREF(pto->kw); |
| 157 | Py_XDECREF(pto->dict); |
| 158 | Py_TYPE(pto)->tp_free(pto); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static PyObject * |
| 162 | partial_call(partialobject *pto, PyObject *args, PyObject *kw) |
| 163 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 164 | PyObject *ret; |
| 165 | PyObject *argappl = NULL, *kwappl = NULL; |
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 | assert (PyCallable_Check(pto->fn)); |
| 168 | assert (PyTuple_Check(pto->args)); |
| 169 | assert (pto->kw == Py_None || PyDict_Check(pto->kw)); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 171 | if (PyTuple_GET_SIZE(pto->args) == 0) { |
| 172 | argappl = args; |
| 173 | Py_INCREF(args); |
| 174 | } else if (PyTuple_GET_SIZE(args) == 0) { |
| 175 | argappl = pto->args; |
| 176 | Py_INCREF(pto->args); |
| 177 | } else { |
| 178 | argappl = PySequence_Concat(pto->args, args); |
| 179 | if (argappl == NULL) |
| 180 | return NULL; |
| 181 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 183 | if (pto->kw == Py_None) { |
| 184 | kwappl = kw; |
| 185 | Py_XINCREF(kw); |
| 186 | } else { |
| 187 | kwappl = PyDict_Copy(pto->kw); |
| 188 | if (kwappl == NULL) { |
| 189 | Py_DECREF(argappl); |
| 190 | return NULL; |
| 191 | } |
| 192 | if (kw != NULL) { |
| 193 | if (PyDict_Merge(kwappl, kw, 1) != 0) { |
| 194 | Py_DECREF(argappl); |
| 195 | Py_DECREF(kwappl); |
| 196 | return NULL; |
| 197 | } |
| 198 | } |
| 199 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 201 | ret = PyObject_Call(pto->fn, argappl, kwappl); |
| 202 | Py_DECREF(argappl); |
| 203 | Py_XDECREF(kwappl); |
| 204 | return ret; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int |
| 208 | partial_traverse(partialobject *pto, visitproc visit, void *arg) |
| 209 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 210 | Py_VISIT(pto->fn); |
| 211 | Py_VISIT(pto->args); |
| 212 | Py_VISIT(pto->kw); |
| 213 | Py_VISIT(pto->dict); |
| 214 | return 0; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | PyDoc_STRVAR(partial_doc, |
| 218 | "partial(func, *args, **keywords) - new function with partial application\n\ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 219 | of the given arguments and keywords.\n"); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 220 | |
| 221 | #define OFF(x) offsetof(partialobject, x) |
| 222 | static PyMemberDef partial_memberlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 223 | {"func", T_OBJECT, OFF(fn), READONLY, |
| 224 | "function object to use in future partial calls"}, |
| 225 | {"args", T_OBJECT, OFF(args), READONLY, |
| 226 | "tuple of arguments to future partial calls"}, |
| 227 | {"keywords", T_OBJECT, OFF(kw), READONLY, |
| 228 | "dictionary of keyword arguments to future partial calls"}, |
| 229 | {NULL} /* Sentinel */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 232 | static PyObject * |
| 233 | partial_get_dict(partialobject *pto) |
| 234 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 235 | if (pto->dict == NULL) { |
| 236 | pto->dict = PyDict_New(); |
| 237 | if (pto->dict == NULL) |
| 238 | return NULL; |
| 239 | } |
| 240 | Py_INCREF(pto->dict); |
| 241 | return pto->dict; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static int |
| 245 | partial_set_dict(partialobject *pto, PyObject *value) |
| 246 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 247 | PyObject *tmp; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 249 | /* It is illegal to del p.__dict__ */ |
| 250 | if (value == NULL) { |
| 251 | PyErr_SetString(PyExc_TypeError, |
| 252 | "a partial object's dictionary may not be deleted"); |
| 253 | return -1; |
| 254 | } |
| 255 | /* Can only set __dict__ to a dictionary */ |
| 256 | if (!PyDict_Check(value)) { |
| 257 | PyErr_SetString(PyExc_TypeError, |
| 258 | "setting partial object's dictionary to a non-dict"); |
| 259 | return -1; |
| 260 | } |
| 261 | tmp = pto->dict; |
| 262 | Py_INCREF(value); |
| 263 | pto->dict = value; |
| 264 | Py_XDECREF(tmp); |
| 265 | return 0; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Georg Brandl | c2fb6c7 | 2006-02-21 17:49:57 +0000 | [diff] [blame] | 268 | static PyGetSetDef partial_getsetlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 269 | {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict}, |
| 270 | {NULL} /* Sentinel */ |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 273 | /* Pickle strategy: |
| 274 | __reduce__ by itself doesn't support getting kwargs in the unpickle |
| 275 | operation so we define a __setstate__ that replaces all the information |
| 276 | 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] | 277 | it as a hook to do strange things. |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 278 | */ |
| 279 | |
| 280 | PyObject * |
| 281 | partial_reduce(partialobject *pto, PyObject *unused) |
| 282 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 283 | return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn, |
| 284 | pto->args, pto->kw, |
| 285 | pto->dict ? pto->dict : Py_None); |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | PyObject * |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 289 | partial_setstate(partialobject *pto, PyObject *state) |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 290 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 291 | PyObject *fn, *fnargs, *kw, *dict; |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 292 | if (!PyArg_ParseTuple(state, "OOOO", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 293 | &fn, &fnargs, &kw, &dict)) |
| 294 | return NULL; |
| 295 | Py_XDECREF(pto->fn); |
| 296 | Py_XDECREF(pto->args); |
| 297 | Py_XDECREF(pto->kw); |
| 298 | Py_XDECREF(pto->dict); |
| 299 | pto->fn = fn; |
| 300 | pto->args = fnargs; |
| 301 | pto->kw = kw; |
| 302 | if (dict != Py_None) { |
| 303 | pto->dict = dict; |
| 304 | Py_INCREF(dict); |
| 305 | } else { |
| 306 | pto->dict = NULL; |
| 307 | } |
| 308 | Py_INCREF(fn); |
| 309 | Py_INCREF(fnargs); |
| 310 | Py_INCREF(kw); |
| 311 | Py_RETURN_NONE; |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static PyMethodDef partial_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 315 | {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS}, |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 316 | {"__setstate__", (PyCFunction)partial_setstate, METH_O}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 317 | {NULL, NULL} /* sentinel */ |
Jack Diederich | d60c29e | 2009-03-31 23:46:48 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 320 | static PyTypeObject partial_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 321 | PyVarObject_HEAD_INIT(NULL, 0) |
| 322 | "functools.partial", /* tp_name */ |
| 323 | sizeof(partialobject), /* tp_basicsize */ |
| 324 | 0, /* tp_itemsize */ |
| 325 | /* methods */ |
| 326 | (destructor)partial_dealloc, /* tp_dealloc */ |
| 327 | 0, /* tp_print */ |
| 328 | 0, /* tp_getattr */ |
| 329 | 0, /* tp_setattr */ |
| 330 | 0, /* tp_compare */ |
| 331 | 0, /* tp_repr */ |
| 332 | 0, /* tp_as_number */ |
| 333 | 0, /* tp_as_sequence */ |
| 334 | 0, /* tp_as_mapping */ |
| 335 | 0, /* tp_hash */ |
| 336 | (ternaryfunc)partial_call, /* tp_call */ |
| 337 | 0, /* tp_str */ |
| 338 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 339 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 340 | 0, /* tp_as_buffer */ |
| 341 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 342 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 343 | partial_doc, /* tp_doc */ |
| 344 | (traverseproc)partial_traverse, /* tp_traverse */ |
| 345 | 0, /* tp_clear */ |
| 346 | 0, /* tp_richcompare */ |
| 347 | offsetof(partialobject, weakreflist), /* tp_weaklistoffset */ |
| 348 | 0, /* tp_iter */ |
| 349 | 0, /* tp_iternext */ |
| 350 | partial_methods, /* tp_methods */ |
| 351 | partial_memberlist, /* tp_members */ |
| 352 | partial_getsetlist, /* tp_getset */ |
| 353 | 0, /* tp_base */ |
| 354 | 0, /* tp_dict */ |
| 355 | 0, /* tp_descr_get */ |
| 356 | 0, /* tp_descr_set */ |
| 357 | offsetof(partialobject, dict), /* tp_dictoffset */ |
| 358 | 0, /* tp_init */ |
| 359 | 0, /* tp_alloc */ |
| 360 | partial_new, /* tp_new */ |
| 361 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | |
| 365 | /* module level code ********************************************************/ |
| 366 | |
| 367 | PyDoc_STRVAR(module_doc, |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 368 | "Tools that operate on functions."); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 369 | |
| 370 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 371 | {"reduce", functools_reduce, METH_VARARGS, reduce_doc}, |
| 372 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | PyMODINIT_FUNC |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 376 | init_functools(void) |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 377 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 378 | int i; |
| 379 | PyObject *m; |
| 380 | char *name; |
| 381 | PyTypeObject *typelist[] = { |
| 382 | &partial_type, |
| 383 | NULL |
| 384 | }; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 386 | m = Py_InitModule3("_functools", module_methods, module_doc); |
| 387 | if (m == NULL) |
| 388 | return; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 390 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 391 | if (PyType_Ready(typelist[i]) < 0) |
| 392 | return; |
| 393 | name = strchr(typelist[i]->tp_name, '.'); |
| 394 | assert (name != NULL); |
| 395 | Py_INCREF(typelist[i]); |
| 396 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
| 397 | } |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 398 | } |