[Bf-blender-cvs] [58eacb8e7c7] master: Cleanup: pass sizeof array element to PyC_AsArray

Campbell Barton noreply at git.blender.org
Tue Jul 27 14:39:03 CEST 2021


Commit: 58eacb8e7c7ae95bf34896d9cdd868e7a11e044e
Author: Campbell Barton
Date:   Tue Jul 27 22:26:33 2021 +1000
Branches: master
https://developer.blender.org/rB58eacb8e7c7ae95bf34896d9cdd868e7a11e044e

Cleanup: pass sizeof array element to PyC_AsArray

Replace the is_double argument which was only used for single/double
precision floats.

This allows supporting different sized int types more easily.

===================================================================

M	source/blender/python/generic/idprop_py_api.c
M	source/blender/python/generic/imbuf_py_api.c
M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/gpu/gpu_py_element.c
M	source/blender/python/gpu/gpu_py_shader.c
M	source/blender/python/gpu/gpu_py_texture.c
M	source/blender/python/intern/bpy_props.c
M	source/blender/python/intern/bpy_rna_gizmo.c

===================================================================

diff --git a/source/blender/python/generic/idprop_py_api.c b/source/blender/python/generic/idprop_py_api.c
index bfdc763e4df..024900db691 100644
--- a/source/blender/python/generic/idprop_py_api.c
+++ b/source/blender/python/generic/idprop_py_api.c
@@ -1836,7 +1836,7 @@ static int BPy_IDArray_ass_slice(BPy_IDArray *self, int begin, int end, PyObject
   /* NOTE: we count on int/float being the same size here */
   vec = MEM_mallocN(alloc_len, "array assignment");
 
-  if (PyC_AsArray(vec, seq, size, py_type, is_double, "slice assignment: ") == -1) {
+  if (PyC_AsArray(vec, elem_size, seq, size, py_type, "slice assignment: ") == -1) {
     MEM_freeN(vec);
     return -1;
   }
diff --git a/source/blender/python/generic/imbuf_py_api.c b/source/blender/python/generic/imbuf_py_api.c
index 08ddef992a3..87afc40330c 100644
--- a/source/blender/python/generic/imbuf_py_api.c
+++ b/source/blender/python/generic/imbuf_py_api.c
@@ -242,7 +242,7 @@ static int py_imbuf_ppm_set(Py_ImBuf *self, PyObject *value, void *UNUSED(closur
   PY_IMBUF_CHECK_INT(self);
   double ppm[2];
 
-  if (PyC_AsArray(ppm, value, 2, &PyFloat_Type, true, "ppm") == -1) {
+  if (PyC_AsArray(ppm, sizeof(*ppm), value, 2, &PyFloat_Type, "ppm") == -1) {
     return -1;
   }
 
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index 68fefee4a61..37b2849aa94 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -57,10 +57,10 @@
 
 /* array utility function */
 int PyC_AsArray_FAST(void *array,
+                     const size_t array_item_size,
                      PyObject *value_fast,
                      const Py_ssize_t length,
                      const PyTypeObject *type,
-                     const bool is_double,
                      const char *error_prefix)
 {
   const Py_ssize_t value_len = PySequence_Fast_GET_SIZE(value_fast);
@@ -80,30 +80,55 @@ int PyC_AsArray_FAST(void *array,
 
   /* for each type */
   if (type == &PyFloat_Type) {
-    if (is_double) {
-      double *array_double = array;
-      for (i = 0; i < length; i++) {
-        array_double[i] = PyFloat_AsDouble(value_fast_items[i]);
+    switch (array_item_size) {
+      case sizeof(double): {
+        double *array_double = array;
+        for (i = 0; i < length; i++) {
+          array_double[i] = PyFloat_AsDouble(value_fast_items[i]);
+        }
+        break;
       }
-    }
-    else {
-      float *array_float = array;
-      for (i = 0; i < length; i++) {
-        array_float[i] = PyFloat_AsDouble(value_fast_items[i]);
+      case sizeof(float): {
+        float *array_float = array;
+        for (i = 0; i < length; i++) {
+          array_float[i] = PyFloat_AsDouble(value_fast_items[i]);
+        }
+        break;
+      }
+      default: {
+        /* Internal error. */
+        BLI_assert_unreachable();
       }
     }
   }
   else if (type == &PyLong_Type) {
-    /* could use is_double for 'long int' but no use now */
-    int *array_int = array;
-    for (i = 0; i < length; i++) {
-      array_int[i] = PyC_Long_AsI32(value_fast_items[i]);
+    switch (array_item_size) {
+      case sizeof(int32_t): {
+        int32_t *array_int = array;
+        for (i = 0; i < length; i++) {
+          array_int[i] = PyC_Long_AsI32(value_fast_items[i]);
+        }
+        break;
+      }
+      default: {
+        /* Internal error. */
+        BLI_assert_unreachable();
+      }
     }
   }
   else if (type == &PyBool_Type) {
-    bool *array_bool = array;
-    for (i = 0; i < length; i++) {
-      array_bool[i] = (PyLong_AsLong(value_fast_items[i]) != 0);
+    switch (array_item_size) {
+      case sizeof(bool): {
+        bool *array_bool = array;
+        for (i = 0; i < length; i++) {
+          array_bool[i] = (PyLong_AsLong(value_fast_items[i]) != 0);
+        }
+        break;
+      }
+      default: {
+        /* Internal error. */
+        BLI_assert_unreachable();
+      }
     }
   }
   else {
@@ -123,10 +148,10 @@ int PyC_AsArray_FAST(void *array,
 }
 
 int PyC_AsArray(void *array,
+                const size_t array_item_size,
                 PyObject *value,
                 const Py_ssize_t length,
                 const PyTypeObject *type,
-                const bool is_double,
                 const char *error_prefix)
 {
   PyObject *value_fast;
@@ -136,7 +161,7 @@ int PyC_AsArray(void *array,
     return -1;
   }
 
-  ret = PyC_AsArray_FAST(array, value_fast, length, type, is_double, error_prefix);
+  ret = PyC_AsArray_FAST(array, array_item_size, value_fast, length, type, error_prefix);
   Py_DECREF(value_fast);
   return ret;
 }
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 842e1482c06..5e0f60956f0 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -42,16 +42,16 @@ void PyC_Err_PrintWithFunc(PyObject *py_func);
 void PyC_FileAndNum(const char **r_filename, int *r_lineno);
 void PyC_FileAndNum_Safe(const char **r_filename, int *r_lineno); /* checks python is running */
 int PyC_AsArray_FAST(void *array,
+                     const size_t array_elem_size,
                      PyObject *value_fast,
                      const Py_ssize_t length,
                      const PyTypeObject *type,
-                     const bool is_double,
                      const char *error_prefix);
 int PyC_AsArray(void *array,
+                const size_t array_elem_size,
                 PyObject *value,
                 const Py_ssize_t length,
                 const PyTypeObject *type,
-                const bool is_double,
                 const char *error_prefix);
 
 PyObject *PyC_Tuple_PackArray_F32(const float *array, uint len);
diff --git a/source/blender/python/gpu/gpu_py_element.c b/source/blender/python/gpu/gpu_py_element.c
index 2dd8d1c379e..2fb722f74db 100644
--- a/source/blender/python/gpu/gpu_py_element.c
+++ b/source/blender/python/gpu/gpu_py_element.c
@@ -144,8 +144,12 @@ static PyObject *pygpu_IndexBuf__tp_new(PyTypeObject *UNUSED(type), PyObject *ar
           goto finally;
         }
 
-        ok = PyC_AsArray_FAST(
-                 values, seq_fast_item, verts_per_prim, &PyLong_Type, false, error_prefix) == 0;
+        ok = PyC_AsArray_FAST(values,
+                              sizeof(*values),
+                              seq_fast_item,
+                              verts_per_prim,
+                              &PyLong_Type,
+                              error_prefix) == 0;
 
         if (ok) {
           for (uint j = 0; j < verts_per_prim; j++) {
diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c
index 00e876aee7d..b3f1c186716 100644
--- a/source/blender/python/gpu/gpu_py_shader.c
+++ b/source/blender/python/gpu/gpu_py_shader.c
@@ -309,7 +309,8 @@ static PyObject *pygpu_shader_uniform_bool(BPyGPUShader *self, PyObject *args)
         ret = -1;
       }
       else {
-        ret = PyC_AsArray_FAST(values, seq_fast, length, &PyLong_Type, false, error_prefix);
+        ret = PyC_AsArray_FAST(
+            values, sizeof(*values), seq_fast, length, &PyLong_Type, error_prefix);
       }
       Py_DECREF(seq_fast);
     }
@@ -448,7 +449,8 @@ static PyObject *pygpu_shader_uniform_int(BPyGPUShader *self, PyObject *args)
         ret = -1;
       }
       else {
-        ret = PyC_AsArray_FAST(values, seq_fast, length, &PyLong_Type, false, error_prefix);
+        ret = PyC_AsArray_FAST(
+            values, sizeof(*values), seq_fast, length, &PyLong_Type, error_prefix);
       }
       Py_DECREF(seq_fast);
     }
diff --git a/source/blender/python/gpu/gpu_py_texture.c b/source/blender/python/gpu/gpu_py_texture.c
index ca41662db9d..4cbbd77438c 100644
--- a/source/blender/python/gpu/gpu_py_texture.c
+++ b/source/blender/python/gpu/gpu_py_texture.c
@@ -153,7 +153,7 @@ static PyObject *pygpu_texture__tp_new(PyTypeObject *UNUSED(self), PyObject *arg
   int len = 1;
   if (PySequence_Check(py_size)) {
     len = PySequence_Size(py_size);
-    if (PyC_AsArray(size, py_size, len, &PyLong_Type, false, "GPUTexture.__new__") == -1) {
+    if (PyC_AsArray(size, sizeof(*size), py_size, len, &PyLong_Type, "GPUTexture.__new__") == -1) {
       return NULL;
     }
   }
@@ -321,10 +321,11 @@ static PyObject *pygpu_texture_clear(BPyGPUTexture *self, PyObject *args, PyObje
 
   memset(&values, 0, sizeof(values));
   if (PyC_AsArray(&values,
+                  (pygpu_dataformat.value_found == GPU_DATA_FLOAT) ? sizeof(*values.f) :
+                                                                     sizeof(*values.i),
                   py_values,
                   shape,
-                  pygpu_dataformat.value_found == GPU_DATA_FLOAT ? &PyFloat_Type : &PyLong_Type,
-                  false,
+                  (pygpu_dataformat.value_found == GPU_DATA_FLOAT) ? &PyFloat_Type : &PyLong_Type,
                   "clear") == -1) {
     return NULL;
   }
diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c
index f332d547965..0b812037810 100644
--- a/source/blender/python/intern/bpy_props.c
+++ b/source/blender/python/intern/bpy_props.c
@@ -719,7 +719,8 @@ static void bpy_prop_boolean_array_get_fn(struct PointerRNA *ptr,
     }
   }
   else {
-    if (PyC_AsArray(values, ret, len, &PyBool_Type, false, "BoolVectorProperty get") == -1) {
+    if (PyC_AsArray(values, sizeof(*values), ret, len, &PyBool_Type, "BoolVectorProperty get: ") ==
+        -1) {
       PyC_Err_PrintWithFunc(py_func);
 
       for (i = 0; i < len; i++) {
@@ -969,7 +970,8 @@ static void bpy_prop_int_array_get_fn(struct PointerRNA *ptr,
     }
   }
   else {
-    if (PyC_AsArray(values, ret, len, &PyLong_Type, false, "IntVectorProperty get") == -1) {
+    if (PyC_AsArray(values, sizeof(*values), ret, len, &PyLong_Type, "IntVectorProperty get: ") ==
+        -1) {
       PyC_Err_PrintWithFunc(py_func);
 
       for (i = 0; i < len; i++) {
@@ -1219,7 +1221,8 @@ static void bpy_prop_float_array_get_fn(struct PointerRNA *ptr,
     }
   }
   else {
-    if (PyC_AsArray(values, ret, len, &PyFloat_Type, false, "Floa

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list