-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpycallfunc.pyx
38 lines (33 loc) · 857 Bytes
/
pycallfunc.pyx
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
# -*-python-*-
# See Makefile, which will compile and test this
# for exc_info()
import sys
# the C library
cimport c_callfunc
cdef class calldata:
cdef:
object f # Python callback function
object exc # Exception info
# C wrapper for Python callback
# Does exception handling
cdef int callback_wrapper(int a, int *b, void *user_data):
cdef calldata cd
cd= <object>user_data
f= <object>cd.f
try:
b[0]=f(a)
return 0
except:
cd.exc=sys.exc_info()
return 1
# Python wrapper around c_callfunc.call
# Checks for error return, extracts exception and rethrows it
def py_call(f,a):
cdef int b
cdef calldata cd
cd=calldata()
cd.f=f
if 0 == c_callfunc.c_call(callback_wrapper, a, &b, <void*>cd):
return b
else:
raise cd.exc[0],cd.exc[1],cd.exc[2]