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;
}