[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [30843] trunk/blender/source/blender/ python/intern/bpy_interface.c: bugfix [#23065] Pickle can not dump instances of user defined classes

Campbell Barton ideasman42 at gmail.com
Wed Jul 28 18:26:42 CEST 2010


Revision: 30843
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=30843
Author:   campbellbarton
Date:     2010-07-28 18:26:42 +0200 (Wed, 28 Jul 2010)

Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/python/intern/bpy_interface.c

Modified: trunk/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_interface.c	2010-07-28 14:42:03 UTC (rev 30842)
+++ trunk/blender/source/blender/python/intern/bpy_interface.c	2010-07-28 16:26:42 UTC (rev 30843)
@@ -152,9 +152,28 @@
 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 @@
 	} 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);




More information about the Bf-blender-cvs mailing list