donglu9872 2018-08-14 15:37
浏览 120
已采纳

如何使用ctypes将数组从Go [lang]返回到Python?

I am trying to write some code that creates an array in GoLang, and returns it to a python script ctypes (and some numpy). What I have got so far doesn't work, and I cannot figure out why... I would appreciate any help!

My Go code goes something like this:

func Function(physics_stuff... float64,  N int ) []float64{
    result := make([]float64, N)
    for i:= 0; i< N; i++{
        result[i] =  blah....
    }
    return result;
}

and I am currently trying to import this functionality to python using:

from ctypes import c_double, cdll, c_int
from numpy.ctypeslib import ndpointer

lib = cdll.LoadLibrary("./go/library.so")
lib.Function.argtypes = [c_double]*6 + [c_int]

def lovely_python_function(stuff..., N):
    lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
    return lib.Function(stuff..., N)

This python function never returns. Other functions from the same library work just fine, but they all return a single float64 (c_double in python).

  • 写回答

1条回答 默认 最新

  • dsieyx2015 2018-08-18 20:46
    关注

    In your code restype is expecting _ndtpr type, see:

    lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
    

    See too in numpy document:

    def ndpointer(dtype=None, ndim=None, shape=None, flags=None)

    [others texts]

    Returns

    klass : ndpointer type object

    A type object, which is an _ndtpr instance containing
    dtype, ndim, shape and flags information.

    [others texts]

    In this way lib.Function.restype is pointer type which appropriated type in Golang must be unsafe.Pointer.

    However you want of an slice that need be passed as pointer:

    func Function(s0, s1, s2 float64, N int) unsafe.Pointer {
        result := make([]float64, N)
        for i := 0; i < N; i++ {
            result[i] = (s0 + s1 + s2)
        }
        return unsafe.Pointer(&result)//<-- pointer of result
    }
    

    This cause a problem in Rules for passing pointers between Go and C.

    1. C code may not keep a copy of a Go pointer after the call returns.

    Source: https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md

    So you must convert unsafe.Pointer to uintptr golang type.

    func Function(s0, s1, s2 float64, N int) uintptr {
        result := make([]float64, N)
        for i := 0; i < N; i++ {
            result[i] = (s0 + s1 + s2)
        }
        return uintptr(unsafe.Pointer(&result[0]))//<-- note: result[0]
    }
    

    In this way you will work fine!

    Note: The structure of slice in C is represented by typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;, but C expect only data, for this is need result only void *data (first field, or field[0]).

    PoC: https://github.com/ag-studies/stackoverflow-pointers-ref-in-golang

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