Recreating bge python modules instead of using existing

All of the initXPythonBinding functions are changed to always creating the module instead of importing if previously existing.
I can instead only remove the module return when the import is ok, so that it always inits. But then, I don't see the point in importing.

I make sure that these functions are called only once per run, inside initBGE.
This was not the case with GameTypes. I moved initPyTypes inside of initGameTypesPythonBinding due to that.

I reorganized initGamePlayerPythonScripting and initGamePythonScripting so that they run things in the same order.
initGamePlayerPythonScripting imports mathutils and aud, the other only aud. Shouldn't it be the same for both?

Reviewers: campbellbarton

Subscribers: sybren

Projects: #game_engine, #game_python

Differential Revision: https://developer.blender.org/D1070
This commit is contained in:
Ines Almeida 2015-02-09 20:56:38 +00:00
parent 31e26bb83b
commit a088b9488d
5 changed files with 43 additions and 128 deletions

@ -753,19 +753,8 @@ PyMODINIT_FUNC initConstraintPythonBinding()
PyObject *d; PyObject *d;
PyObject *item; PyObject *item;
/* Use existing module where possible m = PyModule_Create(&PhysicsConstraints_module_def);
* be careful not to init any runtime vars after this */ PyDict_SetItemString(PySys_GetObject("modules"), PhysicsConstraints_module_def.m_name, m);
m = PyImport_ImportModule( "PhysicsConstraints" );
if (m) {
Py_DECREF(m);
return m;
}
else {
PyErr_Clear();
m = PyModule_Create(&PhysicsConstraints_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), PhysicsConstraints_module_def.m_name, m);
}
// Add some symbolic constants to the module // Add some symbolic constants to the module
d = PyModule_GetDict(m); d = PyModule_GetDict(m);

@ -1577,21 +1577,11 @@ PyMODINIT_FUNC initGameLogicPythonBinding()
gUseVisibilityTemp=false; gUseVisibilityTemp=false;
PyObjectPlus::ClearDeprecationWarning(); /* Not that nice to call here but makes sure warnings are reset between loading scenes */ PyObjectPlus::ClearDeprecationWarning(); /* Not that nice to call here but makes sure warnings are reset between loading scenes */
/* Use existing module where possible m = PyModule_Create(&GameLogic_module_def);
* be careful not to init any runtime vars after this */ PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m);
m = PyImport_ImportModule( "GameLogic" );
if (m) {
Py_DECREF(m);
return m;
}
else {
PyErr_Clear();
// Create the module and add the functions
m = PyModule_Create(&GameLogic_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m);
}
// Add some symbolic constants to the module // Add some symbolic constants to the module
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
@ -2116,7 +2106,6 @@ PyMODINIT_FUNC initBGE(void)
PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule); PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
Py_INCREF(submodule); Py_INCREF(submodule);
/* GameTypes is initted *after* in initPyTypes() */
PyModule_AddObject(mod, "types", (submodule = initGameTypesPythonBinding())); PyModule_AddObject(mod, "types", (submodule = initGameTypesPythonBinding()));
PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule); PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
Py_INCREF(submodule); Py_INCREF(submodule);
@ -2196,8 +2185,6 @@ PyObject *initGamePlayerPythonScripting(Main *maggie, int argc, char** argv)
initPySysObjects(maggie); initPySysObjects(maggie);
PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE());
/* mathutils types are used by the BGE even if we don't import them */ /* mathutils types are used by the BGE even if we don't import them */
{ {
PyObject *mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0); PyObject *mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0);
@ -2212,7 +2199,7 @@ PyObject *initGamePlayerPythonScripting(Main *maggie, int argc, char** argv)
} }
#endif #endif
initPyTypes(); PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE());
first_time = false; first_time = false;
@ -2254,7 +2241,9 @@ PyObject *initGamePythonScripting(Main *maggie)
{ {
/* no need to Py_SetProgramName, it was already taken care of in BPY_python_start */ /* no need to Py_SetProgramName, it was already taken care of in BPY_python_start */
PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE()); bpy_import_main_set(maggie);
initPySysObjects(maggie);
#ifdef WITH_AUDASPACE #ifdef WITH_AUDASPACE
/* accessing a SoundActuator's sound results in a crash if aud is not initialized... */ /* accessing a SoundActuator's sound results in a crash if aud is not initialized... */
@ -2264,11 +2253,7 @@ PyObject *initGamePythonScripting(Main *maggie)
} }
#endif #endif
initPyTypes(); PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE());
bpy_import_main_set(maggie);
initPySysObjects(maggie);
PyObjectPlus::NullDeprecationWarning(); PyObjectPlus::NullDeprecationWarning();
@ -2342,20 +2327,9 @@ PyMODINIT_FUNC initRasterizerPythonBinding()
PyObject *m; PyObject *m;
PyObject *d; PyObject *d;
/* Use existing module where possible m = PyModule_Create(&Rasterizer_module_def);
* be careful not to init any runtime vars after this */ PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m);
m = PyImport_ImportModule( "Rasterizer" );
if (m) {
Py_DECREF(m);
return m;
}
else {
PyErr_Clear();
// Create the module and add the functions
m = PyModule_Create(&Rasterizer_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m);
}
// Add some symbolic constants to the module // Add some symbolic constants to the module
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
@ -2478,19 +2452,8 @@ PyMODINIT_FUNC initGameKeysPythonBinding()
PyObject *m; PyObject *m;
PyObject *d; PyObject *d;
/* Use existing module where possible */ m = PyModule_Create(&GameKeys_module_def);
m = PyImport_ImportModule( "GameKeys" ); PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m);
if (m) {
Py_DECREF(m);
return m;
}
else {
PyErr_Clear();
// Create the module and add the functions
m = PyModule_Create(&GameKeys_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m);
}
// Add some symbolic constants to the module // Add some symbolic constants to the module
d = PyModule_GetDict(m); d = PyModule_GetDict(m);

