[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
Thu Jul 29 00:19:40 CEST 2010


Setting up a namespace in python isn't well documented and not as
simple as I'd first expected, mailed the python developer list with
some questions relating to this commit.
for the record:
http://mail.python.org/pipermail/python-dev/2010-July/102499.html

On Thu, Jul 29, 2010 at 2:26 AM, Campbell Barton <ideasman42 at gmail.com> wrote:
> 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);
>
>
> _______________________________________________
> Bf-blender-cvs mailing list
> Bf-blender-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-blender-cvs
>



-- 
- Campbell



More information about the Bf-blender-cvs mailing list