annotate Modules/_functoolsmodule.c @ 49955:878d40468c4e legacy-trunk

Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
author Vinay Sajip <vinay_sajip@yahoo.co.uk>
date Wed, 03 Dec 2008 23:22:58 +0000
parents b3abff17e2dd
children e38335d4df91 184ca6293218
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 #include "Python.h"
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3 #include "structmember.h"
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
5 /* _functools module written and maintained
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 by Hye-Shik Chang <perky@FreeBSD.org>
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 with adaptations by Raymond Hettinger <python@rcn.com>
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
8 Copyright (c) 2004, 2005, 2006 Python Software Foundation.
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 All rights reserved.
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11
48204
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
12 /* reduce() *************************************************************/
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
13
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
14 static PyObject *
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
15 functools_reduce(PyObject *self, PyObject *args)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
16 {
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
17 PyObject *seq, *func, *result = NULL, *it;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
18
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
19 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
20 return NULL;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
21 if (result != NULL)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
22 Py_INCREF(result);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
23
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
24 it = PyObject_GetIter(seq);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
25 if (it == NULL) {
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
26 PyErr_SetString(PyExc_TypeError,
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
27 "reduce() arg 2 must support iteration");
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
28 Py_XDECREF(result);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
29 return NULL;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
30 }
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
31
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
32 if ((args = PyTuple_New(2)) == NULL)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
33 goto Fail;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
34
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
35 for (;;) {
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
36 PyObject *op2;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
37
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
38 if (args->ob_refcnt > 1) {
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
39 Py_DECREF(args);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
40 if ((args = PyTuple_New(2)) == NULL)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
41 goto Fail;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
42 }
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
43
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
44 op2 = PyIter_Next(it);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
45 if (op2 == NULL) {
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
46 if (PyErr_Occurred())
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
47 goto Fail;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
48 break;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
49 }
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
50
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
51 if (result == NULL)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
52 result = op2;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
53 else {
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
54 PyTuple_SetItem(args, 0, result);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
55 PyTuple_SetItem(args, 1, op2);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
56 if ((result = PyEval_CallObject(func, args)) == NULL)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
57 goto Fail;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
58 }
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
59 }
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
60
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
61 Py_DECREF(args);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
62
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
63 if (result == NULL)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
64 PyErr_SetString(PyExc_TypeError,
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
65 "reduce() of empty sequence with no initial value");
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
66
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
67 Py_DECREF(it);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
68 return result;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
69
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
70 Fail:
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
71 Py_XDECREF(args);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
72 Py_XDECREF(result);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
73 Py_DECREF(it);
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
74 return NULL;
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
75 }
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
76
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
77 PyDoc_STRVAR(reduce_doc,
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
78 "reduce(function, sequence[, initial]) -> value\n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
79 \n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
80 Apply a function of two arguments cumulatively to the items of a sequence,\n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
81 from left to right, so as to reduce the sequence to a single value.\n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
82 For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
83 ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
84 of the sequence in the calculation, and serves as a default when the\n\
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
85 sequence is empty.");
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
86
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
87
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
88
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
89
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 /* partial object **********************************************************/
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 typedef struct {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 PyObject_HEAD
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 PyObject *fn;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 PyObject *args;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 PyObject *kw;
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
97 PyObject *dict;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
98 PyObject *weakreflist; /* List of weak references */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 } partialobject;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 static PyTypeObject partial_type;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 static PyObject *
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
106 PyObject *func;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 partialobject *pto;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 if (PyTuple_GET_SIZE(args) < 1) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 PyErr_SetString(PyExc_TypeError,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111 "type 'partial' takes at least one argument");
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
112 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 func = PyTuple_GET_ITEM(args, 0);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 if (!PyCallable_Check(func)) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 PyErr_SetString(PyExc_TypeError,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 "the first argument must be callable");
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 /* create partialobject structure */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 pto = (partialobject *)type->tp_alloc(type, 0);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 if (pto == NULL)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127 pto->fn = func;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 Py_INCREF(func);
37673
606818c33e50 Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents: 36635
diff changeset
129 pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 if (pto->args == NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 pto->kw = NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 Py_DECREF(pto);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 if (kw != NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 pto->kw = PyDict_Copy(kw);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 if (pto->kw == NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 Py_DECREF(pto);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 } else {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 pto->kw = Py_None;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 Py_INCREF(Py_None);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
146 pto->weakreflist = NULL;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
147 pto->dict = NULL;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
148
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
149 return (PyObject *)pto;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
151
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 static void
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153 partial_dealloc(partialobject *pto)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
154 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
155 PyObject_GC_UnTrack(pto);
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
156 if (pto->weakreflist != NULL)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
157 PyObject_ClearWeakRefs((PyObject *) pto);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
158 Py_XDECREF(pto->fn);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
159 Py_XDECREF(pto->args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160 Py_XDECREF(pto->kw);
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
161 Py_XDECREF(pto->dict);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
162 Py_TYPE(pto)->tp_free(pto);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165 static PyObject *
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166 partial_call(partialobject *pto, PyObject *args, PyObject *kw)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 PyObject *ret;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 PyObject *argappl = NULL, *kwappl = NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
170
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 assert (PyCallable_Check(pto->fn));
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
172 assert (PyTuple_Check(pto->args));
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173 assert (pto->kw == Py_None || PyDict_Check(pto->kw));
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
175 if (PyTuple_GET_SIZE(pto->args) == 0) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 argappl = args;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 Py_INCREF(args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178 } else if (PyTuple_GET_SIZE(args) == 0) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 argappl = pto->args;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180 Py_INCREF(pto->args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181 } else {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182 argappl = PySequence_Concat(pto->args, args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
183 if (argappl == NULL)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
184 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 if (pto->kw == Py_None) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 kwappl = kw;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 Py_XINCREF(kw);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 } else {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 kwappl = PyDict_Copy(pto->kw);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192 if (kwappl == NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 Py_DECREF(argappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 if (kw != NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 if (PyDict_Merge(kwappl, kw, 1) != 0) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 Py_DECREF(argappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 Py_DECREF(kwappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 ret = PyObject_Call(pto->fn, argappl, kwappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 Py_DECREF(argappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 Py_XDECREF(kwappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 return ret;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 static int
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 partial_traverse(partialobject *pto, visitproc visit, void *arg)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 Py_VISIT(pto->fn);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 Py_VISIT(pto->args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
216 Py_VISIT(pto->kw);
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
217 Py_VISIT(pto->dict);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 return 0;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 PyDoc_STRVAR(partial_doc,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 "partial(func, *args, **keywords) - new function with partial application\n\
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 of the given arguments and keywords.\n");
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225 #define OFF(x) offsetof(partialobject, x)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 static PyMemberDef partial_memberlist[] = {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 {"func", T_OBJECT, OFF(fn), READONLY,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 "function object to use in future partial calls"},
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 {"args", T_OBJECT, OFF(args), READONLY,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230 "tuple of arguments to future partial calls"},
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 {"keywords", T_OBJECT, OFF(kw), READONLY,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 "dictionary of keyword arguments to future partial calls"},
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 {NULL} /* Sentinel */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
236 static PyObject *
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
237 partial_get_dict(partialobject *pto)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
238 {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
239 if (pto->dict == NULL) {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
240 pto->dict = PyDict_New();
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
241 if (pto->dict == NULL)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
242 return NULL;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
243 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
244 Py_INCREF(pto->dict);
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
245 return pto->dict;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
246 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
247
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
248 static int
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
249 partial_set_dict(partialobject *pto, PyObject *value)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
250 {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
251 PyObject *tmp;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
252
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
253 /* It is illegal to del p.__dict__ */
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
254 if (value == NULL) {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
255 PyErr_SetString(PyExc_TypeError,
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
256 "a partial object's dictionary may not be deleted");
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
257 return -1;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
258 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
259 /* Can only set __dict__ to a dictionary */
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
260 if (!PyDict_Check(value)) {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
261 PyErr_SetString(PyExc_TypeError,
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
262 "setting partial object's dictionary to a non-dict");
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
263 return -1;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
264 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
265 tmp = pto->dict;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
266 Py_INCREF(value);
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
267 pto->dict = value;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
268 Py_XDECREF(tmp);
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
269 return 0;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
270 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
271
36635
145d5fc9a6a5 Fix typo in functional module
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
272 static PyGetSetDef partial_getsetlist[] = {
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
273 {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
274 {NULL} /* Sentinel */
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
275 };
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
276
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
277 static PyTypeObject partial_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 38477
diff changeset
278 PyVarObject_HEAD_INIT(NULL, 0)
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
279 "functools.partial", /* tp_name */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 sizeof(partialobject), /* tp_basicsize */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
281 0, /* tp_itemsize */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282 /* methods */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283 (destructor)partial_dealloc, /* tp_dealloc */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 0, /* tp_print */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
285 0, /* tp_getattr */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 0, /* tp_setattr */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
287 0, /* tp_compare */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 0, /* tp_repr */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
289 0, /* tp_as_number */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 0, /* tp_as_sequence */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
291 0, /* tp_as_mapping */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 0, /* tp_hash */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 (ternaryfunc)partial_call, /* tp_call */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
294 0, /* tp_str */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
295 PyObject_GenericGetAttr, /* tp_getattro */
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
296 PyObject_GenericSetAttr, /* tp_setattro */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
297 0, /* tp_as_buffer */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
298 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
299 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
300 partial_doc, /* tp_doc */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
301 (traverseproc)partial_traverse, /* tp_traverse */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
302 0, /* tp_clear */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 0, /* tp_richcompare */
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
304 offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 0, /* tp_iter */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 0, /* tp_iternext */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307 0, /* tp_methods */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308 partial_memberlist, /* tp_members */
36635
145d5fc9a6a5 Fix typo in functional module
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
309 partial_getsetlist, /* tp_getset */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
310 0, /* tp_base */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311 0, /* tp_dict */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
312 0, /* tp_descr_get */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 0, /* tp_descr_set */
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
314 offsetof(partialobject, dict), /* tp_dictoffset */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 0, /* tp_init */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316 0, /* tp_alloc */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 partial_new, /* tp_new */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318 PyObject_GC_Del, /* tp_free */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322 /* module level code ********************************************************/
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
323
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324 PyDoc_STRVAR(module_doc,
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
325 "Tools that operate on functions.");
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327 static PyMethodDef module_methods[] = {
48204
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 44350
diff changeset
328 {"reduce", functools_reduce, METH_VARARGS, reduce_doc},
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 {NULL, NULL} /* sentinel */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
332 PyMODINIT_FUNC
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
333 init_functools(void)
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 int i;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 PyObject *m;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337 char *name;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
338 PyTypeObject *typelist[] = {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 &partial_type,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 NULL
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
343 m = Py_InitModule3("_functools", module_methods, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34703
diff changeset
344 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34703
diff changeset
345 return;
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347 for (i=0 ; typelist[i] != NULL ; i++) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 if (PyType_Ready(typelist[i]) < 0)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 return;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
350 name = strchr(typelist[i]->tp_name, '.');
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
351 assert (name != NULL);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
352 Py_INCREF(typelist[i]);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
354 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
355 }