简体   繁体   English

Cython错误:将Integer数组传递给函数时,无法将Python对象转换为类型'int *'错误

[英]Cython error: Cannot convert Python object to type 'int *' error, when passing an Integer array to a function

I am trying to pass an integer array to a function in Cython and I cannot understand why I get the error mentioned in the title. 我试图将整数数组传递给Cython中的函数,但我不明白为什么会出现标题中提到的错误。

A sample code of what I am trying to do is the following: 我正在尝试执行的示例代码如下:

cpdef foo(int *table):
for i in range(10):
    table[i] = i

cdef int *temp=<int *>malloc(10*sizeof(int))
foo(temp)

for i in range(10):
    print temp[i]

I would appreciate any pointers as to how to pass the array correctly to the function. 我将不胜感激如何将数组正确传递给函数。 Thank you. 谢谢。

The issue here is the function is cpdef - this means it can be called from both C and Python code, and means all arguments must be Python objects (otherwise how would one call it from Python?) 这里的问题是该函数是cpdef这意味着可以从C和Python代码中调用它,并且意味着所有参数都必须是Python对象(否则,如何从Python调用它?)

Declare it as cdef instead. 将其声明为cdef

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误消息“无法转换<type_name> * 到 Python 对象”在 Cython 中 - Error message "Cannot convert <type_name>* to Python object" in Cython 自由函数的Cython编译错误(无法将Python对象参数转换为'FooBar *'类型) - Cython compilation error for free function (Cannot convert Python object argument to type 'FooBar *') Cython:“无法转换 Python 对象”错误 - Cython: “Cannot convert Python object” error “无法将 Python 对象参数转换为类型 &#39;<typename> &#39;&quot; - 用 Cython 包装 c++ 类时出错 - "Cannot convert Python object argument to type '<typename>'" - error while wrapping c++-classes with Cython 类型错误 int object 在 function python 上不可迭代 - type error int object is not iterable on function python Python pydantic model 传递 None 作为 int 值不是有效的 integer (type=type_error.integer) - Python pydantic model passing None as int value is not a valid integer (type=type_error.integer) 将对象传递给函数时类型错误 - Type Error when passing an object into a function Cython无法转换为int? - Cython cannot convert to int? 如何修复编译器错误:无法使用Cython + numpy转换为指针类型? - How to fix compiler error: cannot convert to a pointer type with Cython + numpy? Numpy-&gt; Cython转换:编译错误:无法将&#39;npy_intp *&#39;转换为Python对象 - Numpy->Cython conversion: Compile error:Cannot convert 'npy_intp *' to Python object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM