Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | #include "structmember.h" |
| 4 | |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 5 | /* _functools module written and maintained |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 6 | by Hye-Shik Chang <perky@FreeBSD.org> |
| 7 | with adaptations by Raymond Hettinger <python@rcn.com> |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 8 | Copyright (c) 2004, 2005, 2006 Python Software Foundation. |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 9 | All rights reserved. |
| 10 | */ |
| 11 | |
| 12 | /* partial object **********************************************************/ |
| 13 | |
| 14 | typedef struct { |
| 15 | PyObject_HEAD |
| 16 | PyObject *fn; |
| 17 | PyObject *args; |
| 18 | PyObject *kw; |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 19 | PyObject *dict; |
| 20 | PyObject *weakreflist; /* List of weak references */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 21 | } partialobject; |
| 22 | |
| 23 | static PyTypeObject partial_type; |
| 24 | |
| 25 | static PyObject * |
| 26 | partial_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); |
Martin v. Löwis | b1ed7fa | 2006-04-13 07:52:27 +0000 | [diff] [blame] | 51 | pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 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 | } |
| 67 | |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 68 | pto->weakreflist = NULL; |
| 69 | pto->dict = NULL; |
| 70 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 71 | return (PyObject *)pto; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | partial_dealloc(partialobject *pto) |
| 76 | { |
| 77 | PyObject_GC_UnTrack(pto); |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 78 | if (pto->weakreflist != NULL) |
| 79 | PyObject_ClearWeakRefs((PyObject *) pto); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 80 | Py_XDECREF(pto->fn); |
| 81 | Py_XDECREF(pto->args); |
| 82 | Py_XDECREF(pto->kw); |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 83 | Py_XDECREF(pto->dict); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 84 | pto->ob_type->tp_free(pto); |
| 85 | } |
| 86 | |
| 87 | static PyObject * |
| 88 | partial_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 | |
| 133 | static int |
| 134 | partial_traverse(partialobject *pto, visitproc visit, void *arg) |
| 135 | { |
| 136 | Py_VISIT(pto->fn); |
| 137 | Py_VISIT(pto->args); |
| 138 | Py_VISIT(pto->kw); |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 139 | Py_VISIT(pto->dict); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | PyDoc_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) |
| 148 | static 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 Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 158 | static PyObject * |
| 159 | partial_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 | |
| 170 | static int |
| 171 | partial_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 Brandl | c2fb6c7 | 2006-02-21 17:49:57 +0000 | [diff] [blame] | 194 | static PyGetSetDef partial_getsetlist[] = { |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 195 | {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict}, |
| 196 | {NULL} /* Sentinel */ |
| 197 | }; |
| 198 | |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 199 | static PyTypeObject partial_type = { |
| 200 | PyObject_HEAD_INIT(NULL) |
| 201 | 0, /* ob_size */ |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 202 | "functools.partial", /* tp_name */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 203 | sizeof(partialobject), /* tp_basicsize */ |
| 204 | 0, /* tp_itemsize */ |
| 205 | /* methods */ |
| 206 | (destructor)partial_dealloc, /* tp_dealloc */ |
| 207 | 0, /* tp_print */ |
| 208 | 0, /* tp_getattr */ |
| 209 | 0, /* tp_setattr */ |
| 210 | 0, /* tp_compare */ |
| 211 | 0, /* tp_repr */ |
| 212 | 0, /* tp_as_number */ |
| 213 | 0, /* tp_as_sequence */ |
| 214 | 0, /* tp_as_mapping */ |
| 215 | 0, /* tp_hash */ |
| 216 | (ternaryfunc)partial_call, /* tp_call */ |
| 217 | 0, /* tp_str */ |
| 218 | PyObject_GenericGetAttr, /* tp_getattro */ |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 219 | PyObject_GenericSetAttr, /* tp_setattro */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 220 | 0, /* tp_as_buffer */ |
| 221 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 222 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 223 | partial_doc, /* tp_doc */ |
| 224 | (traverseproc)partial_traverse, /* tp_traverse */ |
| 225 | 0, /* tp_clear */ |
| 226 | 0, /* tp_richcompare */ |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 227 | offsetof(partialobject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 228 | 0, /* tp_iter */ |
| 229 | 0, /* tp_iternext */ |
| 230 | 0, /* tp_methods */ |
| 231 | partial_memberlist, /* tp_members */ |
Georg Brandl | c2fb6c7 | 2006-02-21 17:49:57 +0000 | [diff] [blame] | 232 | partial_getsetlist, /* tp_getset */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 233 | 0, /* tp_base */ |
| 234 | 0, /* tp_dict */ |
| 235 | 0, /* tp_descr_get */ |
| 236 | 0, /* tp_descr_set */ |
Raymond Hettinger | c8b6d1b | 2005-03-08 06:14:50 +0000 | [diff] [blame] | 237 | offsetof(partialobject, dict), /* tp_dictoffset */ |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 238 | 0, /* tp_init */ |
| 239 | 0, /* tp_alloc */ |
| 240 | partial_new, /* tp_new */ |
| 241 | PyObject_GC_Del, /* tp_free */ |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | /* module level code ********************************************************/ |
| 246 | |
| 247 | PyDoc_STRVAR(module_doc, |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 248 | "Tools that operate on functions."); |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 249 | |
| 250 | static PyMethodDef module_methods[] = { |
| 251 | {NULL, NULL} /* sentinel */ |
| 252 | }; |
| 253 | |
| 254 | PyMODINIT_FUNC |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 255 | init_functools(void) |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 256 | { |
| 257 | int i; |
| 258 | PyObject *m; |
| 259 | char *name; |
| 260 | PyTypeObject *typelist[] = { |
| 261 | &partial_type, |
| 262 | NULL |
| 263 | }; |
| 264 | |
Nick Coghlan | c649ec5 | 2006-05-29 12:43:05 +0000 | [diff] [blame] | 265 | m = Py_InitModule3("_functools", module_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 266 | if (m == NULL) |
| 267 | return; |
Raymond Hettinger | 9c323f8 | 2005-02-28 19:39:44 +0000 | [diff] [blame] | 268 | |
| 269 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 270 | if (PyType_Ready(typelist[i]) < 0) |
| 271 | return; |
| 272 | name = strchr(typelist[i]->tp_name, '.'); |
| 273 | assert (name != NULL); |
| 274 | Py_INCREF(typelist[i]); |
| 275 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
| 276 | } |
| 277 | } |