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

@@ -66,8 +66,6 @@ static PyObject *Color_vectorcall(PyObject *type,
const size_t nargsf,
PyObject *kwnames)
{
float col[3] = {0.0f, 0.0f, 0.0f};
if (UNLIKELY(kwnames && PyDict_Size(kwnames))) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Color(): "
@@ -75,21 +73,26 @@ static PyObject *Color_vectorcall(PyObject *type,
return nullptr;
}
float col[3] = {0.0f, 0.0f, 0.0f};
const size_t nargs = PyVectorcall_NARGS(nargsf);
switch (nargs) {
case 0:
case 0: {
break;
case 1:
}
case 1: {
if (mathutils_array_parse(col, COLOR_SIZE, COLOR_SIZE, args[0], "mathutils.Color()") == -1) {
return nullptr;
}
break;
default:
}
default: {
PyErr_Format(PyExc_TypeError,
"mathutils.Color(): "
"takes at most 1 argument (%zd given)",
nargs);
return nullptr;
}
}
return Color_CreatePyObject(col, (PyTypeObject *)type);
}
@@ -446,22 +449,25 @@ static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
}
switch (op) {
case Py_NE:
case Py_NE: {
ok = !ok;
ATTR_FALLTHROUGH;
case Py_EQ:
}
case Py_EQ: {
res = ok ? Py_False : Py_True;
break;
}
case Py_LT:
case Py_LE:
case Py_GT:
case Py_GE:
case Py_GE: {
res = Py_NotImplemented;
break;
default:
}
default: {
PyErr_BadArgument();
return nullptr;
}
}
return Py_NewRef(res);

View File

