0

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!

PintoDoido
  • 1,011
  • 16
  • 35
  • "Any ideas on how to fix this?" - Yes. Read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (or two). – Jesper Juhl Apr 16 '19 at 14:00
  • 1
    Based on the title: you're looking the wrong side of the equals sign on the line. The problem is with `result` not `pyfoo_T`. – DavidW Apr 16 '19 at 14:22

0 Answers0