@ -163,23 +163,34 @@ static void PyType_Ready_ADD(PyObject *dict, PyTypeObject *tp, PyAttributeDef *a
#define PyType_Ready_Attr(d, n, i) PyType_Ready_ADD(d, &n::Type, n::Attributes, NULL, i) #define PyType_Ready_Attr(d, n, i) PyType_Ready_ADD(d, &n::Type, n::Attributes, NULL, i)
#define PyType_Ready_AttrPtr(d, n, i) PyType_Ready_ADD(d, &n::Type, n::Attributes, n::AttributesPtr, i) #define PyType_Ready_AttrPtr(d, n, i) PyType_Ready_ADD(d, &n::Type, n::Attributes, n::AttributesPtr, i)
void initPyTypes(void)
PyDoc_STRVAR(GameTypes_module_documentation,
"This module provides access to the game engine data types."
);
static struct PyModuleDef GameTypes_module_def = {
PyModuleDef_HEAD_INIT,
"GameTypes", /* m_name */
GameTypes_module_documentation, /* m_doc */
0, /* m_size */
NULL, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
PyMODINIT_FUNC initGameTypesPythonBinding(void)
{ {
PyObject *m;
PyObject *dict;
/* m = PyModule_Create(&GameTypes_module_def);
* initPyObjectPlusType(BL_ActionActuator::Parents); PyDict_SetItemString(PySys_GetObject("modules"), GameTypes_module_def.m_name, m);
* .....
*/
/* Use existing module where possible */ dict = PyModule_GetDict(m);
PyObject *mod = initGameTypesPythonBinding();
/* For now just do PyType_Ready */
PyObject *dict = PyModule_GetDict(mod);
PyDict_SetItemString(PySys_GetObject("modules"), "GameTypes", mod);
Py_DECREF(mod);
for (int init_getset= 1; init_getset > -1; init_getset--) { /* run twice, once to init the getsets another to run PyType_Ready */ for (int init_getset= 1; init_getset > -1; init_getset--) { /* run twice, once to init the getsets another to run PyType_Ready */
PyType_Ready_Attr(dict, BL_ActionActuator, init_getset); PyType_Ready_Attr(dict, BL_ActionActuator, init_getset);
PyType_Ready_Attr(dict, BL_Shader, init_getset); PyType_Ready_Attr(dict, BL_Shader, init_getset);
@ -269,42 +280,6 @@ void initPyTypes(void)
KX_GameObject_Mathutils_Callback_Init(); KX_GameObject_Mathutils_Callback_Init();
KX_ObjectActuator_Mathutils_Callback_Init(); KX_ObjectActuator_Mathutils_Callback_Init();
#endif #endif
}
PyDoc_STRVAR(GameTypes_module_documentation,
"This module provides access to the game engine data types."
);
static struct PyModuleDef GameTypes_module_def = {
PyModuleDef_HEAD_INIT,
"GameTypes", /* m_name */
GameTypes_module_documentation, /* m_doc */
0, /* m_size */
NULL, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
PyMODINIT_FUNC initGameTypesPythonBinding(void)
{
PyObject *m;
/* Use existing module where possible */
m = PyImport_ImportModule( "GameTypes" );
if (m) {
Py_DECREF(m);
return m;
}
else {
PyErr_Clear();
// Create the module and add the functions
m = PyModule_Create(&GameTypes_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), GameTypes_module_def.m_name, m);
}
return m; return m;
} }

@ -34,7 +34,6 @@
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
#include <Python.h> #include <Python.h>
void initPyTypes(void);
PyMODINIT_FUNC initGameTypesPythonBinding(void); PyMODINIT_FUNC initGameTypesPythonBinding(void);
#endif #endif

@ -191,20 +191,9 @@ PyMODINIT_FUNC initVideoTexturePythonBinding(void)
if (PyType_Ready(&TextureType) < 0) if (PyType_Ready(&TextureType) < 0)
return NULL; return NULL;
/* Use existing module where possible m = PyModule_Create(&VideoTexture_module_def);
* be careful not to init any runtime vars after this */ PyDict_SetItemString(PySys_GetObject("modules"), VideoTexture_module_def.m_name, m);
m = PyImport_ImportModule( "VideoTexture" );
if (m) {
Py_DECREF(m);
return m;
}
else {
PyErr_Clear();
m = PyModule_Create(&VideoTexture_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), VideoTexture_module_def.m_name, m);
}
if (m == NULL) if (m == NULL)
return NULL; return NULL;