@@ -43,18 +43,24 @@ short euler_order_from_string(const char *str, const char *error_prefix)
#define MAKE_ID3(a, b, c) ((a) | ((b) << 8) | ((c) << 16))
switch (*((const PY_INT32_T *)str)) {
case MAKE_ID3('X', 'Y', 'Z'):
case MAKE_ID3('X', 'Y', 'Z'): {
return EULER_ORDER_XYZ;
case MAKE_ID3('X', 'Z', 'Y'):
}
case MAKE_ID3('X', 'Z', 'Y'): {
return EULER_ORDER_XZY;
case MAKE_ID3('Y', 'X', 'Z'):
}
case MAKE_ID3('Y', 'X', 'Z'): {
return EULER_ORDER_YXZ;
case MAKE_ID3('Y', 'Z', 'X'):
}
case MAKE_ID3('Y', 'Z', 'X'): {
return EULER_ORDER_YZX;
case MAKE_ID3('Z', 'X', 'Y'):
}
case MAKE_ID3('Z', 'X', 'Y'): {
return EULER_ORDER_ZXY;
case MAKE_ID3('Z', 'Y', 'X'):
}
case MAKE_ID3('Z', 'Y', 'X'): {
return EULER_ORDER_ZYX;
}
}
#undef MAKE_ID3
@@ -99,9 +105,6 @@ static PyObject *Euler_vectorcall(PyObject *type,
const size_t nargsf,
PyObject *kwnames)
{
float eul[EULER_SIZE] = {0.0f, 0.0f, 0.0f};
short order = EULER_ORDER_XYZ;
if (UNLIKELY(kwnames && PyDict_Size(kwnames))) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Euler(): "
@@ -109,10 +112,14 @@ static PyObject *Euler_vectorcall(PyObject *type,
return nullptr;
}
float eul[EULER_SIZE] = {0.0f, 0.0f, 0.0f};
short order = EULER_ORDER_XYZ;
const size_t nargs = PyVectorcall_NARGS(nargsf);
switch (nargs) {
case 0:
case 0: {
break;
}
case 2: {
const char *order_str;
@@ -123,11 +130,12 @@ static PyObject *Euler_vectorcall(PyObject *type,
}
ATTR_FALLTHROUGH;
}
case 1:
case 1: {
if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, args[0], "mathutils.Euler()") == -1) {
return nullptr;
}
break;
}
default: {
PyErr_Format(PyExc_TypeError,
"mathutils.Euler(): "
@@ -469,22 +477,25 @@ static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
}
switch (op) {
case Py_NE:
case Py_NE: {
ok = !ok;
ATTR_FALLTHROUGH;
case Py_EQ:
}
case Py_EQ: {
res = ok ? Py_False : Py_True;
break;
}
case Py_LT:
case Py_LE:
case Py_GT:
case Py_GE:
case Py_GE: {
res = Py_NotImplemented;
break;
default:
}
default: {
PyErr_BadArgument();
return nullptr;
}
}
return Py_NewRef(res);
@@ -831,8 +842,7 @@ static PyGetSetDef Euler_getseters[] = {
(setter) nullptr,
BaseMathObject_owner_doc,
nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
};
{nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */};
/** \} */
@@ -891,7 +901,8 @@ PyDoc_STRVAR(
"\n"
" This object gives access to Eulers in Blender.\n"
"\n"
" .. seealso:: `Euler angles <https://en.wikipedia.org/wiki/Euler_angles>`__ on Wikipedia.\n"
" .. seealso:: `Euler angles <https://en.wikipedia.org/wiki/Euler_angles>`__ on "
"Wikipedia.\n"
"\n"
" :arg angles: (X, Y, Z) angles in radians.\n"
" :type angles: Sequence[float]\n"

View File

@@ -607,9 +607,9 @@ static PyObject *Matrix_vectorcall(PyObject *type,
}
switch (PyVectorcall_NARGS(nargsf)) {
case 0:
case 0: {
return Matrix_CreatePyObject(nullptr, 4, 4, (PyTypeObject *)type);
}
case 1: {
PyObject *arg = args[0];
@@ -1596,9 +1596,10 @@ static bool matrix_invert_is_compat(const MatrixObject *self)
static bool matrix_invert_args_check(const MatrixObject *self, PyObject *args, bool check_type)
{
switch (PyTuple_GET_SIZE(args)) {
case 0:
case 0: {
return true;
case 1:
}
case 1: {
if (check_type) {
const MatrixObject *fallback = (const MatrixObject *)PyTuple_GET_ITEM(args, 0);
if (!MatrixObject_Check(fallback)) {
@@ -1617,11 +1618,13 @@ static bool matrix_invert_args_check(const MatrixObject *self, PyObject *args, b
}
return true;
default:
}
default: {
PyErr_SetString(PyExc_ValueError,
"Matrix.invert(ed): "
"takes at most one argument");
return false;
}
}
}
@@ -2302,14 +2305,14 @@ static PyObject *Matrix_repr(MatrixObject *self)
}
}
switch (self->row_num) {
case 2:
case 2: {
return PyUnicode_FromFormat(
"Matrix((%R,\n"
" %R))",
rows[0],
rows[1]);
case 3:
}
case 3: {
return PyUnicode_FromFormat(
"Matrix((%R,\n"
" %R,\n"
@@ -2317,8 +2320,8 @@ static PyObject *Matrix_repr(MatrixObject *self)
rows[0],
rows[1],
rows[2]);
case 4:
}
case 4: {
return PyUnicode_FromFormat(
"Matrix((%R,\n"
" %R,\n"
@@ -2328,6 +2331,7 @@ static PyObject *Matrix_repr(MatrixObject *self)
rows[1],
rows[2],
rows[3]);
}
}
Py_FatalError("Matrix(): invalid row size!");
@@ -2464,22 +2468,25 @@ static PyObject *Matrix_richcmpr(PyObject *a, PyObject *b, int op)
}
switch (op) {
case Py_NE:
case Py_NE: {
ok = !ok;
ATTR_FALLTHROUGH;
case Py_EQ:
}
case Py_EQ: {
res = ok ? Py_False : Py_True;
break;
}
case Py_LT:
case Py_LE:
case Py_GT:
case Py_GE:
case Py_GE: {
res = Py_NotImplemented;
break;
default:
}
default: {
PyErr_BadArgument();
return nullptr;
}
}
return Py_NewRef(res);

View File

@@ -109,10 +109,6 @@ static PyObject *Quaternion_vectorcall(PyObject *type,
const size_t nargsf,
PyObject *kwnames)
{
double angle = 0.0f;
float quat[QUAT_SIZE];
unit_qt(quat);
if (UNLIKELY(kwnames && PyDict_Size(kwnames))) {
PyErr_SetString(PyExc_TypeError,
"mathutils.Quaternion(): "
@@ -120,10 +116,15 @@ static PyObject *Quaternion_vectorcall(PyObject *type,
return nullptr;
}
double angle = 0.0f;
float quat[QUAT_SIZE];
unit_qt(quat);
const size_t nargs = PyVectorcall_NARGS(nargsf);
switch (nargs) {
case 0:
case 0: {
break;
}
case 1: {
const int size = mathutils_array_parse(
quat, 3, QUAT_SIZE, args[0], "mathutils.Quaternion()");
@@ -950,22 +951,25 @@ static PyObject *Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
}
switch (op) {
case Py_NE:
case Py_NE: {
ok = !ok;
ATTR_FALLTHROUGH;
case Py_EQ:
}
case Py_EQ: {
res = ok ? Py_False : Py_True;
break;
}
case Py_LT:
case Py_LE:
case Py_GT:
case Py_GE:
case Py_GE: {
res = Py_NotImplemented;
break;
default:
}
default: {
PyErr_BadArgument();
return nullptr;
}
}
return Py_NewRef(res);

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;