2.5: warning fixes
[blender.git] / source / blender / python / intern / bpy_util.c
index d5b131583dce7f8fab4be0bb1061158633e88631..be343843acc437411e21967b15a62ae9c43e5445 100644 (file)
@@ -81,6 +81,7 @@ int BPY_flag_from_seq(BPY_flag_def *flagdef, PyObject *seq, int *flag)
        char *cstring;
        PyObject *item;
        BPY_flag_def *fd;
+       *flag = 0;
 
        if (PySequence_Check(seq)) {
                i= PySequence_Length(seq);
@@ -108,6 +109,9 @@ int BPY_flag_from_seq(BPY_flag_def *flagdef, PyObject *seq, int *flag)
                error_val= 1;
        }
 
+       if (*flag == 0)
+               error_val = 1;
+
        if (error_val) {
                char *buf = bpy_flag_error_str(flagdef);
                PyErr_SetString(PyExc_AttributeError, buf);
@@ -120,7 +124,6 @@ int BPY_flag_from_seq(BPY_flag_def *flagdef, PyObject *seq, int *flag)
 
 
 /* Copied from pythons 3's Object.c */
-#ifndef Py_CmpToRich
 PyObject *
 Py_CmpToRich(int op, int cmp)
 {
@@ -156,7 +159,6 @@ Py_CmpToRich(int op, int cmp)
        Py_INCREF(res);
        return res;
 }
-#endif
 
 /* for debugging */
 void PyObSpit(char *name, PyObject *var) {
@@ -166,8 +168,8 @@ void PyObSpit(char *name, PyObject *var) {
        }
        else {
                PyObject_Print(var, stderr, 0);
-               fprintf(stderr, " ref:%d ", var->ob_refcnt);
-               fprintf(stderr, " ptr:%ld", (long)var);
+               fprintf(stderr, " ref:%d ", (int)var->ob_refcnt);
+               fprintf(stderr, " ptr:%p", (void *)var);
                
                fprintf(stderr, " type:");
                if(Py_TYPE(var))
@@ -300,12 +302,21 @@ int BPY_class_validate(const char *class_type, PyObject *class, PyObject *base_c
                                                PyErr_Format( PyExc_AttributeError, "expected %s class \"%s\" attribute to be a string", class_type, class_attrs->name);
                                                return -1;
                                        }
+                                       if(class_attrs->len != -1 && class_attrs->len < PyUnicode_GetSize(item)) {
+                                               PyErr_Format( PyExc_AttributeError, "expected %s class \"%s\" attribute string to be shorter then %d", class_type, class_attrs->name, class_attrs->len);
+                                               return -1;
+                                       }
+
                                        break;
                                case 'l':
                                        if (PyList_Check(item)==0) {
                                                PyErr_Format( PyExc_AttributeError, "expected %s class \"%s\" attribute to be a list", class_type, class_attrs->name);
                                                return -1;
                                        }
+                                       if(class_attrs->len != -1 && class_attrs->len < PyList_GET_SIZE(item)) {
+                                               PyErr_Format( PyExc_AttributeError, "expected %s class \"%s\" attribute list to be shorter then %d", class_type, class_attrs->name, class_attrs->len);
+                                               return -1;
+                                       }
                                        break;
                                case 'f':
                                        if (PyMethod_Check(item))
@@ -344,8 +355,8 @@ PyObject *BPY_exception_buffer(void)
        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;
        
@@ -360,19 +371,12 @@ PyObject *BPY_exception_buffer(void)
         * string_io = StringIO.StringIO()
         */
        
-#if PY_VERSION_HEX < 0x03000000
-       if(! (string_io_mod= PyImport_ImportModule("StringIO")) ) {
-#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.
@@ -399,6 +403,18 @@ PyObject *BPY_exception_buffer(void)
        
        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)
@@ -408,7 +424,8 @@ char *BPy_enum_as_string(EnumPropertyItem *item)
        char *cstring;
 
        for (e= item; item->identifier; item++) {
-               BLI_dynstr_appendf(dynstr, (e==item)?"'%s'":", '%s'", item->identifier);
+               if(item->identifier[0])
+                       BLI_dynstr_appendf(dynstr, (e==item)?"'%s'":", '%s'", item->identifier);
        }
 
        cstring = BLI_dynstr_get_cstring(dynstr);