5
我試圖包裹C++函數,使用用Cython簽名無法將「矢量<無符號長>」到Python對象
vector < unsigned long > Optimized_Eratosthenes_sieve(unsigned long max)
。我有一個包含了函數,靜態庫sieve.a文件sieve.h和我的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
但我得到這個「無法轉換」矢量「Python對象」錯誤。我錯過了什麼嗎?
SOLUTION:我從我的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
巴斯蒂安,你是絕對正確的。我是Python與C/C++相結合的領域的noob。我編輯了我的問題以顯示解決方案。 – hymloth 2011-03-24 18:06:43