1

I have these files to reproduce the problem, mod2.pxd:

cdef public api:
    void myapi()

cdef public:
    cppclass myclass:
        void mymethod()

mod2.pyx

cdef void myapi():
    print("I am OK (from myapi)")

cdef cppclass myclass:
    void mymethod():
        # Using anything C is OK
        cdef int i = 0
        i += 1

        # Crash with Python var
        # Segmentation fault
        j = 0
        j += 1

        # Crash with Python function
        # Segmentation fault
        print("I am crashing!")

mod1.pxd

cdef public api:
    void myfunc()

mod1.pyx

from mod2 cimport myapi, myclass

cdef public cppclass foo:
    void bar():
        print("I am OK (from foo.bar)")

cdef void myfunc():
    print("I am OK")
    myapi()
    cdef foo f
    f.bar()

    # Stack var: Segmentation fault
    print("Calling myclass.mymethod...")
    cdef myclass t
    t.mymethod()

    # # Pointer: Segmentation fault
    # print("Calling myclass.mymethod...")
    # cdef myclass* t2 = new myclass()
    # t2.mymethod()

cpdef test():
    myfunc()

Main Python programme app.py

import mod1
mod1.test()

Result

I am OK
I am OK (from myapi)
I am OK (from foo.bar)
Calling myclass.mymethod...
Segmentation fault

It's a segmentation fault which happens in a Cython struct myclass when using anything Python. Also in mod2, but the global function myapi is OK. From a struct method foo.bar but in the same file is also OK.

I'm using Python 3.8.2, and Cython 0.29.26

mod1 and mod2 are translated into C++ with cython -3 --cplus command, and compiled with gcc to .so files. It happens the same when using Python build tool.

As suggested from https://stackoverflow.com/a/55669343/5581893 I tried to add -DCYTHON_PEP489_MULTI_PHASE_INIT=0 to all gcc commands but no help.

This situation seems strange, why are Python stuff not touch-able from mod2 (fails in struct only)?

Dee
  • 7,455
  • 6
  • 36
  • 70

0 Answers0