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.
This commit is contained in:
2010-11-24 10:23:23 +00:00
parent a5cecd8284
commit 05f2e47ff0

View File

@@ -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);