blob: e0781a98d5e5b0ffe5aad2fd3ee27dc18689639e [file] [log] [blame]
Raymond Hettinger9c323f82005-02-28 19:39:44 +00001
2#include "Python.h"
3#include "structmember.h"
4
Antoine Pitrouc83ea132010-05-09 14:46:46 +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>
Nick Coghlanc649ec52006-05-29 12:43:05 +00008 Copyright (c) 2004, 2005, 2006 Python Software Foundation.
Raymond Hettinger9c323f82005-02-28 19:39:44 +00009 All rights reserved.
10*/
11
Brett Cannon83e81842008-08-09 23:30:55 +000012/* reduce() *************************************************************/
13
14static PyObject *
15functools_reduce(PyObject *self, PyObject *args)
16{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000017 PyObject *seq, *func, *result = NULL, *it;
Brett Cannon83e81842008-08-09 23:30:55 +000018
Antoine Pitrouc83ea132010-05-09 14:46:46 +000019 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
20 return NULL;
21 if (result != NULL)
22 Py_INCREF(result);
Brett Cannon83e81842008-08-09 23:30:55 +000023
Antoine Pitrouc83ea132010-05-09 14:46:46 +000024 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 Cannon83e81842008-08-09 23:30:55 +000031
Antoine Pitrouc83ea132010-05-09 14:46:46 +000032 if ((args = PyTuple_New(2)) == NULL)
33 goto Fail;
Brett Cannon83e81842008-08-09 23:30:55 +000034
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 for (;;) {
36 PyObject *op2;
Brett Cannon83e81842008-08-09 23:30:55 +000037
Antoine Pitrouc83ea132010-05-09 14:46:46 +000038 if (args->ob_refcnt > 1) {
39 Py_DECREF(args);
40 if ((args = PyTuple_New(2)) == NULL)
41 goto Fail;
42 }
Brett Cannon83e81842008-08-09 23:30:55 +000043
Antoine Pitrouc83ea132010-05-09 14:46:46 +000044 op2 = PyIter_Next(it);
45 if (op2 == NULL) {
46 if (PyErr_Occurred())
47 goto Fail;
48 break;
49 }
Brett Cannon83e81842008-08-09 23:30:55 +000050
Antoine Pitrouc83ea132010-05-09 14:46:46 +000051 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 Cannon83e81842008-08-09 23:30:55 +000060
Antoine Pitrouc83ea132010-05-09 14:46:46 +000061 Py_DECREF(args);
Brett Cannon83e81842008-08-09 23:30:55 +000062
Antoine Pitrouc83ea132010-05-09 14:46:46 +000063 if (result == NULL)
64 PyErr_SetString(PyExc_TypeError,
65 "reduce() of empty sequence with no initial value");
Brett Cannon83e81842008-08-09 23:30:55 +000066
Antoine Pitrouc83ea132010-05-09 14:46:46 +000067 Py_DECREF(it);
68 return result;
Brett Cannon83e81842008-08-09 23:30:55 +000069
70Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000071 Py_XDECREF(args);
72 Py_XDECREF(result);
73 Py_DECREF(it);
74 return NULL;
Brett Cannon83e81842008-08-09 23:30:55 +000075}
76
77PyDoc_STRVAR(reduce_doc,
78"reduce(function, sequence[, initial]) -> value\n\
79\n\
80Apply a function of two arguments cumulatively to the items of a sequence,\n\
81from left to right, so as to reduce the sequence to a single value.\n\
82For 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\
84of the sequence in the calculation, and serves as a default when the\n\
85sequence is empty.");
86
87
88
89
Raymond Hettinger9c323f82005-02-28 19:39:44 +000090/* partial object **********************************************************/
91
92typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000093 PyObject_HEAD
94 PyObject *fn;
95 PyObject *args;
96 PyObject *kw;
97 PyObject *dict;
98 PyObject *weakreflist; /* List of weak references */
Raymond Hettinger9c323f82005-02-28 19:39:44 +000099} partialobject;
100
101static PyTypeObject partial_type;
102
103static PyObject *
104partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
105{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000106 PyObject *func;
107 partialobject *pto;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000108
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000109 if (PyTuple_GET_SIZE(args) < 1) {
110 PyErr_SetString(PyExc_TypeError,
111 "type 'partial' takes at least one argument");
112 return NULL;
113 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000114
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000115 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 Hettinger9c323f82005-02-28 19:39:44 +0000121
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000122 /* create partialobject structure */
123 pto = (partialobject *)type->tp_alloc(type, 0);
124 if (pto == NULL)
125 return NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000126
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 pto->fn = func;
128 Py_INCREF(func);
129 pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
130 if (pto->args == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 Py_DECREF(pto);
132 return NULL;
133 }
Benjamin Peterson72c01412015-05-09 00:23:41 -0400134 pto->kw = (kw != NULL) ? PyDict_Copy(kw) : PyDict_New();
135 if (pto->kw == NULL) {
136 Py_DECREF(pto);
137 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000138 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000139
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000140 return (PyObject *)pto;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000141}
142
143static void
144partial_dealloc(partialobject *pto)
145{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000146 PyObject_GC_UnTrack(pto);
147 if (pto->weakreflist != NULL)
148 PyObject_ClearWeakRefs((PyObject *) pto);
149 Py_XDECREF(pto->fn);
150 Py_XDECREF(pto->args);
151 Py_XDECREF(pto->kw);
152 Py_XDECREF(pto->dict);
153 Py_TYPE(pto)->tp_free(pto);
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000154}
155
156static PyObject *
157partial_call(partialobject *pto, PyObject *args, PyObject *kw)
158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 PyObject *ret;
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200160 PyObject *argappl, *kwappl;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000161
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 assert (PyCallable_Check(pto->fn));
163 assert (PyTuple_Check(pto->args));
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200164 assert (PyDict_Check(pto->kw));
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000165
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166 if (PyTuple_GET_SIZE(pto->args) == 0) {
167 argappl = args;
168 Py_INCREF(args);
169 } else if (PyTuple_GET_SIZE(args) == 0) {
170 argappl = pto->args;
171 Py_INCREF(pto->args);
172 } else {
173 argappl = PySequence_Concat(pto->args, args);
174 if (argappl == NULL)
175 return NULL;
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200176 assert(PyTuple_Check(argappl));
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000178
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200179 if (PyDict_Size(pto->kw) == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000180 kwappl = kw;
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200181 Py_XINCREF(kwappl);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 } else {
183 kwappl = PyDict_Copy(pto->kw);
184 if (kwappl == NULL) {
185 Py_DECREF(argappl);
186 return NULL;
187 }
188 if (kw != NULL) {
189 if (PyDict_Merge(kwappl, kw, 1) != 0) {
190 Py_DECREF(argappl);
191 Py_DECREF(kwappl);
192 return NULL;
193 }
194 }
195 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000196
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 ret = PyObject_Call(pto->fn, argappl, kwappl);
198 Py_DECREF(argappl);
199 Py_XDECREF(kwappl);
200 return ret;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000201}
202
203static int
204partial_traverse(partialobject *pto, visitproc visit, void *arg)
205{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000206 Py_VISIT(pto->fn);
207 Py_VISIT(pto->args);
208 Py_VISIT(pto->kw);
209 Py_VISIT(pto->dict);
210 return 0;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000211}
212
213PyDoc_STRVAR(partial_doc,
214"partial(func, *args, **keywords) - new function with partial application\n\
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 of the given arguments and keywords.\n");
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000216
217#define OFF(x) offsetof(partialobject, x)
218static PyMemberDef partial_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 {"func", T_OBJECT, OFF(fn), READONLY,
220 "function object to use in future partial calls"},
221 {"args", T_OBJECT, OFF(args), READONLY,
222 "tuple of arguments to future partial calls"},
223 {"keywords", T_OBJECT, OFF(kw), READONLY,
224 "dictionary of keyword arguments to future partial calls"},
225 {NULL} /* Sentinel */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000226};
227
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000228static PyObject *
229partial_get_dict(partialobject *pto)
230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 if (pto->dict == NULL) {
232 pto->dict = PyDict_New();
233 if (pto->dict == NULL)
234 return NULL;
235 }
236 Py_INCREF(pto->dict);
237 return pto->dict;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000238}
239
240static int
241partial_set_dict(partialobject *pto, PyObject *value)
242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 PyObject *tmp;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000244
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 /* It is illegal to del p.__dict__ */
246 if (value == NULL) {
247 PyErr_SetString(PyExc_TypeError,
248 "a partial object's dictionary may not be deleted");
249 return -1;
250 }
251 /* Can only set __dict__ to a dictionary */
252 if (!PyDict_Check(value)) {
253 PyErr_SetString(PyExc_TypeError,
254 "setting partial object's dictionary to a non-dict");
255 return -1;
256 }
257 tmp = pto->dict;
258 Py_INCREF(value);
259 pto->dict = value;
260 Py_XDECREF(tmp);
261 return 0;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000262}
263
Georg Brandlc2fb6c72006-02-21 17:49:57 +0000264static PyGetSetDef partial_getsetlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
266 {NULL} /* Sentinel */
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000267};
268
Jack Diederichd60c29e2009-03-31 23:46:48 +0000269/* Pickle strategy:
270 __reduce__ by itself doesn't support getting kwargs in the unpickle
271 operation so we define a __setstate__ that replaces all the information
272 about the partial. If we only replaced part of it someone would use
Ezio Melottic2077b02011-03-16 12:34:31 +0200273 it as a hook to do strange things.
Jack Diederichd60c29e2009-03-31 23:46:48 +0000274 */
275
276PyObject *
277partial_reduce(partialobject *pto, PyObject *unused)
278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
280 pto->args, pto->kw,
281 pto->dict ? pto->dict : Py_None);
Jack Diederichd60c29e2009-03-31 23:46:48 +0000282}
283
284PyObject *
Serhiy Storchakaa07a8b42013-02-04 12:45:46 +0200285partial_setstate(partialobject *pto, PyObject *state)
Jack Diederichd60c29e2009-03-31 23:46:48 +0000286{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 PyObject *fn, *fnargs, *kw, *dict;
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200288
289 if (!PyTuple_Check(state) ||
290 !PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) ||
291 !PyCallable_Check(fn) ||
292 !PyTuple_Check(fnargs) ||
293 (kw != Py_None && !PyDict_Check(kw)))
294 {
295 PyErr_SetString(PyExc_TypeError, "invalid partial state");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 }
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200298
299 if(!PyTuple_CheckExact(fnargs))
300 fnargs = PySequence_Tuple(fnargs);
301 else
302 Py_INCREF(fnargs);
303 if (fnargs == NULL)
304 return NULL;
305
306 if (kw == Py_None)
307 kw = PyDict_New();
308 else if(!PyDict_CheckExact(kw))
309 kw = PyDict_Copy(kw);
310 else
311 Py_INCREF(kw);
312 if (kw == NULL) {
313 Py_DECREF(fnargs);
314 return NULL;
315 }
316
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000317 Py_INCREF(fn);
Serhiy Storchaka71b71762016-02-02 18:45:59 +0200318 if (dict == Py_None)
319 dict = NULL;
320 else
321 Py_INCREF(dict);
322
Serhiy Storchakabb650632016-04-11 09:53:37 +0300323 Py_SETREF(pto->fn, fn);
324 Py_SETREF(pto->args, fnargs);
325 Py_SETREF(pto->kw, kw);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300326 Py_XSETREF(pto->dict, dict);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000327 Py_RETURN_NONE;
Jack Diederichd60c29e2009-03-31 23:46:48 +0000328}
329
330static PyMethodDef partial_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
Serhiy Storchakaa07a8b42013-02-04 12:45:46 +0200332 {"__setstate__", (PyCFunction)partial_setstate, METH_O},
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000333 {NULL, NULL} /* sentinel */
Jack Diederichd60c29e2009-03-31 23:46:48 +0000334};
335
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000336static PyTypeObject partial_type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 PyVarObject_HEAD_INIT(NULL, 0)
338 "functools.partial", /* tp_name */
339 sizeof(partialobject), /* tp_basicsize */
340 0, /* tp_itemsize */
341 /* methods */
342 (destructor)partial_dealloc, /* tp_dealloc */
343 0, /* tp_print */
344 0, /* tp_getattr */
345 0, /* tp_setattr */
346 0, /* tp_compare */
347 0, /* tp_repr */
348 0, /* tp_as_number */
349 0, /* tp_as_sequence */
350 0, /* tp_as_mapping */
351 0, /* tp_hash */
352 (ternaryfunc)partial_call, /* tp_call */
353 0, /* tp_str */
354 PyObject_GenericGetAttr, /* tp_getattro */
355 PyObject_GenericSetAttr, /* tp_setattro */
356 0, /* tp_as_buffer */
357 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
358 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
359 partial_doc, /* tp_doc */
360 (traverseproc)partial_traverse, /* tp_traverse */
361 0, /* tp_clear */
362 0, /* tp_richcompare */
363 offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
364 0, /* tp_iter */
365 0, /* tp_iternext */
366 partial_methods, /* tp_methods */
367 partial_memberlist, /* tp_members */
368 partial_getsetlist, /* tp_getset */
369 0, /* tp_base */
370 0, /* tp_dict */
371 0, /* tp_descr_get */
372 0, /* tp_descr_set */
373 offsetof(partialobject, dict), /* tp_dictoffset */
374 0, /* tp_init */
375 0, /* tp_alloc */
376 partial_new, /* tp_new */
377 PyObject_GC_Del, /* tp_free */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000378};
379
380
381/* module level code ********************************************************/
382
383PyDoc_STRVAR(module_doc,
Nick Coghlanc649ec52006-05-29 12:43:05 +0000384"Tools that operate on functions.");
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000385
386static PyMethodDef module_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000387 {"reduce", functools_reduce, METH_VARARGS, reduce_doc},
388 {NULL, NULL} /* sentinel */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000389};
390
391PyMODINIT_FUNC
Nick Coghlanc649ec52006-05-29 12:43:05 +0000392init_functools(void)
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 int i;
395 PyObject *m;
396 char *name;
397 PyTypeObject *typelist[] = {
398 &partial_type,
399 NULL
400 };
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000401
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000402 m = Py_InitModule3("_functools", module_methods, module_doc);
403 if (m == NULL)
404 return;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000405
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000406 for (i=0 ; typelist[i] != NULL ; i++) {
407 if (PyType_Ready(typelist[i]) < 0)
408 return;
409 name = strchr(typelist[i]->tp_name, '.');
410 assert (name != NULL);
411 Py_INCREF(typelist[i]);
412 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
413 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000414}