Is it possible to apply a type intto a class type?
I have the following code in C:
#include "Python.h"
#define PYTHON_FILENAME "modelparam"
void getmodelparam(long pModelParam) ;
typedef struct {
int seconds;
int nanoseconds;
} someTime;
int main ()
{
someTime *pSome ;
long a ;
printf ("Testing the python interfaces\n") ;
pSome = (someTime *) calloc(1, sizeof(someTime)) ;
pSome->seconds = 10 ;
pSome->nanoseconds = 20 ;
a = (long) pSome ;
printf ("a is %d, pSome is %d\n", a, pSome) ;
getmodelparam(a) ;
printf ("After the python call values are : %d, %d\n",pSome->seconds, pSome->nanoseconds) ;
return 0 ;
}
void getmodelparam(long pModelParam)
{
PyObject *pName ;
PyObject *pModule ;
PyObject *pDict ;
PyObject *pFunc ;
int iSize = 0 ;
char pcFunctionName[] = "modifymodelparam" ;
double dTemp1, dTemp2 ;
Py_Initialize() ;
pName = PyUnicode_FromString(PYTHON_FILENAME);
if (NULL != pName)
{
pModule = PyImport_Import(pName);
Py_DECREF(pName) ;
if (NULL != pModule)
{
pFunc = PyObject_GetAttrString(pModule, pcFunctionName);
if (pFunc && PyCallable_Check(pFunc))
{
PyObject *pResult = PyObject_CallFunction(pFunc,"i", pModelParam) ;
}
else
{
printf ("Some error with the function\n") ;
}
}
else
{
printf ("Couldnt load the module %s\n", PYTHON_FILENAME) ;
}
}
else
{
printf ("Couldnt convert the name of the module to python name\n") ;
}
Py_DECREF(pModule) ;
Py_DECREF(pFunc) ;
Py_DECREF(pName) ;
Py_Finalize() ;
}
And in Python code:
import ctypes
class someTime(ctypes.Structure):
_fields_ = [("seconds", ctypes.c_uint),
("nanoseconds", ctypes.c_uint)]
def modifymodelparam(m):
n = someTime(m)
print ('Seconds', n.seconds)
How can I deduce the address passed from C to a class type in Python so that I can access these class parameters or indirectly say access to structure parameters?
source
share