blob: 369eb5cc5a44b190fa23c4678eac7042db239e30 [file] [log] [blame]
Raymond Hettinger9c323f82005-02-28 19:39:44 +00001
2#include "Python.h"
3#include "structmember.h"
4
Thomas Wouters4d70c3d2006-06-08 14:42:34 +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 {
15 PyObject_HEAD
16 PyObject *fn;
17 PyObject *args;
18 PyObject *kw;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +000019 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{
28 PyObject *func;
29 partialobject *pto;
30
31 if (PyTuple_GET_SIZE(args) < 1) {
32 PyErr_SetString(PyExc_TypeError,
33 "type 'partial' takes at least one argument");
34 return NULL;
35 }
36
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 }
43
44 /* create partialobject structure */
45 pto = (partialobject *)type->tp_alloc(type, 0);
46 if (pto == NULL)
47 return NULL;
48
49 pto->fn = func;
50 Py_INCREF(func);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051 pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
Raymond Hettinger9c323f82005-02-28 19:39:44 +000052 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 }
67
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +000068 pto->weakreflist = NULL;
69 pto->dict = NULL;
70
Raymond Hettinger9c323f82005-02-28 19:39:44 +000071 return (PyObject *)pto;
72}
73
74static void
75partial_dealloc(partialobject *pto)
76{
77 PyObject_GC_UnTrack(pto);
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +000078 if (pto->weakreflist != NULL)
79 PyObject_ClearWeakRefs((PyObject *) pto);
Raymond Hettinger9c323f82005-02-28 19:39:44 +000080 Py_XDECREF(pto->fn);
81 Py_XDECREF(pto->args);
82 Py_XDECREF(pto->kw);
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +000083 Py_XDECREF(pto->dict);
Christian Heimes90aa7642007-12-19 02:45:37 +000084 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{
90 PyObject *ret;
91 PyObject *argappl = NULL, *kwappl = NULL;
92
93 assert (PyCallable_Check(pto->fn));
94 assert (PyTuple_Check(pto->args));
95 assert (pto->kw == Py_None || PyDict_Check(pto->kw));
96
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 }
108
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 }
126
127 ret = PyObject_Call(pto->fn, argappl, kwappl);
128 Py_DECREF(argappl);
129 Py_XDECREF(kwappl);
130 return ret;
131}
132
133static int
134partial_traverse(partialobject *pto, visitproc visit, void *arg)
135{
136 Py_VISIT(pto->fn);
137 Py_VISIT(pto->args);
138 Py_VISIT(pto->kw);
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000139 Py_VISIT(pto->dict);
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000140 return 0;
141}
142
143PyDoc_STRVAR(partial_doc,
144"partial(func, *args, **keywords) - new function with partial application\n\
145 of the given arguments and keywords.\n");
146
147#define OFF(x) offsetof(partialobject, x)
148static PyMemberDef partial_memberlist[] = {
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 */
156};
157
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000158static PyObject *
159partial_get_dict(partialobject *pto)
160{
161 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;
168}
169
170static int
171partial_set_dict(partialobject *pto, PyObject *value)
172{
173 PyObject *tmp;
174
175 /* 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;
192}
193
Georg Brandlc2fb6c72006-02-21 17:49:57 +0000194static PyGetSetDef partial_getsetlist[] = {
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000195 {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
196 {NULL} /* Sentinel */
197};
198
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000199static PyTypeObject partial_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000200 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000201 "functools.partial", /* tp_name */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000202 sizeof(partialobject), /* tp_basicsize */
203 0, /* tp_itemsize */
204 /* methods */
205 (destructor)partial_dealloc, /* tp_dealloc */
206 0, /* tp_print */
207 0, /* tp_getattr */
208 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000209 0, /* tp_reserved */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000210 0, /* tp_repr */
211 0, /* tp_as_number */
212 0, /* tp_as_sequence */
213 0, /* tp_as_mapping */
214 0, /* tp_hash */
215 (ternaryfunc)partial_call, /* tp_call */
216 0, /* tp_str */
217 PyObject_GenericGetAttr, /* tp_getattro */
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000218 PyObject_GenericSetAttr, /* tp_setattro */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000219 0, /* tp_as_buffer */
220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000221 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000222 partial_doc, /* tp_doc */
223 (traverseproc)partial_traverse, /* tp_traverse */
224 0, /* tp_clear */
225 0, /* tp_richcompare */
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000226 offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000227 0, /* tp_iter */
228 0, /* tp_iternext */
229 0, /* tp_methods */
230 partial_memberlist, /* tp_members */
Georg Brandlc2fb6c72006-02-21 17:49:57 +0000231 partial_getsetlist, /* tp_getset */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000232 0, /* tp_base */
233 0, /* tp_dict */
234 0, /* tp_descr_get */
235 0, /* tp_descr_set */
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000236 offsetof(partialobject, dict), /* tp_dictoffset */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000237 0, /* tp_init */
238 0, /* tp_alloc */
239 partial_new, /* tp_new */
240 PyObject_GC_Del, /* tp_free */
241};
242
243
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000244/* reduce (used to be a builtin) ********************************************/
245
246static PyObject *
247functools_reduce(PyObject *self, PyObject *args)
248{
249 PyObject *seq, *func, *result = NULL, *it;
250
251 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
252 return NULL;
253 if (result != NULL)
254 Py_INCREF(result);
255
256 it = PyObject_GetIter(seq);
257 if (it == NULL) {
258 PyErr_SetString(PyExc_TypeError,
259 "reduce() arg 2 must support iteration");
260 Py_XDECREF(result);
261 return NULL;
262 }
263
264 if ((args = PyTuple_New(2)) == NULL)
265 goto Fail;
266
267 for (;;) {
268 PyObject *op2;
269
270 if (args->ob_refcnt > 1) {
271 Py_DECREF(args);
272 if ((args = PyTuple_New(2)) == NULL)
273 goto Fail;
274 }
275
276 op2 = PyIter_Next(it);
277 if (op2 == NULL) {
278 if (PyErr_Occurred())
279 goto Fail;
280 break;
281 }
282
283 if (result == NULL)
284 result = op2;
285 else {
286 PyTuple_SetItem(args, 0, result);
287 PyTuple_SetItem(args, 1, op2);
288 if ((result = PyEval_CallObject(func, args)) == NULL)
289 goto Fail;
290 }
291 }
292
293 Py_DECREF(args);
294
295 if (result == NULL)
296 PyErr_SetString(PyExc_TypeError,
297 "reduce() of empty sequence with no initial value");
298
299 Py_DECREF(it);
300 return result;
301
302Fail:
303 Py_XDECREF(args);
304 Py_XDECREF(result);
305 Py_DECREF(it);
306 return NULL;
307}
308
309PyDoc_STRVAR(functools_reduce_doc,
310"reduce(function, sequence[, initial]) -> value\n\
311\n\
312Apply a function of two arguments cumulatively to the items of a sequence,\n\
313from left to right, so as to reduce the sequence to a single value.\n\
314For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
315((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
316of the sequence in the calculation, and serves as a default when the\n\
317sequence is empty.");
318
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000319/* module level code ********************************************************/
320
321PyDoc_STRVAR(module_doc,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000322"Tools that operate on functions.");
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000323
324static PyMethodDef module_methods[] = {
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000325 {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000326 {NULL, NULL} /* sentinel */
327};
328
Martin v. Löwis1a214512008-06-11 05:26:20 +0000329
330static struct PyModuleDef _functoolsmodule = {
331 PyModuleDef_HEAD_INIT,
332 "_functools",
333 module_doc,
334 -1,
335 module_methods,
336 NULL,
337 NULL,
338 NULL,
339 NULL
340};
341
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000342PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000343PyInit__functools(void)
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000344{
345 int i;
346 PyObject *m;
347 char *name;
348 PyTypeObject *typelist[] = {
349 &partial_type,
350 NULL
351 };
352
Martin v. Löwis1a214512008-06-11 05:26:20 +0000353 m = PyModule_Create(&_functoolsmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000354 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000355 return NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000356
357 for (i=0 ; typelist[i] != NULL ; i++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +0000358 if (PyType_Ready(typelist[i]) < 0) {
359 Py_DECREF(m);
360 return NULL;
361 }
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000362 name = strchr(typelist[i]->tp_name, '.');
363 assert (name != NULL);
364 Py_INCREF(typelist[i]);
365 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
366 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000367 return m;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000368}