|
From: alvin <ala...@gm...> - 2015-03-24 06:38:14
|
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;
};
%}
*PyTest.py*
import MyProject
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 o.num_of_inputs:
print "input ",o.input_details[i].argName
*main.cpp*
#include <stdio.h>
#include <Python.h>
extern "C" // only needed when compiling in C++
{
#include "MyProject_wrap.c"
}
// struct SPythoned is defined within MyProject_wrap.c just included above
void PythonTest(void)
{
int i;
Py_Initialize(); // Init Python
SWIG_init(); // Initialise SWIG types
init_MyProject(); // Init our project wrapped by Swig
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\"./\")");
SPythoned Object;
PyObject *pMod, *pGlobalDict, *pFunc, *pResult, *pArg;
Object.kerName = strdup("MatMatMul");
Object.kerFileName = strdup("MatMatMul.cl");
Object.devType = strdup("GPU");
Object.num_of_inputs = 2;
Object.num_of_outputs = 1;
Object.input_details = (struct argumentDetails
*)malloc(Object.num_of_inputs * sizeof(argumentDetails));
Object.output_details = (struct argumentDetails
*)malloc(Object.num_of_outputs * sizeof(argumentDetails));
for(i=0;i<Object.num_of_inputs;i++)
{
Object.input_details[i].argName = strdup("a");
Object.input_details[i].argType = strdup("float *");
Object.input_details[i].argSize = 1024;
}
for(i=0;i<Object.num_of_outputs;i++)
{
Object.output_details[i].argName = strdup("b");
Object.output_details[i].argType = strdup("float *");
Object.output_details[i].argSize = 1024;
}
pMod = PyImport_ImportModule("PyTest"); // Load "PyTest.py" (it will
create a compiled version "PyTest.pyc")
if (pMod)
{
pGlobalDict = PyModule_GetDict(pMod); // Get main
dictionary
if (pGlobalDict)
{
pFunc = PyDict_GetItemString(pGlobalDict,
"TestObject"); // Look for function TestObject
if (pFunc)
{
pArg = SWIG_NewPointerObj((void*)&Object,
SWIGTYPE_p_SPythoned, 1); // Get Python Object for our Structure Pointer
if (pArg)
{
pResult =
PyObject_CallFunction(pFunc, "O", pArg);
Py_CLEAR(pResult);
printf("kername %s\tkerfilename
%s\tdevtype %s\n",Object.kerName,Object.kerFileName,Object.devType);
/* for(i=0;i<2;i++)
printf("object kername
%s\n",Object.ker_name[i]);*/
// printf("\nobject kername
%s\n",Object.ker_names.ker_name[i]);
}
}
}
}
Py_Finalize();
}
int main()
{
PythonTest();
return 0;
}
How can I access all the elements of a structre SPythoned from PyTest.py
file.?
--
View this message in context: http://swig.10945.n7.nabble.com/Accessing-structure-members-from-python-file-which-has-been-initialized-in-C-tp14306.html
Sent from the swig-user mailing list archive at Nabble.com.
|