Cannot convert 'vector<unsigned long>' to Python object
I am trying to wrap a c++ function with signature
vector < unsigned long > Optimized_Eratosthenes_sieve(unsigned long max)
using Cython. I have a file sieve.h containing the function, a static library sieve.a and my setup.py is as follows:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("sieve",
["sieve.pyx"],
language='c++',
extra_objects=["sieve.a"],
)]
setup(
name = 'sieve',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
In my sieve.pyx I am trying:
from libcpp.vector cimport vector
cdef extern from "sieve.h":
vector[unsigned long] Optimized_Eratosthenes_sieve(unsigned long max)
def OES(unsigned long a):
return Optimized_Eratosthenes_sieve(a) # this is were the error occurs
but I am getting this "Cannot convert 'vector' to Python object" error. Am I missing something?
SOLUTION: I have to return a python object from my OES function:
def OES(unsigned long a):
cdef vector[unsigned long] aa
cdef int N
开发者_如何学Python b = []
aa = Optimized_Eratosthenes_sieve(a)
N=aa.size()
for i in range(N):
b.append(aa[i]) # creates the list from the vector
return b
If you only need to call you function for C++, declare it with cdef
instead of def
.
On the other hand, if you need to call it from Python, your function has to return a Python object. In this case, you'll probably make it return a Python list of integers.
精彩评论