In this case, the module is a python script loaded from a file. I can't find anything on the Internet about this.
If I could loop through all objects in the module, I can filter for classes using PyClass_Check. But I don't know how to do this either.
// Load
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
inithello();
PyObject *pModule = PyImport_ImportModule("test123");
if (pModule != NULL) {
// iterate through classes.
Py_DECREF(pModule);
} else {
PyErr_Print();
std::cerr << "Failed to load module" << std::endl;
return 1;
}
Py_Finalize();
The reason for this is that the user will be defining game content using classes derived from base classes.
class grass_tile(game.Tile):
def __init__(x, y):
// initialise
I have tried using PyObject_GetIter() on the module, but it returns NULL (obviously not iterate-able that way.)
Worked it out. Hope this helps someone else.
PyObject *dict = PyModule_GetDict(pModule);
PyObject *key, *value = NULL;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
if (PyClass_Check(value)) {
std::cerr << "IsClass" << std::endl;
} else {
std::cerr << "NotClass" << std::endl;
}
}