A Python-defined class is instantiated in a Python/C extension exactly the same as in Python: by importing it and calling the class object:
// obtain the RealTimeData class
PyObject *get_RealTimeData()
{
PyObject *module = PyImport_ImportModule("CppInterop");
if (!module)
return NULL;
return PyObject_GetAttrString(module, "RealTimeData");
}
static
PyObject *ReadRealTimeDataList(PyObject *self, PyObject *args)
{
static PyObject *RealTimeData = get_RealTimeData();
...
// instantiate the class like you would in Python - by
// calling the class object
dataObj = PyObject_CallFunction(RealTimeData, "sd", "some id", 0.0);
if (!dataObj)
return NULL;
...
}
Several unrelated notes:
RealReadTimeDataList
for some reason doesn't return the data list, but a single-element tuple that holds the list. Since your Python code expects the list itself, just returnlistObj
instead.- Error checking is missing: the code doesn't check the result of
PyString_FromString
,PyFloat_FromDouble
, orPyObject_SetAttrString
. Py_DECREF(listObj)
should be called whenPyList_Append
fails. As written, the code would leaklistObj
in such a situation.