I am attempting to use Cython to:
1. Embed the Python interpreter
and then
2. Import and call some custom Python functions.
I can successfully do this when using Python 2.7. However, my custom
Python modules require Python 3. When I (minimally) adapt my Cython
code for Python 3, the end result is a segmentation fault during
import of my Python module. Attached is a minimal example. It
segfaults on the 'import sys' line. Compile by running something like:
> python3 setup.py build_ext
> gcc crashtest.c build/lib.linux-i686-3.2/
crash.cpython-32mu.so -I /usr/include/python3.2 -lpython3.2mu
Note that before running gcc, you must edit crash.h and remove the
DL_IMPORT. (See
http://codespeak.net/pipermail/cython-dev/2010-April/008888.html.)
Here is the backtrace.
#0 0x001b9f95 in PyType_IsSubtype () from /usr/lib/
libpython3.2mu.so.
1.0
#1 0x001a79b3 in PyModule_GetDict () from /usr/lib/
libpython3.2mu.so.
1.0
#2 0x00132736 in __Pyx_Import (name=0xb7caf220, from_list=<optimized
out>, level=<optimized out>) at crash.c:768
#3 initializePython () at crash.c:521
#4 0x080484ff in main ()
Cython version: 0.15
Python version: 3.2.2
OS: Ubuntu 11.10
Any idea what is going on? Let me know if any other information would
be helpful.
Adam
----- crash.pyx -------------------
# cython: language_level=3
cdef extern from "stdio.h":
int printf(char *format, ...)
cdef extern from "Python.h":
# embedding functions
void Py_Initialize()
int Py_IsInitialized()
cdef extern object PyInit_crash()
#cdef extern void initcrash()
cdef public void initializePython():
if not Py_IsInitialized():
printf("initializing Python\n")
Py_Initialize()
printf("initializing crash Cython module\n")
PyInit_crash()
#initcrash()
print('importing sys')
import sys
print('imported sys!')
------- setup.py -----------
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
name = 'python3 embed crash example',
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("crash", ["crash.pyx"],)]
)
--------- crashtest.c ----------------
#include "Python.h"
#include "crash.h"
int main(void * argv, int argc) {
initializePython();
return 0;
}