Cython: segmentation error using API Embedding Cython in C

I am trying to inject Cython code in C, following the O'reilly Cython book in chapter 8. I found this paragraph in Cython, but still don't know what to do:

If the C code that wants to use these functions is part of more than one shared library or executable, the import_modulename () function must be called in each of the shared libraries that use these functions. If you cause a segmentation error (SIGSEGV on linux) when you make one of these api calls, it most likely indicates that the shared library containing the api call that generates the segmentation error does not call the import_modulename () function until the api call, which is a failure.

I am running Python 3.4, Cython 0.23, and GCC 5 on OS X. Source code transcendentals.pyxand main.c:

main.c

#include "transcendentals_api.h"
#include <math.h>
#include <stdio.h>

int main(int argc, char **argv)
{
  Py_SetPythonHome(L"/Users/spacegoing/anaconda");
  Py_Initialize();
  import_transcendentals();
  printf("pi**e: %f\n", pow(get_pi(), get_e()));

  Py_Finalize();
    return 0;
}

transcendentals.pyx

cdef api double get_pi():
    return 3.1415926

cdef api double get_e():
    print("calling get_e()")
    return 2.718281828

I am compiling these files with setup.pyand Makefile:

setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

setup(
    ext_modules=cythonize([
        Extension("transcendentals", ["transcendentals.pyx"])
    ])
)

Makefile

python-config=/Users/spacegoing/anaconda/bin/python3-config
ldflags:=$(shell $(python-config) --ldflags)
cflags:=$(shell $(python-config) --cflags)

a.out: main.c transcendentals.so
    gcc-5 $(cflags) $(ldflags) transcendentals.c main.c

transcendentals.so: setup.py transcendentals.pyx
    python setup.py build_ext --inplace
    cython transcendentals.pyx


clean:
    rm -r a.out a.out.dSYM build transcendentals.[ch] transcendentals.so transcendentals_api.h

However, I came up with an error Segmentation fault: 11. Any idea can help with this? Thank!

+4
source share
1 answer

This makefile has

transcendentals.so: setup.py transcendentals.pyx
    python setup.py build_ext --inplace

If it pythonrefers to /Users/spacegoing/anaconda/bin/python3, it should be replaced, since the module can be compiled for the wrong version of python and cannot be loaded.

main.c import_transcendentals(), , .. . get_pi() get_e() , .

, - , . , python. PYTHONPATH , , transcendentals.so.

C , .

, PyInit_transcendentals() .

transcendentals.h , cython public i.e.

cdef public api double get_pi():
...
cdef public api double get_e():

main.c include

#include <Python.h>
#include "transcendentals.h"

main

Py_Initialize();
PyInit_transcendentals();

#include "transcendentals_api.h" no import_transcendentals()

,

, modulename.h, modulename_api.h C, , .

, transcendentals.c

gcc $(cflags) $(ldflags) transcendentals.c main.c

. , PyInit_transcendentals() Python 3

+1

All Articles