python调用python-c-api 的ndarray,如何在python脚本中发送c api数组或结构

Hi,

I am new to python and I can't seem to send array's or struct's params from a python script to a c api. I have sent int's and strings ok.

I have managed to embed python into C that calls a python module script. I am also using SWIG. I will be using python unitttest to test calls to c api's.

I have had a good dig around and all the simple examples show params as int's or strings. There seems no examples of sending arrays or structs. How do I send from a python script a C array or struct to a c api via SWIG?

Is there a conversion? I tried sending "simple_array=array('i',[8,9])" Is it in the swig file? Any help appreciated!/*simple_api.c*/

#includeint Simple_api_Connector_UTF_SimplePointerArrayWriteTestFunc(int *pArray);

int Simple_api_Connector_UTF_SimplePointerArrayWriteTestFunc(int *pArray)

{

printf("Simple_api_Connector_UTF_SimpleArrayWriteTestFunc: array byte 1:%x array byte 2: %x\n", pArray[1], pArray[2]);

return 1;

}

-----------

/*simple_api.i*/

%module simple_api

int Simple_api_Connector_UTF_SimplePointerArrayWriteTestFunc(int *pArray);

-----------

#py_test_python.py

#!/usr/bin/env python

'''py_test_python.py - Python source designed to '''

'''demonstrate the use of python embedding in and out'''

def py_test_python_func():

simple_api.Simple_api_Connector_UTF_ArrayWriteTestFunc([1,2])#

解决方案

Thanks.

I have found a working typemap to convert the array variable in the SWIG interface (.i) file that works for writing an int array from the python script to the c api:

//type map for an input int array, this will calculate the length

%typemap(in) (const int length_in, int *int_in_array)

{

int i;

if (!PyList_Check(

input))

{

PyErr_SetString(PyExc_ValueError, "Expecting a list");

return NULL;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值