Cython callbacks and external C program

22 views
Skip to first unread message

Charles Leifer

unread,
Nov 12, 2015, 12:25:17 PM11/12/15
to cython-users
I'm very interested in using Cython to write C extensions for SQLite. I was able to use Cython so long as everything stayed "in C", but as soon as Cython tried to acquire the GIL I'd get a segfault.

Turns out the problem could be fixed by hand-hacking the generated C source file and explicitly calling `Py_Initialize()` and the generated module init function, `init<mymodule>`.

I was reading the docs and there's a section that mentions external C code must be responsible for calling Py_Initialize() and Py_Finalize(), but is there a way I can embed this logic in the Cython module itself?

If it helps, SQLite extensions have a well-defined entrypoint. I opened up the C source and, towards the end of the generated SQLite entry-point, I added my calls to Py_Initialize() and init<mymodule>(). Can I write that initialization logic in Cython rather than having to hand-hack it into the generated file?

Any help is very appreciated.

Charles Leifer

unread,
Nov 12, 2015, 12:28:44 PM11/12/15
to cython-users
Shame on me for not Googling harder.

Here's how I got it working:

cdef extern from "Python.h":
    cdef void Py_Initialize()

cdef extern void initmymodule() except *

cdef public int sqlite3_mymodule_init(sqlite3 *conn, char **errMessage, const sqlite3_api_routines *pApi):
    global sqlite3_api
    sqlite3_api = pApi

    Py_Initialize()
    initmymodule()
Reply all
Reply to author
Forward
0 new messages