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.
This commit is contained in:
Campbell Barton 2021-07-27 22:26:33 +10:00
parent b1a2abd6b2
commit 58eacb8e7c
9 changed files with 89 additions and 46 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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++) {

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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, "FloatVectorProperty get") == -1) {
if (PyC_AsArray(
values, sizeof(*values), ret, len, &PyFloat_Type, "FloatVectorProperty get: ") == -1) {
PyC_Err_PrintWithFunc(py_func);
for (i = 0; i < len; i++) {
@ -2629,9 +2632,12 @@ static PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject
return NULL;
}
if (pydef &&
PyC_AsArray(
def, pydef, size, &PyBool_Type, false, "BoolVectorProperty(default=sequence)") == -1) {
if (pydef && (PyC_AsArray(def,
sizeof(*def),
pydef,
size,
&PyBool_Type,
"BoolVectorProperty(default=sequence): ") == -1)) {
return NULL;
}
@ -2916,9 +2922,12 @@ static PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject
return NULL;
}
if (pydef &&
PyC_AsArray(
def, pydef, size, &PyLong_Type, false, "IntVectorProperty(default=sequence)") == -1) {
if (pydef && (PyC_AsArray(def,
sizeof(*def),
pydef,
size,
&PyLong_Type,
"IntVectorProperty(default=sequence): ") == -1)) {
return NULL;
}
@ -3196,10 +3205,12 @@ static PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObjec
return NULL;
}
if (pydef &&
PyC_AsArray(
def, pydef, size, &PyFloat_Type, false, "FloatVectorProperty(default=sequence)") ==
-1) {
if (pydef && (PyC_AsArray(def,
sizeof(*def),
pydef,
size,
&PyFloat_Type,
"FloatVectorProperty(default=sequence): ") == -1)) {
return NULL;
}

View File

@ -80,10 +80,10 @@ static void py_rna_gizmo_handler_get_cb(const wmGizmo *UNUSED(gz),
}
else {
if (PyC_AsArray(value,
sizeof(*value),
ret,
gz_prop->type->array_length,
&PyFloat_Type,
false,
"Gizmo get callback: ") == -1) {
goto fail;
}
@ -426,11 +426,11 @@ static PyObject *bpy_gizmo_target_set_value(PyObject *UNUSED(self), PyObject *ar
if (array_len != 0) {
float *value = BLI_array_alloca(value, array_len);
if (PyC_AsArray(value,
sizeof(*value),
params.value,
gz_prop->type->array_length,
&PyFloat_Type,
false,
"Gizmo target property array") == -1) {
"Gizmo target property array: ") == -1) {
goto fail;
}
WM_gizmo_target_property_float_set_array(BPY_context_get(), gz, gz_prop, value);