[Bf-taskforce25] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19666] ...
Ton Roosendaal
ton at blender.org
Mon Apr 13 15:03:40 CEST 2009
Hi Campbell,
This call:
PyImport_ImportModuleLevel()
is new in Python 2.5. Maybe it's easier for our code to drop support of
older python versions? Put it in the lib dir in svn or what was your
idea?
-Ton-
------------------------------------------------------------------------
Ton Roosendaal Blender Foundation ton at blender.org www.blender.org
Blender Institute BV Entrepotdok 57A 1018AD Amsterdam The Netherlands
On 11 Apr, 2009, at 18:17, Campbell Barton wrote:
> Revision: 19666
>
> http://projects.blender.org/plugins/scmsvn/viewcvs.php?
> view=rev&root=bf-blender&revision=19666
> Author: campbellbarton
> Date: 2009-04-11 18:17:39 +0200 (Sat, 11 Apr 2009)
>
> Log Message:
> -----------
> Added back importing UI scripts rather then running,
> The bug was todo with bpy.data and bpy.types becoming invalid,
> temporary fix is to re-assign them to the bpy module before running
> python operators or panels.
> will look into a nicer way to get this working.
>
> Modified Paths:
> --------------
> branches/blender2.5/blender/release/ui/buttons_objects.py
> branches/blender2.5/blender/source/blender/python/BPY_extern.h
>
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_interface.c
>
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_operator_wrap.c
>
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_panel_wrap.c
> branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
> branches/blender2.5/blender/source/blender/python/intern/bpy_util.h
>
> Modified: branches/blender2.5/blender/release/ui/buttons_objects.py
> ===================================================================
> ---
> branches/blender2.5/blender/release/ui/buttons_objects.py 2009-04-11
> 16:10:15 UTC (rev 19665)
> +++
> branches/blender2.5/blender/release/ui/buttons_objects.py 2009-04-11
> 16:17:39 UTC (rev 19666)
> @@ -1,4 +1,4 @@
> -# import bpy
> +import bpy
>
> class OBJECT_PT_transform(bpy.types.Panel):
> __label__ = "Transform"
>
> Modified:
> branches/blender2.5/blender/source/blender/python/BPY_extern.h
> ===================================================================
> ---
> branches/blender2.5/blender/source/blender/python/BPY_extern.h 2009
> -04-11 16:10:15 UTC (rev 19665)
> +++
> branches/blender2.5/blender/source/blender/python/BPY_extern.h 2009
> -04-11 16:17:39 UTC (rev 19666)
> @@ -101,6 +101,7 @@
> int BPY_run_script_space_draw(struct bContext *C, struct SpaceScript
> * sc); // 2.5 working
> void BPY_run_ui_scripts(struct bContext *C);
> // int BPY_run_script_space_listener(struct bContext *C, struct
> SpaceScript * sc, struct ARegion *ar, struct wmNotifier *wmn); // 2.5
> working
> + void BPY_update_modules( void ); // XXX - annoying, need this for
> pointers that get out of date
>
>
>
>
> Modified:
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_interface.c
> ===================================================================
> ---
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_interface.c 2009-04-11 16:10:15 UTC (rev 19665)
> +++
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_interface.c 2009-04-11 16:17:39 UTC (rev 19666)
> @@ -26,6 +26,7 @@
> #include "MEM_guardedalloc.h"
>
> #include "BLI_util.h"
> +#include "BLI_string.h"
>
> #include "BKE_context.h"
> #include "BKE_text.h"
> @@ -39,22 +40,13 @@
> }
>
>
> /
> ***********************************************************************
> ******
> -* Description: This function creates a new Python dictionary object.
> +* Description: Creates the bpy module and adds it to sys.modules for
> importing
>
> ***********************************************************************
> ******/
> -
> -static PyObject *CreateGlobalDictionary( bContext *C )
> +static void bpy_init_modules( void )
> {
> PyObject *mod;
> - PyObject *dict = PyDict_New( );
> - PyObject *item = PyUnicode_FromString( "__main__" );
> - PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) );
> - PyDict_SetItemString( dict, "__name__", item );
> - Py_DECREF(item);
>
> - /* add bpy to global namespace */
> mod = PyModule_New("bpy");
> - PyDict_SetItemString( dict, "bpy", mod );
> - Py_DECREF(mod);
>
> PyModule_AddObject( mod, "data", BPY_rna_module() );
> /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
> @@ -62,12 +54,35 @@
> PyModule_AddObject( mod, "ops", BPY_operator_module() );
> PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very
> experemental, consider this a test, especially PyCObject is not meant
> to be perminant
>
> + /* add the module so we can import it */
> + PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
> + Py_DECREF(mod);
> +}
> +
> +void BPY_update_modules( void )
> +{
> + PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL,
> 0);
> + PyModule_AddObject( mod, "data", BPY_rna_module() );
> + PyModule_AddObject( mod, "types", BPY_rna_types() );
> +}
> +
> +/
> ***********************************************************************
> ******
> +* Description: This function creates a new Python dictionary object.
> +**********************************************************************
> *******/
> +static PyObject *CreateGlobalDictionary( bContext *C )
> +{
> + PyObject *mod;
> + PyObject *dict = PyDict_New( );
> + PyObject *item = PyUnicode_FromString( "__main__" );
> + PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) );
> + PyDict_SetItemString( dict, "__name__", item );
> + Py_DECREF(item);
> +
> // XXX - evil, need to access context
> item = PyCObject_FromVoidPtr( C, NULL );
> PyDict_SetItemString( dict, "__bpy_context__", item );
> Py_DECREF(item);
>
> -
> // XXX - put somewhere more logical
> {
> PyMethodDef *ml;
> @@ -83,13 +98,18 @@
> }
> }
>
> + /* add bpy to global namespace */
> + mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
> + PyDict_SetItemString( dict, "bpy", mod );
> + Py_DECREF(mod);
> +
> return dict;
> }
>
> void BPY_start_python( void )
> {
> PyThreadState *py_tstate = NULL;
> -
> +
> Py_Initialize( );
>
> //PySys_SetArgv( argc_copy, argv_copy );
> @@ -97,8 +117,11 @@
> /* Initialize thread support (also acquires lock) */
> PyEval_InitThreads();
>
> - // todo - sys paths - our own imports
>
> + /* bpy.* and lets us import it */
> + bpy_init_modules();
> +
> +
> py_tstate = PyGILState_GetThisThreadState();
> PyEval_ReleaseThread(py_tstate);
>
> @@ -304,16 +327,30 @@
> }
> #endif
>
> +// #define TIME_REGISTRATION
> +
> +#ifdef TIME_REGISTRATION
> +#include "PIL_time.h"
> +#endif
> +
> /* XXX this is temporary, need a proper script registration system
> for 2.5 */
> -void BPY_run_ui_scripts(bContext *C)
> +void BPY_run_ui_scripts(void)
> {
> +#ifdef TIME_REGISTRATION
> + double time = PIL_check_seconds_timer();
> +#endif
> DIR *dir;
> struct dirent *de;
> - struct stat status;
> char *file_extension;
> char path[FILE_MAX];
> char *dirname= BLI_gethome_folder("ui");
> -
> + int filelen; /* filename length */
> +
> + PyGILState_STATE gilstate;
> + PyObject *mod;
> + PyObject *sys_path_orig;
> + PyObject *sys_path_new;
> +
> if(!dirname)
> return;
>
> @@ -321,23 +358,49 @@
>
> if(!dir)
> return;
> -
> - if (dir != NULL) {
> - while((de = readdir(dir)) != NULL) {
> - BLI_make_file_string("/", path, dirname, de->d_name);
> +
> + gilstate = PyGILState_Ensure();
> +
> + /* backup sys.path */
> + sys_path_orig= PySys_GetObject("path");
> + Py_INCREF(sys_path_orig); /* dont free it */
> +
> + sys_path_new= PyList_New(1);
> + PyList_SET_ITEM(sys_path_new, 0, PyUnicode_FromString(dirname));
> + PySys_SetObject("path", sys_path_new);
> + Py_DECREF(sys_path_new);
> +
> +
> + while((de = readdir(dir)) != NULL) {
> + /* We could stat the file but easier just to let python
> + * import it and complain if theres a problem */
> +
> + file_extension = strstr(de->d_name, ".py");
> +
> + if(file_extension && *(file_extension + 3) == '\0') {
> + filelen = strlen(de->d_name);
> + BLI_strncpy(path, de->d_name, filelen-2); /* cut off the .py on
> copy */
>
> - stat(path, &status);
> -
> - /* run if it is a .py file */
> - if(S_ISREG(status.st_mode)) {
> - file_extension = strstr(de->d_name, ".py");
> -
> - if(file_extension && *(file_extension + 3) == '\0')
> - BPY_run_python_script(C, path, NULL);
> + mod= PyImport_ImportModuleLevel(path, NULL, NULL, NULL, 0);
> + if (mod) {
> + Py_DECREF(mod);
> }
> + else {
> + PyErr_Print();
> + fprintf(stderr, "unable to import \"%s\" %s/%s\n", path,
> dirname, de->d_name);
> + }
> +
> }
> + }
>
> - closedir(dir);
> - }
> + closedir(dir);
> +
> + PySys_SetObject("path", sys_path_orig);
> + Py_DECREF(sys_path_orig);
> +
> + PyGILState_Release(gilstate);
> +#ifdef TIME_REGISTRATION
> + printf("script time %f\n", (PIL_check_seconds_timer()-time));
> +#endif
> }
>
>
> Modified:
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_operator_wrap.c
> ===================================================================
> ---
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_operator_wrap.c 2009-04-11 16:10:15 UTC (rev 19665)
> +++
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_operator_wrap.c 2009-04-11 16:17:39 UTC (rev 19666)
> @@ -181,6 +181,8 @@
> #define PYOP_INVOKE 2
> #define PYOP_POLL 3
>
> +extern void BPY_update_modules( void ); //XXX temp solution
> +
> static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op,
> wmEvent *event)
> {
> PyObject *py_class = op->type->pyop_data;
> @@ -189,6 +191,8 @@
> int ret_flag= (mode==PYOP_POLL ? 0:OPERATOR_CANCELLED);
>
> PyGILState_STATE gilstate = PyGILState_Ensure();
> +
> + BPY_update_modules(); // XXX - the RNA pointers can change so update
> before running, would like a nicer solutuon for this.
>
> args = PyTuple_New(1);
> PyTuple_SET_ITEM(args, 0, PyObject_GetAttrString(py_class,
> "__rna__")); // need to use an rna instance as the first arg
>
> Modified:
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_panel_wrap.c
> ===================================================================
> ---
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_panel_wrap.c 2009-04-11 16:10:15 UTC (rev 19665)
> +++
> branches/blender2.5/blender/source/blender/python/intern/
> bpy_panel_wrap.c 2009-04-11 16:17:39 UTC (rev 19666)
> @@ -44,6 +44,8 @@
> #define PYPANEL_DRAW 1
> #define PYPANEL_POLL 2
>
> +extern void BPY_update_modules( void ); //XXX temp solution
> +
> static int PyPanel_generic(int mode, const bContext *C, Panel *pnl)
> {
> PyObject *py_class= (PyObject *)(pnl->type->py_data);
> @@ -55,6 +57,8 @@
>
> PyGILState_STATE gilstate = PyGILState_Ensure();
>
> + BPY_update_modules(); // XXX - the RNA pointers can change so update
> before running, would like a nicer solutuon for this.
> +
> args = PyTuple_New(1);
> RNA_pointer_create(&CTX_wm_screen(C)->id, pnl->type->srna, pnl,
> &panelptr);
> PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&panelptr));
>
> Modified:
> branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
> ===================================================================
> ---
> branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
> 2009-04-11 16:10:15 UTC (rev 19665)
> +++
> branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
> 2009-04-11 16:17:39 UTC (rev 19666)
> @@ -1714,22 +1714,23 @@
> return list;
> }
>
> -PyTypeObject pyrna_basetype_Type;
> +PyTypeObject pyrna_basetype_Type = BLANK_PYTHON_TYPE;
>
> PyObject *BPY_rna_types(void)
> {
> BPy_BaseTypeRNA *self;
>
> - memset(&pyrna_basetype_Type, 0, sizeof(pyrna_basetype_Type));
> - pyrna_basetype_Type.tp_name = "RNA_Types";
> - pyrna_basetype_Type.tp_basicsize = sizeof( BPy_BaseTypeRNA );
> - pyrna_basetype_Type.tp_getattro = ( getattrofunc
> )pyrna_basetype_getattro;
> - pyrna_basetype_Type.tp_flags = Py_TPFLAGS_DEFAULT;
> - pyrna_basetype_Type.tp_methods = pyrna_basetype_methods;
> + if ((pyrna_basetype_Type.tp_flags & Py_TPFLAGS_READY)==0) {
> + pyrna_basetype_Type.tp_name = "RNA_Types";
> + pyrna_basetype_Type.tp_basicsize = sizeof( BPy_BaseTypeRNA );
> + pyrna_basetype_Type.tp_getattro = ( getattrofunc
> )pyrna_basetype_getattro;
> + pyrna_basetype_Type.tp_flags = Py_TPFLAGS_DEFAULT;
> + pyrna_basetype_Type.tp_methods = pyrna_basetype_methods;
> +
> + if( PyType_Ready( &pyrna_basetype_Type ) < 0 )
> + return NULL;
> + }
>
> - if( PyType_Ready( &pyrna_basetype_Type ) < 0 )
> - return NULL;
> -
> self= (BPy_BaseTypeRNA *)PyObject_NEW( BPy_BaseTypeRNA,
> &pyrna_basetype_Type );
>
>
> @@ Diff output truncated at 10240 characters. @@
>
> _______________________________________________
> Bf-blender-cvs mailing list
> Bf-blender-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-blender-cvs
>
More information about the Bf-taskforce25
mailing list