blob: 311c6df82e2f5f330e27fd6179241fa5be0a964b [file] [log] [blame]
Raymond Hettinger9c323f82005-02-28 19:39:44 +00001
2#include "Python.h"
3#include "structmember.h"
4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000028 PyObject *func;
29 partialobject *pto;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000068 pto->weakreflist = NULL;
69 pto->dict = NULL;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 return (PyObject *)pto;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000072}
73
74static void
75partial_dealloc(partialobject *pto)
76{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000090 PyObject *ret;
91 PyObject *argappl = NULL, *kwappl = NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000173 PyObject *tmp;
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000195 {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
196 {NULL} /* Sentinel */
Raymond Hettingerc8b6d1b2005-03-08 06:14:50 +0000197};
198
Alexander Belopolsky41e422a2010-12-01 20:05:49 +0000199static PyObject *
200partial_repr(partialobject *pto)
201{
202 PyObject *result;
203 PyObject *arglist;
204 PyObject *tmp;
205 Py_ssize_t i, n;
206
207 arglist = PyUnicode_FromString("");
208 if (arglist == NULL) {
209 return NULL;
210 }
211 /* Pack positional arguments */
212 assert (PyTuple_Check(pto->args));
213 n = PyTuple_GET_SIZE(pto->args);
214 for (i = 0; i < n; i++) {
215 tmp = PyUnicode_FromFormat("%U, %R", arglist,
216 PyTuple_GET_ITEM(pto->args, i));
217 Py_DECREF(arglist);
218 if (tmp == NULL)
219 return NULL;
220 arglist = tmp;
221 }
222 /* Pack keyword arguments */
223 assert (pto->kw == Py_None || PyDict_Check(pto->kw));
224 if (pto->kw != Py_None) {
225 PyObject *key, *value;
226 for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) {
227 tmp = PyUnicode_FromFormat("%U, %U=%R", arglist,
228 key, value);
229 Py_DECREF(arglist);
230 if (tmp == NULL)
231 return NULL;
232 arglist = tmp;
233 }
234 }
235 result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name,
236 pto->fn, arglist);
237 Py_DECREF(arglist);
238 return result;
239}
240
Jack Diederiche0cbd692009-04-01 04:27:09 +0000241/* Pickle strategy:
242 __reduce__ by itself doesn't support getting kwargs in the unpickle
243 operation so we define a __setstate__ that replaces all the information
244 about the partial. If we only replaced part of it someone would use
Ezio Melotti13925002011-03-16 11:05:33 +0200245 it as a hook to do strange things.
Jack Diederiche0cbd692009-04-01 04:27:09 +0000246 */
247
Antoine Pitrou69f71142009-05-24 21:25:49 +0000248static PyObject *
Jack Diederiche0cbd692009-04-01 04:27:09 +0000249partial_reduce(partialobject *pto, PyObject *unused)
250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
252 pto->args, pto->kw,
253 pto->dict ? pto->dict : Py_None);
Jack Diederiche0cbd692009-04-01 04:27:09 +0000254}
255
Antoine Pitrou69f71142009-05-24 21:25:49 +0000256static PyObject *
Jack Diederiche0cbd692009-04-01 04:27:09 +0000257partial_setstate(partialobject *pto, PyObject *args)
258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyObject *fn, *fnargs, *kw, *dict;
260 if (!PyArg_ParseTuple(args, "(OOOO):__setstate__",
261 &fn, &fnargs, &kw, &dict))
262 return NULL;
263 Py_XDECREF(pto->fn);
264 Py_XDECREF(pto->args);
265 Py_XDECREF(pto->kw);
266 Py_XDECREF(pto->dict);
267 pto->fn = fn;
268 pto->args = fnargs;
269 pto->kw = kw;
270 if (dict != Py_None) {
271 pto->dict = dict;
272 Py_INCREF(dict);
273 } else {
274 pto->dict = NULL;
275 }
276 Py_INCREF(fn);
277 Py_INCREF(fnargs);
278 Py_INCREF(kw);
279 Py_RETURN_NONE;
Jack Diederiche0cbd692009-04-01 04:27:09 +0000280}
281
282static PyMethodDef partial_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
284 {"__setstate__", (PyCFunction)partial_setstate, METH_VARARGS},
285 {NULL, NULL} /* sentinel */
Jack Diederiche0cbd692009-04-01 04:27:09 +0000286};
287
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000288static PyTypeObject partial_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 PyVarObject_HEAD_INIT(NULL, 0)
290 "functools.partial", /* tp_name */
291 sizeof(partialobject), /* tp_basicsize */
292 0, /* tp_itemsize */
293 /* methods */
294 (destructor)partial_dealloc, /* tp_dealloc */
295 0, /* tp_print */
296 0, /* tp_getattr */
297 0, /* tp_setattr */
298 0, /* tp_reserved */
Alexander Belopolsky41e422a2010-12-01 20:05:49 +0000299 (reprfunc)partial_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 0, /* tp_as_number */
301 0, /* tp_as_sequence */
302 0, /* tp_as_mapping */
303 0, /* tp_hash */
304 (ternaryfunc)partial_call, /* tp_call */
305 0, /* tp_str */
306 PyObject_GenericGetAttr, /* tp_getattro */
307 PyObject_GenericSetAttr, /* tp_setattro */
308 0, /* tp_as_buffer */
309 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
310 Py_TPFLAGS_BASETYPE, /* tp_flags */
311 partial_doc, /* tp_doc */
312 (traverseproc)partial_traverse, /* tp_traverse */
313 0, /* tp_clear */
314 0, /* tp_richcompare */
315 offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
316 0, /* tp_iter */
317 0, /* tp_iternext */
318 partial_methods, /* tp_methods */
319 partial_memberlist, /* tp_members */
320 partial_getsetlist, /* tp_getset */
321 0, /* tp_base */
322 0, /* tp_dict */
323 0, /* tp_descr_get */
324 0, /* tp_descr_set */
325 offsetof(partialobject, dict), /* tp_dictoffset */
326 0, /* tp_init */
327 0, /* tp_alloc */
328 partial_new, /* tp_new */
329 PyObject_GC_Del, /* tp_free */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000330};
331
332
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700333/* cmp_to_key ***************************************************************/
334
335typedef struct {
Victor Stinner446c8d52011-04-05 12:21:35 +0200336 PyObject_HEAD
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700337 PyObject *cmp;
338 PyObject *object;
339} keyobject;
340
341static void
342keyobject_dealloc(keyobject *ko)
343{
344 Py_DECREF(ko->cmp);
345 Py_XDECREF(ko->object);
346 PyObject_FREE(ko);
347}
348
349static int
350keyobject_traverse(keyobject *ko, visitproc visit, void *arg)
351{
352 Py_VISIT(ko->cmp);
353 if (ko->object)
354 Py_VISIT(ko->object);
355 return 0;
356}
357
358static PyMemberDef keyobject_members[] = {
359 {"obj", T_OBJECT,
360 offsetof(keyobject, object), 0,
361 PyDoc_STR("Value wrapped by a key function.")},
362 {NULL}
363};
364
365static PyObject *
366keyobject_call(keyobject *ko, PyObject *args, PyObject *kw);
367
368static PyObject *
369keyobject_richcompare(PyObject *ko, PyObject *other, int op);
370
371static PyTypeObject keyobject_type = {
372 PyVarObject_HEAD_INIT(&PyType_Type, 0)
373 "functools.KeyWrapper", /* tp_name */
374 sizeof(keyobject), /* tp_basicsize */
375 0, /* tp_itemsize */
376 /* methods */
377 (destructor)keyobject_dealloc, /* tp_dealloc */
378 0, /* tp_print */
379 0, /* tp_getattr */
380 0, /* tp_setattr */
381 0, /* tp_reserved */
382 0, /* tp_repr */
383 0, /* tp_as_number */
384 0, /* tp_as_sequence */
385 0, /* tp_as_mapping */
386 0, /* tp_hash */
387 (ternaryfunc)keyobject_call, /* tp_call */
388 0, /* tp_str */
389 PyObject_GenericGetAttr, /* tp_getattro */
390 0, /* tp_setattro */
391 0, /* tp_as_buffer */
392 Py_TPFLAGS_DEFAULT, /* tp_flags */
393 0, /* tp_doc */
394 (traverseproc)keyobject_traverse, /* tp_traverse */
395 0, /* tp_clear */
396 keyobject_richcompare, /* tp_richcompare */
397 0, /* tp_weaklistoffset */
398 0, /* tp_iter */
399 0, /* tp_iternext */
400 0, /* tp_methods */
401 keyobject_members, /* tp_members */
402 0, /* tp_getset */
403};
404
405static PyObject *
406keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
407{
408 PyObject *object;
409 keyobject *result;
410 static char *kwargs[] = {"obj", NULL};
411
412 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwargs, &object))
413 return NULL;
414 result = PyObject_New(keyobject, &keyobject_type);
415 if (!result)
416 return NULL;
417 Py_INCREF(ko->cmp);
418 result->cmp = ko->cmp;
419 Py_INCREF(object);
420 result->object = object;
421 return (PyObject *)result;
422}
423
424static PyObject *
425keyobject_richcompare(PyObject *ko, PyObject *other, int op)
426{
427 PyObject *res;
428 PyObject *args;
429 PyObject *x;
430 PyObject *y;
431 PyObject *compare;
432 PyObject *answer;
433 static PyObject *zero;
434
435 if (zero == NULL) {
436 zero = PyLong_FromLong(0);
437 if (!zero)
438 return NULL;
439 }
440
441 if (Py_TYPE(other) != &keyobject_type){
442 PyErr_Format(PyExc_TypeError, "other argument must be K instance");
443 return NULL;
444 }
445 compare = ((keyobject *) ko)->cmp;
446 assert(compare != NULL);
447 x = ((keyobject *) ko)->object;
448 y = ((keyobject *) other)->object;
449 if (!x || !y){
450 PyErr_Format(PyExc_AttributeError, "object");
451 return NULL;
452 }
453
454 /* Call the user's comparison function and translate the 3-way
455 * result into true or false (or error).
456 */
457 args = PyTuple_New(2);
458 if (args == NULL)
459 return NULL;
460 Py_INCREF(x);
461 Py_INCREF(y);
462 PyTuple_SET_ITEM(args, 0, x);
463 PyTuple_SET_ITEM(args, 1, y);
464 res = PyObject_Call(compare, args, NULL);
465 Py_DECREF(args);
466 if (res == NULL)
467 return NULL;
468 answer = PyObject_RichCompare(res, zero, op);
469 Py_DECREF(res);
470 return answer;
471}
472
473static PyObject *
Victor Stinner446c8d52011-04-05 12:21:35 +0200474functools_cmp_to_key(PyObject *self, PyObject *args, PyObject *kwds)
475{
476 PyObject *cmp;
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700477 static char *kwargs[] = {"mycmp", NULL};
Victor Stinner446c8d52011-04-05 12:21:35 +0200478 keyobject *object;
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700479
480 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwargs, &cmp))
481 return NULL;
Victor Stinner446c8d52011-04-05 12:21:35 +0200482 object = PyObject_New(keyobject, &keyobject_type);
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700483 if (!object)
484 return NULL;
485 Py_INCREF(cmp);
486 object->cmp = cmp;
487 object->object = NULL;
488 return (PyObject *)object;
489}
490
491PyDoc_STRVAR(functools_cmp_to_key_doc,
492"Convert a cmp= function into a key= function.");
493
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000494/* reduce (used to be a builtin) ********************************************/
495
496static PyObject *
497functools_reduce(PyObject *self, PyObject *args)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
502 return NULL;
503 if (result != NULL)
504 Py_INCREF(result);
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 it = PyObject_GetIter(seq);
507 if (it == NULL) {
Alexander Belopolskye29e6bf2010-08-16 18:55:46 +0000508 if (PyErr_ExceptionMatches(PyExc_TypeError))
509 PyErr_SetString(PyExc_TypeError,
510 "reduce() arg 2 must support iteration");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 Py_XDECREF(result);
512 return NULL;
513 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if ((args = PyTuple_New(2)) == NULL)
516 goto Fail;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 for (;;) {
519 PyObject *op2;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (args->ob_refcnt > 1) {
522 Py_DECREF(args);
523 if ((args = PyTuple_New(2)) == NULL)
524 goto Fail;
525 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 op2 = PyIter_Next(it);
528 if (op2 == NULL) {
529 if (PyErr_Occurred())
530 goto Fail;
531 break;
532 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (result == NULL)
535 result = op2;
536 else {
537 PyTuple_SetItem(args, 0, result);
538 PyTuple_SetItem(args, 1, op2);
539 if ((result = PyEval_CallObject(func, args)) == NULL)
540 goto Fail;
541 }
542 }
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_DECREF(args);
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (result == NULL)
547 PyErr_SetString(PyExc_TypeError,
548 "reduce() of empty sequence with no initial value");
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 Py_DECREF(it);
551 return result;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000552
553Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 Py_XDECREF(args);
555 Py_XDECREF(result);
556 Py_DECREF(it);
557 return NULL;
Guido van Rossum0919a1a2006-08-26 20:49:04 +0000558}
559
560PyDoc_STRVAR(functools_reduce_doc,
561"reduce(function, sequence[, initial]) -> value\n\
562\n\
563Apply a function of two arguments cumulatively to the items of a sequence,\n\
564from left to right, so as to reduce the sequence to a single value.\n\
565For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
566((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
567of the sequence in the calculation, and serves as a default when the\n\
568sequence is empty.");
569
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000570/* module level code ********************************************************/
571
572PyDoc_STRVAR(module_doc,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000573"Tools that operate on functions.");
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000574
575static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
Victor Stinner446c8d52011-04-05 12:21:35 +0200577 {"cmp_to_key", (PyCFunction)functools_cmp_to_key,
578 METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 {NULL, NULL} /* sentinel */
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000580};
581
Martin v. Löwis1a214512008-06-11 05:26:20 +0000582
583static struct PyModuleDef _functoolsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 PyModuleDef_HEAD_INIT,
585 "_functools",
586 module_doc,
587 -1,
588 module_methods,
589 NULL,
590 NULL,
591 NULL,
592 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000593};
594
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000595PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000596PyInit__functools(void)
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 int i;
599 PyObject *m;
600 char *name;
601 PyTypeObject *typelist[] = {
602 &partial_type,
603 NULL
604 };
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 m = PyModule_Create(&_functoolsmodule);
607 if (m == NULL)
608 return NULL;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 for (i=0 ; typelist[i] != NULL ; i++) {
611 if (PyType_Ready(typelist[i]) < 0) {
612 Py_DECREF(m);
613 return NULL;
614 }
615 name = strchr(typelist[i]->tp_name, '.');
616 assert (name != NULL);
617 Py_INCREF(typelist[i]);
618 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
619 }
620 return m;
Raymond Hettinger9c323f82005-02-28 19:39:44 +0000621}