blob: 34373539336919c59e040172746a942b35e46fe5 [file] [log] [blame]
Raymond Hettinger9c323f82005-02-28 19:39:44 +00001
2#include "Python.h"
3#include "structmember.h"
4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005/* _functools module written and maintained
Raymond Hettinger9c323f82005-02-28 19:39:44 +00006 by Hye-Shik Chang <perky@FreeBSD.org>
7 with adaptations by Raymond Hettinger <python@rcn.com>
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00008 Copyright (c) 2004, 2005, 2006 Python Software Foundation.
Raymond Hettinger9c323f82005-02-28 19:39:44 +00009 All rights reserved.
10*/
11
12/* partial object **********************************************************/
13
14typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000015 PyObject_HEAD
16 PyObject *fn;
17 PyObject *args;
18 PyObject *kw;
19 PyObject *dict;
20 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger9c323f82005-02-28 19:39:44 +000021} partialobject;
22
23static PyTypeObject partial_type;
24
25static PyObject *
26partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
27{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000028 PyObject *func;
29 partialobject *pto;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000030
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000031 if (PyTuple_GET_SIZE(args) < 1) {
32 PyErr_SetString(PyExc_TypeError,
33 "type 'partial' takes at least one argument");
34 return NULL;
35 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +000036
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000037 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 Hettinger9c323f82005-02-28 19:39:44 +000043
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000044 /* create partialobject structure */
45 pto = (partialobject *)type->tp_alloc(type, 0);
46 if (pto == NULL)
47 return NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000048
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000049 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 Hettinger9c323f82005-02-28 19:39:44 +000067
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000068 pto->weakreflist = NULL;
69 pto->dict = NULL;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +000070
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000071 return (PyObject *)pto;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000072}
73
74static void
75partial_dealloc(partialobject *pto)
76{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000077 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 Hettinger9c323f82005-02-28 19:39:44 +000085}
86
87static PyObject *
88partial_call(partialobject *pto, PyObject *args, PyObject *kw)
89{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000090 PyObject *ret;
91 PyObject *argappl = NULL, *kwappl = NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000092
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000093 assert (PyCallable_Check(pto->fn));
94 assert (PyTuple_Check(pto->args));
95 assert (pto->kw == Py_None || PyDict_Check(pto->kw));
Raymond Hettinger9c323f82005-02-28 19:39:44 +000096
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 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 Hettinger9c323f82005-02-28 19:39:44 +0000108
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000109 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 Hettinger9c323f82005-02-28 19:39:44 +0000126
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000127 ret = PyObject_Call(pto->fn, argappl, kwappl);
128 Py_DECREF(argappl);
129 Py_XDECREF(kwappl);
130 return ret;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000131}
132
133static int
134partial_traverse(partialobject *pto, visitproc visit, void *arg)
135{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000136 Py_VISIT(pto->fn);
137 Py_VISIT(pto->args);
138 Py_VISIT(pto->kw);
139 Py_VISIT(pto->dict);
140 return 0;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000141}
142
143PyDoc_STRVAR(partial_doc,
144"partial(func, *args, **keywords) - new function with partial application\n\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000145 of the given arguments and keywords.\n");
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000146
147#define OFF(x) offsetof(partialobject, x)
148static PyMemberDef partial_memberlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 {"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 Hettinger9c323f82005-02-28 19:39:44 +0000156};
157
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000158static PyObject *
159partial_get_dict(partialobject *pto)
160{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 if (pto->dict == NULL) {
162 pto->dict = PyDict_New();
163 if (pto->dict == NULL)
164 return NULL;
165 }
166 Py_INCREF(pto->dict);
167 return pto->dict;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000168}
169
170static int
171partial_set_dict(partialobject *pto, PyObject *value)
172{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000173 PyObject *tmp;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000174
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 /* It is illegal to del p.__dict__ */
176 if (value == NULL) {
177 PyErr_SetString(PyExc_TypeError,
178 "a partial object's dictionary may not be deleted");
179 return -1;
180 }
181 /* Can only set __dict__ to a dictionary */
182 if (!PyDict_Check(value)) {
183 PyErr_SetString(PyExc_TypeError,
184 "setting partial object's dictionary to a non-dict");
185 return -1;
186 }
187 tmp = pto->dict;
188 Py_INCREF(value);
189 pto->dict = value;
190 Py_XDECREF(tmp);
191 return 0;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000192}
193
Georg Brandlc2fb6c72006-02-21 17:49:57 +0000194static PyGetSetDef partial_getsetlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
196 {NULL} /* Sentinel */
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000197};
198
Jack Diederiche0cbd692009-04-01 04:27:09 +0000199/* Pickle strategy:
200 __reduce__ by itself doesn't support getting kwargs in the unpickle
201 operation so we define a __setstate__ that replaces all the information
202 about the partial. If we only replaced part of it someone would use
203 it as a hook to do stange things.
204 */
205
Antoine Pitrou69f71142009-05-24 21:25:49 +0000206static PyObject *
Jack Diederiche0cbd692009-04-01 04:27:09 +0000207partial_reduce(partialobject *pto, PyObject *unused)
208{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
210 pto->args, pto->kw,
211 pto->dict ? pto->dict : Py_None);
Jack Diederiche0cbd692009-04-01 04:27:09 +0000212}
213
Antoine Pitrou69f71142009-05-24 21:25:49 +0000214static PyObject *
Jack Diederiche0cbd692009-04-01 04:27:09 +0000215partial_setstate(partialobject *pto, PyObject *args)
216{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 PyObject *fn, *fnargs, *kw, *dict;
218 if (!PyArg_ParseTuple(args, "(OOOO):__setstate__",
219 &fn, &fnargs, &kw, &dict))
220 return NULL;
221 Py_XDECREF(pto->fn);
222 Py_XDECREF(pto->args);
223 Py_XDECREF(pto->kw);
224 Py_XDECREF(pto->dict);
225 pto->fn = fn;
226 pto->args = fnargs;
227 pto->kw = kw;
228 if (dict != Py_None) {
229 pto->dict = dict;
230 Py_INCREF(dict);
231 } else {
232 pto->dict = NULL;
233 }
234 Py_INCREF(fn);
235 Py_INCREF(fnargs);
236 Py_INCREF(kw);
237 Py_RETURN_NONE;
Jack Diederiche0cbd692009-04-01 04:27:09 +0000238}
239
240static PyMethodDef partial_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000241 {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
242 {"__setstate__", (PyCFunction)partial_setstate, METH_VARARGS},
243 {NULL, NULL} /* sentinel */
Jack Diederiche0cbd692009-04-01 04:27:09 +0000244};
245
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000246static PyTypeObject partial_type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000247 PyVarObject_HEAD_INIT(NULL, 0)
248 "functools.partial", /* tp_name */
249 sizeof(partialobject), /* tp_basicsize */
250 0, /* tp_itemsize */
251 /* methods */
252 (destructor)partial_dealloc, /* tp_dealloc */
253 0, /* tp_print */
254 0, /* tp_getattr */
255 0, /* tp_setattr */
256 0, /* tp_reserved */
257 0, /* tp_repr */
258 0, /* tp_as_number */
259 0, /* tp_as_sequence */
260 0, /* tp_as_mapping */
261 0, /* tp_hash */
262 (ternaryfunc)partial_call, /* tp_call */
263 0, /* tp_str */
264 PyObject_GenericGetAttr, /* tp_getattro */
265 PyObject_GenericSetAttr, /* tp_setattro */
266 0, /* tp_as_buffer */
267 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
268 Py_TPFLAGS_BASETYPE, /* tp_flags */
269 partial_doc, /* tp_doc */
270 (traverseproc)partial_traverse, /* tp_traverse */
271 0, /* tp_clear */
272 0, /* tp_richcompare */
273 offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
274 0, /* tp_iter */
275 0, /* tp_iternext */
276 partial_methods, /* tp_methods */
277 partial_memberlist, /* tp_members */
278 partial_getsetlist, /* tp_getset */
279 0, /* tp_base */
280 0, /* tp_dict */
281 0, /* tp_descr_get */
282 0, /* tp_descr_set */
283 offsetof(partialobject, dict), /* tp_dictoffset */
284 0, /* tp_init */
285 0, /* tp_alloc */
286 partial_new, /* tp_new */
287 PyObject_GC_Del, /* tp_free */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000288};
289
290
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000291/* reduce (used to be a builtin) ********************************************/
292
293static PyObject *
294functools_reduce(PyObject *self, PyObject *args)
295{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000296 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000297
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
299 return NULL;
300 if (result != NULL)
301 Py_INCREF(result);
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000302
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 it = PyObject_GetIter(seq);
304 if (it == NULL) {
Alexander Belopolsky7a9bdbc2010-08-16 19:46:32 +0000305 if (PyErr_ExceptionMatches(PyExc_TypeError))
306 PyErr_SetString(PyExc_TypeError,
307 "reduce() arg 2 must support iteration");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000308 Py_XDECREF(result);
309 return NULL;
310 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000311
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 if ((args = PyTuple_New(2)) == NULL)
313 goto Fail;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000314
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000315 for (;;) {
316 PyObject *op2;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000317
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000318 if (args->ob_refcnt > 1) {
319 Py_DECREF(args);
320 if ((args = PyTuple_New(2)) == NULL)
321 goto Fail;
322 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000323
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000324 op2 = PyIter_Next(it);
325 if (op2 == NULL) {
326 if (PyErr_Occurred())
327 goto Fail;
328 break;
329 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000330
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 if (result == NULL)
332 result = op2;
333 else {
334 PyTuple_SetItem(args, 0, result);
335 PyTuple_SetItem(args, 1, op2);
336 if ((result = PyEval_CallObject(func, args)) == NULL)
337 goto Fail;
338 }
339 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000340
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000341 Py_DECREF(args);
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000342
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000343 if (result == NULL)
344 PyErr_SetString(PyExc_TypeError,
345 "reduce() of empty sequence with no initial value");
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000346
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 Py_DECREF(it);
348 return result;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000349
350Fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 Py_XDECREF(args);
352 Py_XDECREF(result);
353 Py_DECREF(it);
354 return NULL;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000355}
356
357PyDoc_STRVAR(functools_reduce_doc,
358"reduce(function, sequence[, initial]) -> value\n\
359\n\
360Apply a function of two arguments cumulatively to the items of a sequence,\n\
361from left to right, so as to reduce the sequence to a single value.\n\
362For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
363((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
364of the sequence in the calculation, and serves as a default when the\n\
365sequence is empty.");
366
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000367/* module level code ********************************************************/
368
369PyDoc_STRVAR(module_doc,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000370"Tools that operate on functions.");
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000371
372static PyMethodDef module_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
374 {NULL, NULL} /* sentinel */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000375};
376
Martin v. Löwis1a214512008-06-11 05:26:20 +0000377
378static struct PyModuleDef _functoolsmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 PyModuleDef_HEAD_INIT,
380 "_functools",
381 module_doc,
382 -1,
383 module_methods,
384 NULL,
385 NULL,
386 NULL,
387 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000388};
389
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000390PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000391PyInit__functools(void)
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000392{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000393 int i;
394 PyObject *m;
395 char *name;
396 PyTypeObject *typelist[] = {
397 &partial_type,
398 NULL
399 };
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000400
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 m = PyModule_Create(&_functoolsmodule);
402 if (m == NULL)
403 return NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000404
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 for (i=0 ; typelist[i] != NULL ; i++) {
406 if (PyType_Ready(typelist[i]) < 0) {
407 Py_DECREF(m);
408 return NULL;
409 }
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 }
415 return m;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000416}