Python &引用;不是类型标识符“;Cython中的错误

Python &引用;不是类型标识符“;Cython中的错误,python,cython,Python,Cython,我是Cython的新手,我正在尝试让一个从Python调用C函数的测试项目工作: 测试.cpp: void testFn(int arr[]); void testFn(int arr[]) { arr[0] = 1; arr[1] = 2; } cdef extern from "test.cpp": void testFn(int arr[]) cpdef myTest(*arr): testFn(arr) from distutils.core i

我是Cython的新手,我正在尝试让一个从Python调用C函数的测试项目工作:

测试.cpp

void testFn(int arr[]);

void testFn(int arr[])
{
    arr[0] = 1;
    arr[1] = 2;
} 
cdef extern from "test.cpp":
    void testFn(int arr[])

cpdef myTest(*arr):
    testFn(arr)
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

sourcefiles = ['caller.pyx']
ext_modules = [Extension("caller", sourcefiles)]

setup(
    name = 'test app',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)
调用方.pyx

void testFn(int arr[]);

void testFn(int arr[])
{
    arr[0] = 1;
    arr[1] = 2;
} 
cdef extern from "test.cpp":
    void testFn(int arr[])

cpdef myTest(*arr):
    testFn(arr)
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

sourcefiles = ['caller.pyx']
ext_modules = [Extension("caller", sourcefiles)]

setup(
    name = 'test app',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)
setup.caller.py

void testFn(int arr[]);

void testFn(int arr[])
{
    arr[0] = 1;
    arr[1] = 2;
} 
cdef extern from "test.cpp":
    void testFn(int arr[])

cpdef myTest(*arr):
    testFn(arr)
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

sourcefiles = ['caller.pyx']
ext_modules = [Extension("caller", sourcefiles)]

setup(
    name = 'test app',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)
但当我尝试构建项目时,我得到了一个错误:

$ python setup.caller.py build_ext --inplace
running build_ext
cythoning caller.pyx to caller.c

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "test.cpp":
    void testFn(int arr[])

cpdef myTest(*arr):
     ^
------------------------------------------------------------

caller.pyx:4:6: 'myTest' is not a type identifier
在我的例子中,第二个定义不需要“cpdef”或“cdef”,通常的“def”起到了作用:

def myTest(int[:] arr):
    testFn(&arr[0])

我猜你想写
myTest(int*arr)
。但这也行不通,因为
int*arr
不是python对象。您不应该将其声明为python函数(
cdef
而不是
cpdef
),或者选择其他签名。