blob: 18efab20602f3b5ec04471346a1d63ee8a824099 [file] [log] [blame]
Raymond Hettinger9c323f82005-02-28 19:39:44 +00001
2#include "Python.h"
3#include "structmember.h"
4
5/* Functional module written and maintained
6 by Hye-Shik Chang <perky@FreeBSD.org>
7 with adaptations by Raymond Hettinger <python@rcn.com>
8 Copyright (c) 2004, 2005 Python Software Foundation.
9 All rights reserved.
10*/
11
12/* partial object **********************************************************/
13
14typedef struct {
15 PyObject_HEAD
16 PyObject *fn;
17 PyObject *args;
18 PyObject *kw;
19} partialobject;
20
21static PyTypeObject partial_type;
22
23static PyObject *
24partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
25{
26 PyObject *func;
27 partialobject *pto;
28
29 if (PyTuple_GET_SIZE(args) < 1) {
30 PyErr_SetString(PyExc_TypeError,
31 "type 'partial' takes at least one argument");
32 return NULL;
33 }
34
35 func = PyTuple_GET_ITEM(args, 0);
36 if (!PyCallable_Check(func)) {
37 PyErr_SetString(PyExc_TypeError,
38 "the first argument must be callable");
39 return NULL;
40 }
41
42 /* create partialobject structure */
43 pto = (partialobject *)type->tp_alloc(type, 0);
44 if (pto == NULL)
45 return NULL;
46
47 pto->fn = func;
48 Py_INCREF(func);
49 pto->args = PyTuple_GetSlice(args, 1, INT_MAX);
50 if (pto->args == NULL) {
51 pto->kw = NULL;
52 Py_DECREF(pto);
53 return NULL;
54 }
55 if (kw != NULL) {
56 pto->kw = PyDict_Copy(kw);
57 if (pto->kw == NULL) {
58 Py_DECREF(pto);
59 return NULL;
60 }
61 } else {
62 pto->kw = Py_None;
63 Py_INCREF(Py_None);
64 }
65
66 return (PyObject *)pto;
67}
68
69static void
70partial_dealloc(partialobject *pto)
71{
72 PyObject_GC_UnTrack(pto);
73 Py_XDECREF(pto->fn);
74 Py_XDECREF(pto->args);
75 Py_XDECREF(pto->kw);
76 pto->ob_type->tp_free(pto);
77}
78
79static PyObject *
80partial_call(partialobject *pto, PyObject *args, PyObject *kw)
81{
82 PyObject *ret;
83 PyObject *argappl = NULL, *kwappl = NULL;
84
85 assert (PyCallable_Check(pto->fn));
86 assert (PyTuple_Check(pto->args));
87 assert (pto->kw == Py_None || PyDict_Check(pto->kw));
88
89 if (PyTuple_GET_SIZE(pto->args) == 0) {
90 argappl = args;
91 Py_INCREF(args);
92 } else if (PyTuple_GET_SIZE(args) == 0) {
93 argappl = pto->args;
94 Py_INCREF(pto->args);
95 } else {
96 argappl = PySequence_Concat(pto->args, args);
97 if (argappl == NULL)
98 return NULL;
99 }
100
101 if (pto->kw == Py_None) {
102 kwappl = kw;
103 Py_XINCREF(kw);
104 } else {
105 kwappl = PyDict_Copy(pto->kw);
106 if (kwappl == NULL) {
107 Py_DECREF(argappl);
108 return NULL;
109 }
110 if (kw != NULL) {
111 if (PyDict_Merge(kwappl, kw, 1) != 0) {
112 Py_DECREF(argappl);
113 Py_DECREF(kwappl);
114 return NULL;
115 }
116 }
117 }
118
119 ret = PyObject_Call(pto->fn, argappl, kwappl);
120 Py_DECREF(argappl);
121 Py_XDECREF(kwappl);
122 return ret;
123}
124
125static int
126partial_traverse(partialobject *pto, visitproc visit, void *arg)
127{
128 Py_VISIT(pto->fn);
129 Py_VISIT(pto->args);
130 Py_VISIT(pto->kw);
131 return 0;
132}
133
134PyDoc_STRVAR(partial_doc,
135"partial(func, *args, **keywords) - new function with partial application\n\
136 of the given arguments and keywords.\n");
137
138#define OFF(x) offsetof(partialobject, x)
139static PyMemberDef partial_memberlist[] = {
140 {"func", T_OBJECT, OFF(fn), READONLY,
141 "function object to use in future partial calls"},
142 {"args", T_OBJECT, OFF(args), READONLY,
143 "tuple of arguments to future partial calls"},
144 {"keywords", T_OBJECT, OFF(kw), READONLY,
145 "dictionary of keyword arguments to future partial calls"},
146 {NULL} /* Sentinel */
147};
148
149static PyTypeObject partial_type = {
150 PyObject_HEAD_INIT(NULL)
151 0, /* ob_size */
152 "functional.partial", /* tp_name */
153 sizeof(partialobject), /* tp_basicsize */
154 0, /* tp_itemsize */
155 /* methods */
156 (destructor)partial_dealloc, /* tp_dealloc */
157 0, /* tp_print */
158 0, /* tp_getattr */
159 0, /* tp_setattr */
160 0, /* tp_compare */
161 0, /* tp_repr */
162 0, /* tp_as_number */
163 0, /* tp_as_sequence */
164 0, /* tp_as_mapping */
165 0, /* tp_hash */
166 (ternaryfunc)partial_call, /* tp_call */
167 0, /* tp_str */
168 PyObject_GenericGetAttr, /* tp_getattro */
169 0, /* tp_setattro */
170 0, /* tp_as_buffer */
171 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
172 Py_TPFLAGS_BASETYPE, /* tp_flags */
173 partial_doc, /* tp_doc */
174 (traverseproc)partial_traverse, /* tp_traverse */
175 0, /* tp_clear */
176 0, /* tp_richcompare */
177 0, /* tp_weaklistoffset */
178 0, /* tp_iter */
179 0, /* tp_iternext */
180 0, /* tp_methods */
181 partial_memberlist, /* tp_members */
182 0, /* tp_getset */
183 0, /* tp_base */
184 0, /* tp_dict */
185 0, /* tp_descr_get */
186 0, /* tp_descr_set */
187 0, /* tp_dictoffset */
188 0, /* tp_init */
189 0, /* tp_alloc */
190 partial_new, /* tp_new */
191 PyObject_GC_Del, /* tp_free */
192};
193
194
195/* module level code ********************************************************/
196
197PyDoc_STRVAR(module_doc,
198"Tools for functional programming.");
199
200static PyMethodDef module_methods[] = {
201 {NULL, NULL} /* sentinel */
202};
203
204PyMODINIT_FUNC
205initfunctional(void)
206{
207 int i;
208 PyObject *m;
209 char *name;
210 PyTypeObject *typelist[] = {
211 &partial_type,
212 NULL
213 };
214
215 m = Py_InitModule3("functional", module_methods, module_doc);
216
217 for (i=0 ; typelist[i] != NULL ; i++) {
218 if (PyType_Ready(typelist[i]) < 0)
219 return;
220 name = strchr(typelist[i]->tp_name, '.');
221 assert (name != NULL);
222 Py_INCREF(typelist[i]);
223 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
224 }
225}