From 05f2e47ff091de670e6f871daea60276b8171724 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 24 Nov 2010 10:23:23 +0000 Subject: bugfix [#23871] OSX panel button bug (Python Namespace issue) This is an annoying but which isn't a problem for Python because they don't execute multiple scripts, one after another (there is one __main__ and everything else is a module). So when the __main__ module in sys.modules is overwritten, it decref's the module and clears the dictionary with _PyModule_Clear(), even though the modules dictionary is still in use. Strangely this problem only happens with Python3.1.1 and Python3.2x svn but not 3.1.2 This commit restores the namespace after _PyModule_Clear() sets all its values to None. --- source/blender/python/intern/bpy_interface.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'source/blender/python') diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index a894fa60237..7caf07a0973 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -302,7 +302,7 @@ void BPY_end_python( void ) /* Can run a file or text block */ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports) { - PyObject *py_dict, *py_result= NULL; + PyObject *py_dict= NULL, *py_result= NULL; PyGILState_STATE gilstate; if (fn==NULL && text==NULL) { @@ -373,8 +373,23 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc } else { Py_DECREF( py_result ); } - - PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); + +/* super annoying, undo _PyModule_Clear() */ +#define PYMODULE_CLEAR_WORKAROUND + + if(py_dict) { +#ifdef PYMODULE_CLEAR_WORKAROUND + PyObject *py_dict_back= PyDict_Copy(py_dict); +#endif + /* normal */ + PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); +#ifdef PYMODULE_CLEAR_WORKAROUND + PyDict_Clear(py_dict); + PyDict_Update(py_dict, py_dict_back); + Py_DECREF(py_dict_back); +#endif +#undef PYMODULE_CLEAR_WORKAROUND + } bpy_context_clear(C, &gilstate); -- cgit v1.2.3