Cannot convert 'vector <unsigned long>' to a Python object

I am trying to wrap a C ++ function with a signature

vector < unsigned long > Optimized_Eratosthenes_sieve(unsigned long max)

using cython. I have a sieve.h file containing this function, the sieve.a static library and my setup.py looks like this:

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 try:

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 get this error “I can’t convert the vector” to a Python object. Am I missing something?

SOLUTION: I need to return a python object from my OES function:

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
+5
source share
1 answer

If you only need to call a function for C ++, declare it with cdefinstead def.

, Python, Python. , , Python.

+1

All Articles