Revision: 21526
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21526
Author:   campbellbarton
Date:     2009-07-11 15:57:56 +0200 (Sat, 11 Jul 2009)

Log Message:
-----------
PyApi
* refcount error if StringIO or io modules could not be imported
* importing python modules like math didnt work because the script registration 
overwrote the script path. now just prepend the path.

Modified Paths:
--------------
    
branches/blender2.5/blender/source/blender/editors/space_view3d/space_view3d.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_util.c

Modified: 
branches/blender2.5/blender/source/blender/editors/space_view3d/space_view3d.c
===================================================================
--- 
branches/blender2.5/blender/source/blender/editors/space_view3d/space_view3d.c  
    2009-07-11 13:47:05 UTC (rev 21525)
+++ 
branches/blender2.5/blender/source/blender/editors/space_view3d/space_view3d.c  
    2009-07-11 13:57:56 UTC (rev 21526)
@@ -601,7 +601,7 @@
        if(CTX_data_dir(member)) {
                static const char *dir[] = {
                        "selected_objects", "selected_bases" 
"selected_editable_objects",
-                       "selected_editable_bases" "visible_objects", 
"visible_bases",
+                       "selected_editable_bases" "visible_objects", 
"visible_bases", "selectable_objects", "selectable_bases",
                        "active_base", "active_object", "visible_bones", 
"editable_bones",
                        "selected_bones", "selected_editable_bones" 
"visible_pchans",
                        "selected_pchans", "active_bone", "active_pchan", NULL};

Modified: 
branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c    
2009-07-11 13:47:05 UTC (rev 21525)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c    
2009-07-11 13:57:56 UTC (rev 21526)
@@ -457,8 +457,7 @@
        
        PyGILState_STATE gilstate;
        PyObject *mod;
-       PyObject *sys_path_orig;
-       PyObject *sys_path_new;
+       PyObject *sys_path;
 
        gilstate = PyGILState_Ensure();
        
@@ -466,6 +465,10 @@
        BPy_SetContext(C);
        bpy_import_main_set(CTX_data_main(C));
 
+
+       sys_path= PySys_GetObject("path"); /* borrow */
+       PyList_Insert(sys_path, 0, Py_None); /* place holder, resizes the list 
*/
+
        for(a=0; dirs[a]; a++) {
                dirname= BLI_gethome_folder(dirs[a]);
 
@@ -476,15 +479,9 @@
 
                if(!dir)
                        continue;
-
-               /* 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);
+               /* set the first dir in the sys.path for fast importing of 
modules */
+               PyList_SetItem(sys_path, 0, PyUnicode_FromString(dirname)); /* 
steals the ref */
                        
                while((de = readdir(dir)) != NULL) {
                        /* We could stat the file but easier just to let python
@@ -514,11 +511,10 @@
                }
 
                closedir(dir);
-
-               PySys_SetObject("path", sys_path_orig);
-               Py_DECREF(sys_path_orig);
        }
        
+       PyList_SetSlice(sys_path, 0, 1, NULL); /* remove the first item */
+
        bpy_import_main_set(NULL);
        
        PyGILState_Release(gilstate);

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_util.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_util.c 
2009-07-11 13:47:05 UTC (rev 21525)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_util.c 
2009-07-11 13:57:56 UTC (rev 21526)
@@ -348,8 +348,8 @@
        PyObject *stderr_backup = PySys_GetObject("stderr"); /* borrowed */
        PyObject *string_io = NULL;
        PyObject *string_io_buf = NULL;
-       PyObject *string_io_mod;
-       PyObject *string_io_getvalue;
+       PyObject *string_io_mod= NULL;
+       PyObject *string_io_getvalue= NULL;
        
        PyObject *error_type, *error_value, *error_traceback;
        
@@ -369,14 +369,11 @@
 #else
        if(! (string_io_mod= PyImport_ImportModule("io")) ) {
 #endif
-               return NULL;
+               goto error_cleanup;
        } else if (! (string_io = PyObject_CallMethod(string_io_mod, 
"StringIO", NULL))) {
-               Py_DECREF(string_io_mod);
-               return NULL;
+               goto error_cleanup;
        } else if (! (string_io_getvalue= PyObject_GetAttrString(string_io, 
"getvalue"))) {
-               Py_DECREF(string_io_mod);
-               Py_DECREF(string_io);
-               return NULL;
+               goto error_cleanup;
        }
        
        Py_INCREF(stdout_backup); // since these were borrowed we dont want 
them freed when replaced.
@@ -403,6 +400,18 @@
        
        PyErr_Clear();
        return string_io_buf;
+       
+       
+error_cleanup:
+       /* could not import the module so print the error and close */
+       Py_XDECREF(string_io_mod);
+       Py_XDECREF(string_io);
+       
+       PyErr_Restore(error_type, error_value, error_traceback);
+       PyErr_Print(); /* print the error */
+       PyErr_Clear();
+       
+       return NULL;
 }
 
 char *BPy_enum_as_string(EnumPropertyItem *item)


_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to