|
From: Bob H. <bh...@co...> - 2015-03-24 13:11:05
|
On 3/24/2015 12:26 AM, alvin wrote:
> I'm new to python. I want to send a pointer to a structure from my C code to
> a Python function, and then accessing the C structure from within Python.
>
> My structure is asa follows
> *MyProject.i*
> %module MyProject
> %inline %{
> struct argumentDetails
> {
> char *argName;
> char *argType;
> int argSize;
> };
> struct SPythoned
> {
> char *kerName;
> char *kerFileName;
> char *devType;
> int num_of_inputs;
> int num_of_outputs;
> struct argumentDetails *input_details;
> struct argumentDetails *output_details;
> };
> %}
SWIG does not know that "input_details" and "output_details" are actually
arrays--your C code does, but in the way you've arranged things, SWIG only
sees structure members. So, it only creates simple get/set accessors for the
structure members themselves.
You have to provide the logic that treats these variables as arrays instead of
simple data member values. Your interface file can, for example, extend the
"SPythoned" structure with functions to access and return the array values:
%extend SPythoned
{
struct argumentDetails * get_input_details(int index)
{
if(index < 0 || index >= self->num_of_inputs)
return NULL;
return &self->input_details[index];
}
struct argumentDetails * get_output_details(int index)
{
if(index < 0 || index >= self->num_of_outputs)
return NULL;
return &self->output_details[index];
}
};
%ignore SPythoned::input_details;
%ignore SPythoned::output_details;
Your Python script would be altered as follows:
def TestObject(o):
print "kenel name",o.kerName
print "kernel file name",o.kerFileName
print "dev type",o.devType
print "num_of_inputs ",o.num_of_inputs
* for i in range(o.num_of_inputs):
print "input ",o.get_input_details(i).argName**
*
This produces the following output for me:
kenel name MatMatMul
kernel file name MatMatMul.cl
dev type GPU
num_of_inputs 2
input a
input a
kername MatMatMul kerfilename MatMatMul.cl devtype GPU
Which is, I think, what you are expecting to see.
|