I want to create an instance of a PyTypeObject
in my C++ code Python C-api: I used Create an object using Python's C API as a basis.
I have defined a custom type/class:
typedef struct {
PyObject_HEAD
// Other stuff...
} pyfoo;
static PyTypeObject pyfoo_T = {
PyObject_HEAD_INIT(NULL)
// ...
pyfoo_new,
};
I want to create an instance of the object of type pyfoo in my C++ code. Looking at the aforementioned link, I do this by calling:
pyfoo *result = PyObject_CallFunction((PyObject *)&pyfoo_T, argList);
However, when I use a C++ compiler to compile the module I get the following error:
error: cannot convert ‘PyObject*’ {aka ‘_object*’} to ‘pyfoo*’ in initialization.
If I use a C compiler, it shouts the warning:
warning: initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyTypeObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _typeobject *, struct _object *)’} [-Wincompatible-pointer-types]
How is one supposed to correctly create an instance of a type in the C/C++ code?
Thanks!