我正在尝试用签名包装一个 c++ 函数
vector < unsigned long > Optimized_Eratosthenes_sieve(unsigned long max)
使用赛通。我有一个包含该函数的文件 sieve.h,一个静态库 sieve.a,我的 setup.py 如下:
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
)
在我的 sieve.pyx 中,我正在尝试:
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
但我收到此“无法将 'vector' 转换为 Python 对象”错误。我错过了什么吗?
解决方案:我必须从我的 OES 函数返回一个 python 对象:
def OES(unsigned long a):
cdef vector[unsigned long] aa
cdef int N
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