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!

2
  • "Any ideas on how to fix this?" - Yes. Read a good book (or two). Commented Apr 16, 2019 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. Commented Apr 16, 2019 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.