forked from Shimuuar/inline-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.c
More file actions
142 lines (128 loc) · 4.12 KB
/
Copy pathpython.c
File metadata and controls
142 lines (128 loc) · 4.12 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <inline-python.h>
#include <stdlib.h>
// ================================================================
// Callbacks
//
// General idea: we store function pointer (haskell's FunPtr) in
// PyCapsule and use to call function. Most importantly we must
// release GIL before calling into haskell. Haskell callback will
// happen on different thread (on threaded RTS). So it'll have to
// reacquire GIL there.
// ================================================================
// Same wrapper works for METH_O and METH_NOARGS
static PyObject* callback_METH_CFunction(PyObject* self, PyObject* arg) {
PyObject *res;
PyCFunction *fun = PyCapsule_GetPointer(self, NULL);
Py_BEGIN_ALLOW_THREADS
res = (*fun)(self, arg);
Py_END_ALLOW_THREADS
return res;
}
static PyObject* callback_METH_FASTCALL(PyObject* self, PyObject** args, Py_ssize_t nargs) {
PyObject *res;
PyCFunctionFast *fun = PyCapsule_GetPointer(self, NULL);
Py_BEGIN_ALLOW_THREADS
res = (*fun)(self, args, nargs);
Py_END_ALLOW_THREADS
return res;
}
static void capsule_free_FunPtr(PyObject* capsule) {
PyCFunction *fun = PyCapsule_GetPointer(capsule, NULL);
// We call directly to haskell RTS to free FunPtr. Only question
// is how stable is this API.
freeHaskellFunctionPtr(*fun);
free(fun);
}
static PyMethodDef method_METH_NOARGS = {
.ml_name = "[inline_python]",
.ml_meth = callback_METH_CFunction,
.ml_flags = METH_NOARGS,
.ml_doc = "Wrapper for haskell callback"
};
static PyMethodDef method_METH_O = {
.ml_name = "[inline_python]",
.ml_meth = callback_METH_CFunction,
.ml_flags = METH_O,
.ml_doc = "Wrapper for haskell callback"
};
static PyMethodDef method_METH_FASTCALL = {
.ml_name = "[inline_python]",
.ml_meth = (PyCFunction)callback_METH_FASTCALL,
.ml_flags = METH_FASTCALL,
.ml_doc = "Wrapper for haskell callback"
};
PyObject *inline_py_callback_METH_NOARGS(PyCFunction fun) {
PyCFunction *buf = malloc(sizeof(PyCFunction));
*buf = fun;
PyObject* self = PyCapsule_New(buf, NULL, &capsule_free_FunPtr);
if( PyErr_Occurred() )
return NULL;
// Python function
PyObject* f = PyCFunction_New(&method_METH_NOARGS, self);
Py_DECREF(self);
return f;
}
PyObject *inline_py_callback_METH_O(PyCFunction fun) {
PyCFunction *buf = malloc(sizeof(PyCFunction));
*buf = fun;
PyObject* self = PyCapsule_New(buf, NULL, &capsule_free_FunPtr);
if( PyErr_Occurred() )
return NULL;
// Python function
PyObject* f = PyCFunction_New(&method_METH_O, self);
Py_DECREF(self);
return f;
}
PyObject *inline_py_callback_METH_FASTCALL(PyCFunctionFast fun) {
PyCFunctionFast *buf = malloc(sizeof(PyCFunctionFast));
*buf = fun;
PyObject* self = PyCapsule_New(buf, NULL, &capsule_free_FunPtr);
if( PyErr_Occurred() )
return NULL;
// Python function
PyObject* f = PyCFunction_New(&method_METH_FASTCALL, self);
Py_DECREF(self);
return f;
}
// ================================================================
// Marshalling
// ================================================================
int inline_py_unpack_iterable(PyObject *iterable, int n, PyObject **out) {
// Initialize iterator. If object is not an iterable we treat this
// as not an exception but as a conversion failure
PyObject* iter = PyObject_GetIter( iterable );
if( PyErr_Occurred() ) {
PyErr_Clear();
return -1;
}
if( !PyIter_Check(iter) ) {
goto err_iter;
}
// Fill out with NULL. This way we can call XDECREF on them
for(int i = 0; i < n; i++) {
out[i] = NULL;
}
// Fill elements
for(int i = 0; i < n; i++) {
out[i] = PyIter_Next(iter);
if( NULL==out[i] ) {
goto err_elem;
}
}
// End of iteration
PyObject* end = PyIter_Next(iter);
if( NULL != end || PyErr_Occurred() ) {
goto err_end;
}
return 0;
//----------------------------------------
err_end:
Py_XDECREF(end);
err_elem:
for(int i = 0; i < n; i++) {
Py_XDECREF(out[i]);
}
err_iter:
Py_DECREF(iter);
return -1;
}