Putting a conclusion here for posterity: my particular issue was caused by doing too much Python-y stuff before Py_Initialize
was complete.
My module is built via PyImport_AppendInittab
, which expects a function argument that returns a module object. All of my types are being added to the module, but I encountered this issue with instances not having correct type information. My solution is going to be slightly specific to embedded Python applications, but it should make sense to anybody who is calling Py_Main
for their application too.
To correct it, I did this stuff:
- Don’t call any types or try to create any instances during
PyImport_AppendInitTab
. The types are all readied and added to the module. - After
Py_InitializeFromConfig
has been called, Python is actually ready to use the module. - The module object and type objects I provided to initialize the module do not seem to be the same objects in use at the REPL. Maybe
PyType_Ready
copies thePyTypeObject
struct somewhere else to apply defaults / inheritance? Instead, import the module:PyObject* mobj = PyImport_ImportModule("mcrfpy");
- Instead of using the type objects I defined, get the finalized type out of the module:
PyTypeObject* tobj = (PyTypeObject*)PyObject_GetAttrString(mobj, "Texture");
From there, as Cornelius stated, calling the type object would be a very straightforward solution. I was also able to use the type object’s tobj->tp_new
/ tobj->tp_alloc
to side load or “factory function” my objects without any weird side effects.