annotate Modules/_functoolsmodule.c @ 39496:352ffe77b71d legacy-trunk

Patch #1535500: fix segfault in BZ2File.writelines and make sure it raises the correct exceptions.
author Georg Brandl <georg@python.org>
date Mon, 14 Aug 2006 21:42:55 +0000
parents aa065f820252
children 6620a510e9a9
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
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12 /* partial object **********************************************************/
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 typedef struct {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 PyObject_HEAD
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 PyObject *fn;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 PyObject *args;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18 PyObject *kw;
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
19 PyObject *dict;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
20 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
21 } partialobject;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23 static PyTypeObject partial_type;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
24
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25 static PyObject *
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26 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
27 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28 PyObject *func;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 partialobject *pto;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 if (PyTuple_GET_SIZE(args) < 1) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 PyErr_SetString(PyExc_TypeError,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 "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
34 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37 func = PyTuple_GET_ITEM(args, 0);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 if (!PyCallable_Check(func)) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 PyErr_SetString(PyExc_TypeError,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40 "the first argument must be callable");
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44 /* create partialobject structure */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 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
46 if (pto == NULL)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49 pto->fn = func;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50 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
51 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
52 if (pto->args == NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53 pto->kw = NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54 Py_DECREF(pto);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 if (kw != NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 pto->kw = PyDict_Copy(kw);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 if (pto->kw == NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 Py_DECREF(pto);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 } else {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 pto->kw = Py_None;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 Py_INCREF(Py_None);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
68 pto->weakreflist = NULL;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
69 pto->dict = NULL;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
70
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 return (PyObject *)pto;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 static void
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 partial_dealloc(partialobject *pto)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 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
78 if (pto->weakreflist != NULL)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
79 PyObject_ClearWeakRefs((PyObject *) pto);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80 Py_XDECREF(pto->fn);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 Py_XDECREF(pto->args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 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
83 Py_XDECREF(pto->dict);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 pto->ob_type->tp_free(pto);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87 static PyObject *
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 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
89 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 PyObject *ret;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 PyObject *argappl = NULL, *kwappl = NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 assert (PyCallable_Check(pto->fn));
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 assert (PyTuple_Check(pto->args));
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 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
96
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 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
98 argappl = args;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 Py_INCREF(args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 } 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
101 argappl = pto->args;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 Py_INCREF(pto->args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 } else {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 argappl = PySequence_Concat(pto->args, args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 if (argappl == NULL)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
106 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 }
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 (pto->kw == Py_None) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 kwappl = kw;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111 Py_XINCREF(kw);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
112 } else {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 kwappl = PyDict_Copy(pto->kw);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 if (kwappl == NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 Py_DECREF(argappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 if (kw != NULL) {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119 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
120 Py_DECREF(argappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 Py_DECREF(kwappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 return NULL;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 }
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 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
128 Py_DECREF(argappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 Py_XDECREF(kwappl);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 return ret;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 static int
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 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
135 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 Py_VISIT(pto->fn);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 Py_VISIT(pto->args);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 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
139 Py_VISIT(pto->dict);
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 return 0;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 PyDoc_STRVAR(partial_doc,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 "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
145 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
146
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 #define OFF(x) offsetof(partialobject, x)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148 static PyMemberDef partial_memberlist[] = {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
149 {"func", T_OBJECT, OFF(fn), READONLY,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150 "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
151 {"args", T_OBJECT, OFF(args), READONLY,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 "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
153 {"keywords", T_OBJECT, OFF(kw), READONLY,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
154 "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
155 {NULL} /* Sentinel */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
156 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
157
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
158 static PyObject *
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
159 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
160 {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
161 if (pto->dict == NULL) {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
162 pto->dict = PyDict_New();
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
163 if (pto->dict == NULL)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
164 return NULL;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
165 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
166 Py_INCREF(pto->dict);
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
167 return pto->dict;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
168 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
169
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
170 static int
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
171 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
172 {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
173 PyObject *tmp;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
174
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
175 /* 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
176 if (value == NULL) {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
177 PyErr_SetString(PyExc_TypeError,
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
178 "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
179 return -1;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
180 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
181 /* 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
182 if (!PyDict_Check(value)) {
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
183 PyErr_SetString(PyExc_TypeError,
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
184 "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
185 return -1;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
186 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
187 tmp = pto->dict;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
188 Py_INCREF(value);
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
189 pto->dict = value;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
190 Py_XDECREF(tmp);
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
191 return 0;
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
192 }
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
193
36635
145d5fc9a6a5 Fix typo in functional module
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
194 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
195 {"__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
196 {NULL} /* Sentinel */
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
197 };
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
198
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 static PyTypeObject partial_type = {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 PyObject_HEAD_INIT(NULL)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 0, /* ob_size */
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
202 "functools.partial", /* tp_name */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203 sizeof(partialobject), /* tp_basicsize */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 0, /* tp_itemsize */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 /* methods */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 (destructor)partial_dealloc, /* tp_dealloc */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 0, /* tp_print */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 0, /* tp_getattr */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 0, /* tp_setattr */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 0, /* tp_compare */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 0, /* tp_repr */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 0, /* tp_as_number */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 0, /* tp_as_sequence */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 0, /* tp_as_mapping */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 0, /* tp_hash */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
216 (ternaryfunc)partial_call, /* tp_call */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217 0, /* tp_str */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 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
219 PyObject_GenericSetAttr, /* tp_setattro */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 0, /* tp_as_buffer */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 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
222 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
223 partial_doc, /* tp_doc */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 (traverseproc)partial_traverse, /* tp_traverse */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225 0, /* tp_clear */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 0, /* tp_richcompare */
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
227 offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 0, /* tp_iter */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 0, /* tp_iternext */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230 0, /* tp_methods */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 partial_memberlist, /* tp_members */
36635
145d5fc9a6a5 Fix typo in functional module
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
232 partial_getsetlist, /* tp_getset */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 0, /* tp_base */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 0, /* tp_dict */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235 0, /* tp_descr_get */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236 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
237 offsetof(partialobject, dict), /* tp_dictoffset */
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 0, /* tp_init */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239 0, /* tp_alloc */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 partial_new, /* tp_new */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241 PyObject_GC_Del, /* tp_free */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
243
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
244
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
245 /* module level code ********************************************************/
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
246
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
247 PyDoc_STRVAR(module_doc,
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
248 "Tools that operate on functions.");
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
249
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
250 static PyMethodDef module_methods[] = {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
251 {NULL, NULL} /* sentinel */
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
252 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
253
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
254 PyMODINIT_FUNC
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
255 init_functools(void)
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
256 {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
257 int i;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
258 PyObject *m;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
259 char *name;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
260 PyTypeObject *typelist[] = {
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
261 &partial_type,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
262 NULL
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
263 };
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
264
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 37673
diff changeset
265 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
266 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34703
diff changeset
267 return;
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
268
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
269 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
270 if (PyType_Ready(typelist[i]) < 0)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
271 return;
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
272 name = strchr(typelist[i]->tp_name, '.');
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
273 assert (name != NULL);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
274 Py_INCREF(typelist[i]);
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
275 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
276 }
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
277 }