diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-02-01 15:37:53 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-02-01 15:37:53 +0300 |
commit | ffe7bde02c5725993e9c1986bbc8a83174adbbb8 (patch) | |
tree | 14ba26e8e892b701a4cfc4daf9c6cfe1baaad285 /source/blender/python/generic | |
parent | 86f3ba24e4bc5f01b1730d1b48b74b6ce4c659fd (diff) |
correct fix for bug #23871, __main__ module was being overwritten in nested functions, so on returning from calling operators the __main__ module could be cleared and imported modules turn into None
calling
bpy.ops.wm.read_factory_settings()
... would clear a scripts namespace if running directly, not in a module.
Fix by backing up and restoring the __main__ module.
Also found BKE_reportf wasnt printing all reports in background mode as BKE_report() was doing.
Diffstat (limited to 'source/blender/python/generic')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 18 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 3 |
2 files changed, 21 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index d29587a084b..9f7a9f2cabe 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -282,6 +282,10 @@ PyObject *PyC_UnicodeFromByte(const char *str) for 'pickle' to work as well as strings like this... >> foo = 10 >> print(__import__("__main__").foo) +* +* note: this overwrites __main__ which gives problems with nested calles. +* be sure to run PyC_MainModule_Backup & PyC_MainModule_Restore if there is +* any chance that python is in the call stack. *****************************************************************************/ PyObject *PyC_DefaultNameSpace(const char *filename) { @@ -297,6 +301,20 @@ PyObject *PyC_DefaultNameSpace(const char *filename) return PyModule_GetDict(mod_main); } +/* restore MUST be called after this */ +void PyC_MainModule_Backup(PyObject **main_mod) +{ + PyInterpreterState *interp= PyThreadState_GET()->interp; + *main_mod= PyDict_GetItemString(interp->modules, "__main__"); + Py_XINCREF(*main_mod); /* dont free */ +} + +void PyC_MainModule_Restore(PyObject *main_mod) +{ + PyInterpreterState *interp= PyThreadState_GET()->interp; + PyDict_SetItemString(interp->modules, "__main__", main_mod); + Py_XDECREF(main_mod); +} /* Would be nice if python had this built in */ void PyC_RunQuicky(const char *filepath, int n, ...) diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index f786fd80269..ccdbde5cecb 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -39,4 +39,7 @@ const char * PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerce m /* name namespace function for bpy & bge */ PyObject * PyC_DefaultNameSpace(const char *filename); void PyC_RunQuicky(const char *filepath, int n, ...); + +void PyC_MainModule_Backup(PyObject **main_mod); +void PyC_MainModule_Restore(PyObject *main_mod); #endif // PY_CAPI_UTILS_H |