Here is my program of cython
cdef struct Node:
int v
Node* next
Node* pre
def f(int N):
cdef:
vector[Node*] narray
int i
narray.assign(N, 0)
for i in xrange(N):
narray[i] = 0
Cython compiling result:
Error compiling Cython file:
------------------------------------------------------------
...
cdef:
vector[Node*] narray
int i
narray.assign(N, 0)
for i in xrange(N):
narray[i] = 0
^
------------------------------------------------------------
testLinkList.pyx:107:14: Compiler crash in AnalyseExpressionsTransform
But I can use push_back()
to append values at the end of the vector or use int
instead of Node*
.
What is wrong?
Cython can call into both C and C++ code, and even subclass C++ classes.
Cython works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While the resulting code is fast, it makes many calls into the CPython interpreter and CPython standard libraries to perform actual work.
What version of Cython are you using? Version 0.20.1 works for me with this code:
# distutils: language=c++
from libcpp.vector cimport vector
cdef struct Node:
int v
Node* next
Node* pre
def f(int N):
cdef:
vector[Node*] narray
int i
narray.assign(N, NULL)
for i in range(N):
narray[i] = NULL
And with this setup.py
file:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("test_vector.pyx"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With