Python Cython回调中的Segfault

Python Cython回调中的Segfault,python,c,sqlite,cython,Python,C,Sqlite,Cython,我现在有点困了,希望能得到一些指导。我有一个包装sqlite的小模块,我想允许使用sqlite提供的钩子执行用户定义的Python函数 以下是SQLite头文件中的定义: cdef extern from "sqlite3.h": cdef void *sqlite3_commit_hook(sqlite3*, int(*xFunc)(void*), void*) 下面是相应的Cython: # Reference to user-provided function. cdef obj

我现在有点困了,希望能得到一些指导。我有一个包装sqlite的小模块,我想允许使用sqlite提供的钩子执行用户定义的Python函数

以下是SQLite头文件中的定义:

cdef extern from "sqlite3.h":
    cdef void *sqlite3_commit_hook(sqlite3*, int(*xFunc)(void*), void*)
下面是相应的Cython:

# Reference to user-provided function.
cdef object py_commit_hook = None

# Typedef for callback
ctypedef int(*commit_callback)(void *data)

# Wrapper function that will in turn call the user-provided python callback.
cdef int commit_hook_wrapper(void *data):
    print 'hello'  # Just for debugging.
    py_commit_hook()

# API used to set callback and make sqlite3 library call.
def set_commit_hook(DatabaseHelper db, callback):
    cdef commit_callback f
    global py_commit_hook

    py_commit_hook = callback
    f = commit_hook_wrapper

    sqlite3_commit_hook(db.db, f, <void *>0)
我完全被难住了!你知道哪里出了问题吗


我正在动态链接到libsqlite3,我想知道这是否是问题所在。我以前按照这些思路实现过回调,从来没有遇到过问题,但是这次无论我怎么尝试都失败了。

在cython用户列表中,有人建议我将回调声明为“with gil”。这就解决了这个问题:

cdef int commit_hook_wrapper(void *data) with gil:
    print 'hello'
    py_commit_hook()

从cython用户列表中,有人建议我将回调声明为“with gil”。这就解决了这个问题:

cdef int commit_hook_wrapper(void *data) with gil:
    print 'hello'
    py_commit_hook()

有关您的问题的一些信息可在此处找到: (对不起,我还不能发表评论)

要用作不使用GIL执行的C代码回调的C函数需要先获取GIL,然后才能操作Python对象


有关您的问题的一些信息可在此处找到: (对不起,我还不能发表评论)

要用作不使用GIL执行的C代码回调的C函数需要先获取GIL,然后才能操作Python对象

cdef int commit_hook_wrapper(void *data) with gil:
    print 'hello'
    py_commit_hook()