python - 在 cython 函数中使用 python ctypes 数组数据

标签 python cython ctypes

我有 python ctypes 数组。我想在 cython def 和 cdef 函数中使用它。

import ctypes
from my_cython_extention import *

class Test_Node(ctypes.Structure):
    _fields_ = [
        ("i", ctypes.c_int),
        ("f", ctypes.c_float),
    ]
my_array = (Test_Node * 10)()
my_cython_function(my_array)

我应该如何定义 cython 函数 my_cython_function 来访问 my_array 数据?我可以获取 my_array C++ 数据指针吗?

最佳答案

我找到了问题的答案:

cdef my_cython_function_c(
    Test_Node *arr_addr, int arr_size
):
    for i in range(arr_size):
        printf("%i\n", arr_addr.i)
        inc(arr_addr)

def my_cython_function(arr):
    my_cython_function_c(<Test_Node *><long>ctypes.addressof(arr), len(arr))

关于python - 在 cython 函数中使用 python ctypes 数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184882/

相关文章:

python - numpy:找到矩阵中满足相邻条件的所有数字对

python - 使用自定义对象查询 Python 字典键

python - 在 Python 中编写一个 'print' 函数

python - 在 Django 中使用 Cython。是否有意义?

python-3.x - 用 Cython 编译 python3 脚本 | CentOS 6.x

python - 将 Cython 标记为构建依赖项?

python - readline 在读取 "watch"命令输出时卡在 paramiko.Channel 上

c++ - 如何使用 CTypes 将 wchar_t**(UNICODE 字符串的 null 终止数组)返回到 Python 脚本

python - 我可以显式关闭 ctypes CDLL 吗?

python - ctypes:将字符串转换为函数?