-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathoptimized.c
More file actions
120 lines (109 loc) · 2.39 KB
/
optimized.c
File metadata and controls
120 lines (109 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* copyright 2009, James William Pye
* http://python.projects.postgresql.org
*
*//*
* optimizations for postgresql.python package modules.
*/
#include <Python.h>
#include <structmember.h>
static PyObject *
rsetattr(PyObject *self, PyObject *args)
{
PyObject *ob, *attr, *val;
if (!PyArg_ParseTuple(args, "OOO", &attr, &val, &ob))
return(NULL);
if (PyObject_SetAttr(ob, attr, val) < 0)
return(NULL);
Py_INCREF(ob);
return(ob);
}
/*
* Override the functools.Composition __call__.
*/
static PyObject *
compose(PyObject *self, PyObject *args)
{
Py_ssize_t i, len;
PyObject *rob, *argt, *seq, *x;
if (!PyArg_ParseTuple(args, "OO", &seq, &rob))
return(NULL);
Py_INCREF(rob);
if (PyObject_IsInstance(seq, (PyObject *) &PyTuple_Type))
{
len = PyTuple_GET_SIZE(seq);
for (i = 0; i < len; ++i)
{
x = PyTuple_GET_ITEM(seq, i);
argt = PyTuple_New(1);
PyTuple_SET_ITEM(argt, 0, rob);
rob = PyObject_CallObject(x, argt);
Py_DECREF(argt);
if (rob == NULL)
break;
}
}
else if (PyObject_IsInstance(seq, (PyObject *) &PyList_Type))
{
len = PyList_GET_SIZE(seq);
for (i = 0; i < len; ++i)
{
x = PyList_GET_ITEM(seq, i);
argt = PyTuple_New(1);
PyTuple_SET_ITEM(argt, 0, rob);
rob = PyObject_CallObject(x, argt);
Py_DECREF(argt);
if (rob == NULL)
break;
}
}
else
{
/*
* Arbitrary sequence.
*/
len = PySequence_Length(seq);
for (i = 0; i < len; ++i)
{
x = PySequence_GetItem(seq, i);
argt = PyTuple_New(1);
PyTuple_SET_ITEM(argt, 0, rob);
rob = PyObject_CallObject(x, argt);
Py_DECREF(x);
Py_DECREF(argt);
if (rob == NULL)
break;
}
}
return(rob);
}
static PyMethodDef optimized_methods[] = {
{"rsetattr", rsetattr, METH_VARARGS,
PyDoc_STR(
"rsetattr(attr, val, ob) set the attribute to the value *and* return `ob`."
),
},
{"compose", compose, METH_VARARGS,
PyDoc_STR(
"given a sequence of callables, "
"and an argument for the first call, compose the result."
),
},
{NULL}
};
static struct PyModuleDef optimized_module = {
PyModuleDef_HEAD_INIT,
"optimized", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
optimized_methods,
};
PyMODINIT_FUNC
PyInit_optimized(void)
{
return(PyModule_Create(&optimized_module));
}
/*
* vim: ts=3:sw=3:noet:
*/