Cleanup: use braces in mathutils switch statements

Also move error checking before variable declarations.
This commit is contained in:
Campbell Barton
2025-09-16 14:50:41 +10:00
parent b2176bfdd7
commit 96d7c5c4cd
5 changed files with 138 additions and 84 deletions

View File

@@ -135,26 +135,27 @@ static PyObject *Vector_to_tuple_ex(VectorObject *self, int ndigits)
* \{ */
/**
* Supports 2D, 3D, and 4D vector objects both int and float values
* accepted. Mixed float and int values accepted. Ints are parsed to float
* Supports 2D, 3D, and 4D vector objects both int and float values accepted.
* Mixed float and integer values accepted. Integers are converted to float.
*/
static PyObject *Vector_vectorcall(PyObject *type,
PyObject *const *args,
const size_t nargsf,
PyObject *kwnames)
{
float *vec = nullptr;
int vec_num = 3; /* default to a 3D vector */
if (UNLIKELY(kwnames && PyDict_Size(kwnames))) {
PyErr_SetString(PyExc_TypeError,
"Vector(): "
"takes no keyword args");
return nullptr;
}
float *vec = nullptr;
int vec_num = 3; /* Default to a 3D vector. */
const size_t nargs = PyVectorcall_NARGS(nargsf);
switch (nargs) {
case 0:
case 0: {
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
if (vec == nullptr) {
@@ -166,17 +167,20 @@ static PyObject *Vector_vectorcall(PyObject *type,
copy_vn_fl(vec, vec_num, 0.0f);
break;
case 1:
}
case 1: {
if ((vec_num = mathutils_array_parse_alloc(&vec, 2, args[0], "mathutils.Vector()")) == -1) {
return nullptr;
}
break;
default:
}
default: {
PyErr_Format(PyExc_TypeError,
"mathutils.Vector(): "
"takes at most 1 argument (%zd given)",
nargs);
return nullptr;
}
}
return Vector_CreatePyObject_alloc(vec, vec_num, (PyTypeObject *)type);
}
@@ -255,11 +259,12 @@ static PyObject *C_Vector_Range(PyObject *cls, PyObject *args)
}
switch (PyTuple_GET_SIZE(args)) {
case 1:
case 1: {
vec_num = start;
start = 0;
break;
case 2:
}
case 2: {
if (start >= stop) {
PyErr_SetString(PyExc_RuntimeError,
"Start value is larger "
@@ -269,7 +274,8 @@ static PyObject *C_Vector_Range(PyObject *cls, PyObject *args)
vec_num = stop - start;
break;
default:
}
default: {
if (start >= stop) {
PyErr_SetString(PyExc_RuntimeError,
"Start value is larger "
@@ -286,6 +292,7 @@ static PyObject *C_Vector_Range(PyObject *cls, PyObject *args)
vec_num /= step;
break;
}
}
if (vec_num < 2) {
@@ -808,18 +815,22 @@ static PyObject *Vector_to_track_quat(VectorObject *self, PyObject *args)
if (strlen(strack) == 2) {
if (strack[0] == '-') {
switch (strack[1]) {
case 'X':
case 'X': {
track = 3;
break;
case 'Y':
}
case 'Y': {
track = 4;
break;
case 'Z':
}
case 'Z': {
track = 5;
break;
default:
}
default: {
PyErr_SetString(PyExc_ValueError, axis_err_msg);
return nullptr;
}
}
}
else {
@@ -830,18 +841,22 @@ static PyObject *Vector_to_track_quat(VectorObject *self, PyObject *args)
else if (strlen(strack) == 1) {
switch (strack[0]) {
case '-':
case 'X':
case 'X': {
track = 0;
break;
case 'Y':
}
case 'Y': {
track = 1;
break;
case 'Z':
}
case 'Z': {
track = 2;
break;
default:
}
default: {
PyErr_SetString(PyExc_ValueError, axis_err_msg);
return nullptr;
}
}
}
else {
@@ -854,18 +869,22 @@ static PyObject *Vector_to_track_quat(VectorObject *self, PyObject *args)
const char *axis_err_msg = "only X, Y or Z for up axis";
if (strlen(sup) == 1) {
switch (*sup) {
case 'X':
case 'X': {
up = 0;
break;
case 'Y':
}
case 'Y': {
up = 1;
break;
case 'Z':
}
case 'Z': {
up = 2;
break;
default:
}
default: {
PyErr_SetString(PyExc_ValueError, axis_err_msg);
return nullptr;
}
}
}
else {
@@ -1685,14 +1704,15 @@ static PyObject *Vector_richcmpr(PyObject *objectA, PyObject *objectB, int compa
}
switch (comparison_type) {
case Py_LT:
case Py_LT: {
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
if (lenA < lenB) {
result = 1;
}
break;
case Py_LE:
}
case Py_LE: {
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
if (lenA < lenB) {
@@ -1702,20 +1722,24 @@ static PyObject *Vector_richcmpr(PyObject *objectA, PyObject *objectB, int compa
result = (((lenA + epsilon) > lenB) && ((lenA - epsilon) < lenB));
}
break;
case Py_EQ:
}
case Py_EQ: {
result = EXPP_VectorsAreEqual(vecA->vec, vecB->vec, vecA->vec_num, 1);
break;
case Py_NE:
}
case Py_NE: {
result = !EXPP_VectorsAreEqual(vecA->vec, vecB->vec, vecA->vec_num, 1);
break;
case Py_GT:
}
case Py_GT: {
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
if (lenA > lenB) {
result = 1;
}
break;
case Py_GE:
}
case Py_GE: {
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
if (lenA > lenB) {
@@ -1725,9 +1749,11 @@ static PyObject *Vector_richcmpr(PyObject *objectA, PyObject *objectB, int compa
result = (((lenA + epsilon) > lenB) && ((lenA - epsilon) < lenB));
}
break;
default:
}
default: {
printf("The result of the comparison could not be evaluated");
break;
}
}
if (result == 1) {
Py_RETURN_TRUE;