diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-07-28 20:26:42 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-07-28 20:26:42 +0400 |
commit | 2c8d7921bc6bc32dd1fa24e3e00cc717c4ed5ccb (patch) | |
tree | 1be7441101d911e75fcbe4dfcf416af341c9e148 /source | |
parent | b51350fb4f93a1e8c2093f99ccb26ebe81ff3fb9 (diff) |
bugfix [#23065] Pickle can not dump instances of user defined classes
- __import__("__main__").__dict__ will now always match the current scripts namespace. (which is what pickle expects).
- __builtins__ as a module rather then a dict from PyEval_GetBuiltins() acts slightly differently, use the module to follow python.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 5f2f37be062..13b8cd4036a 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -152,9 +152,28 @@ void BPY_update_modules( void ) static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) { PyObject *item; - PyObject *dict = PyDict_New( ); - PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) ); - + PyObject *dict; +#if 1 + /* important we use the dict from __main__, this is what python expects + * for 'pickle' to work as well as strings like this... + + >> foo = 10 + >> print(__import__("__main__").foo) + */ + dict= PyModule_GetDict(PyImport_AddModule("__main__")); + PyDict_Clear(dict); + Py_INCREF(dict); + + /* using builtins rather then PyEval_GetBuiltins() + * print's many less items when printing, the modules __dict__ + * this is how python works so better follow. */ + PyDict_SetItemString(dict, "__builtins__", PyImport_AddModule("builtins")); +#else + /* otherwise this works for 99% of cases, from 2.4x */ + dict = PyDict_New(); + PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins()); +#endif + item = PyUnicode_FromString( "__main__" ); PyDict_SetItemString( dict, "__name__", item ); Py_DECREF(item); @@ -388,7 +407,11 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc } else { Py_DECREF( py_result ); } - + + /* so __main__ module isnt left with an invalid __file__ variable which could be confusing */ + if (PyDict_DelItemString(py_dict, "__file__")) + PyErr_Clear(); + Py_DECREF(py_dict); bpy_context_clear(C, &gilstate); |