-
Notifications
You must be signed in to change notification settings - Fork 13
/
_cheetah.c
156 lines (119 loc) · 3.15 KB
/
_cheetah.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <Python.h>
static PyObject* NotFound;
static PyObject* _builtins_module;
static inline PyObject* _ns_lookup(char* key, PyObject* ns) {
PyObject* ret = NULL;
if ((ret = PyMapping_GetItemString(ns, key))) {
return ret;
}
PyErr_Clear();
{
PyObject* fmt = PyUnicode_FromString("Cannot find '{}'");
PyObject* fmted = PyObject_CallMethod(fmt, "format", "y", key);
PyErr_SetObject(NotFound, fmted);
Py_XDECREF(fmted);
Py_XDECREF(fmt);
}
return NULL;
}
static inline PyObject* _self_lookup(char* key, PyObject* selfobj) {
PyObject* ret = NULL;
if ((ret = PyObject_GetAttrString(selfobj, key))) {
return ret;
}
PyErr_Clear();
return ret;
}
static inline PyObject* _frame_lookup(char* key, PyObject* locals, PyObject* globals) {
PyObject* ret = NULL;
if ((ret = PyMapping_GetItemString(locals, key))) {
return ret;
}
PyErr_Clear();
if ((ret = PyMapping_GetItemString(globals, key))) {
return ret;
}
PyErr_Clear();
if ((ret = PyObject_GetAttrString(_builtins_module, key))) {
return ret;
}
PyErr_Clear();
return ret;
}
static PyObject* value_from_namespace(PyObject* _, PyObject* args) {
char* key;
PyObject* ns;
if (!PyArg_ParseTuple(args, "sO", &key, &ns)) {
return NULL;
}
return _ns_lookup(key, ns);
}
static PyObject* value_from_frame_or_namespace(PyObject* _, PyObject* args) {
char* key;
PyObject* locals;
PyObject* globals;
PyObject* ns;
PyObject* ret;
if (!PyArg_ParseTuple(args, "sOOO", &key, &locals, &globals, &ns)) {
return NULL;
}
if ((ret = _frame_lookup(key, locals, globals))) {
return ret;
} else {
return _ns_lookup(key, ns);
}
}
static PyObject* value_from_search_list(PyObject* _, PyObject* args) {
char* key;
PyObject* selfobj;
PyObject* ns;
PyObject* ret;
if (!PyArg_ParseTuple(args, "sOO", &key, &selfobj, &ns)) {
return NULL;
}
if ((ret = _self_lookup(key, selfobj))) {
return ret;
} else {
return _ns_lookup(key, ns);
}
}
static PyObject* _setup_module(PyObject* module) {
if (module) {
NotFound = PyErr_NewException("_cheetah.NotFound", PyExc_LookupError, NULL);
PyModule_AddObject(module, "NotFound", NotFound);
_builtins_module = PyImport_ImportModule("builtins");
if (!_builtins_module) {
Py_DECREF(module);
module = NULL;
}
}
return module;
}
static struct PyMethodDef methods[] = {
{
"value_from_namespace",
(PyCFunction)value_from_namespace,
METH_VARARGS
},
{
"value_from_frame_or_namespace",
(PyCFunction)value_from_frame_or_namespace,
METH_VARARGS
},
{
"value_from_search_list",
(PyCFunction)value_from_search_list,
METH_VARARGS
},
{NULL, NULL}
};
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"_cheetah",
NULL,
-1,
methods
};
PyMODINIT_FUNC PyInit__cheetah(void) {
return _setup_module(PyModule_Create(&module));
}