Cleanup: Various clang-tidy warnings in python

Pull Request: https://projects.blender.org/blender/blender/pulls/133734
This commit is contained in:
Brecht Van Lommel
2025-01-26 20:08:04 +01:00
parent 2b1dc0a44e
commit 27accea53e
29 changed files with 68 additions and 96 deletions

View File

@@ -36,7 +36,7 @@ bool BPY_is_pyconstraint(Text *text);
/* global interpreter lock */
typedef void *BPy_ThreadStatePtr;
using BPy_ThreadStatePtr = void *;
/**
* Analogue of #PyEval_SaveThread()

View File

@@ -13,7 +13,7 @@
struct bContext;
/* For 'FILE'. */
#include <stdio.h>
#include <cstdio>
/* `bpy_interface.cc` */

View File

@@ -3711,7 +3711,7 @@ static PyObject *bpy_bmiter_next(BPy_BMIter *self)
return nullptr;
}
return (PyObject *)BPy_BMElem_CreatePyObject(self->bm, ele);
return BPy_BMElem_CreatePyObject(self->bm, ele);
}
/* Deallocate Functions

View File

@@ -11,6 +11,8 @@
#include <Python.h>
#include <algorithm>
#include "BLI_math_vector.h"
#include "BLI_utildefines.h"
@@ -833,12 +835,8 @@ static PyObject *bpy_bmlayercollection_subscript_slice(BPy_BMLayerCollection *se
BPY_BM_CHECK_OBJ(self);
if (start > len) {
start = len;
}
if (stop > len) {
stop = len;
}
start = std::min(start, len);
stop = std::min(stop, len);
tuple = PyTuple_New(stop - start);
@@ -1286,9 +1284,7 @@ int BPy_BMLayerItem_SetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer, PyObj
ret = -1;
}
else {
if (tmp_val_len > sizeof(mstring->s)) {
tmp_val_len = sizeof(mstring->s);
}
tmp_val_len = std::min<unsigned long>(tmp_val_len, sizeof(mstring->s));
memcpy(mstring->s, tmp_val, tmp_val_len);
mstring->s_len = tmp_val_len;
}

View File

@@ -360,7 +360,7 @@ int BPy_BMVertSkin_AssignPyObject(MVertSkin *mvertskin, PyObject *value)
return -1;
}
*((MVertSkin *)mvertskin) = *(((BPy_BMVertSkin *)value)->data);
*(mvertskin) = *(((BPy_BMVertSkin *)value)->data);
return 0;
}

View File

@@ -353,7 +353,7 @@ static PyObject *bpy_bmeditseliter_next(BPy_BMEditSelIter *self)
}
self->ese = ese->next;
return (PyObject *)BPy_BMElem_CreatePyObject(self->bm, &ese->ele->head);
return BPy_BMElem_CreatePyObject(self->bm, &ese->ele->head);
}
PyTypeObject BPy_BMEditSelSeq_Type;

View File

@@ -24,6 +24,8 @@
#include <epoxy/gl.h>
#include <algorithm>
#include "CLG_log.h"
/* Forward declare API's defines here. */
@@ -908,15 +910,9 @@ static PyObject *Buffer_slice(Buffer *self, Py_ssize_t begin, Py_ssize_t end)
{
PyObject *list;
if (begin < 0) {
begin = 0;
}
if (end > self->dimensions[0]) {
end = self->dimensions[0];
}
if (begin > end) {
begin = end;
}
begin = std::max<Py_ssize_t>(begin, 0);
end = std::min<Py_ssize_t>(end, self->dimensions[0]);
begin = std::min(begin, end);
list = PyList_New(end - begin);
@@ -967,15 +963,9 @@ static int Buffer_ass_slice(Buffer *self, Py_ssize_t begin, Py_ssize_t end, PyOb
int err = 0;
Py_ssize_t count;
if (begin < 0) {
begin = 0;
}
if (end > self->dimensions[0]) {
end = self->dimensions[0];
}
if (begin > end) {
begin = end;
}
begin = std::max<Py_ssize_t>(begin, 0);
end = std::min<Py_ssize_t>(end, self->dimensions[0]);
begin = std::min(begin, end);
if (!PySequence_Check(seq)) {
PyErr_Format(PyExc_TypeError,

View File

@@ -1045,9 +1045,9 @@ static IDProperty *idp_from_PyObject(IDProperty *prop_exist,
/** \name Mapping Get/Set (Internal Access)
* \{ */
bool BPy_IDProperty_Map_ValidateAndCreate(PyObject *name_obj, IDProperty *group, PyObject *ob)
bool BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, IDProperty *group, PyObject *ob)
{
const char *name = idp_try_read_name(name_obj);
const char *name = idp_try_read_name(key);
if (!name) {
return false;
}
@@ -1617,7 +1617,7 @@ static PyObject *BPy_IDGroup_View_reversed(BPy_IDGroup_View *self, PyObject * /*
static PyMethodDef BPy_IDGroup_View_methods[] = {
{"__reversed__",
(PyCFunction)(void (*)(void))BPy_IDGroup_View_reversed,
(PyCFunction)(void (*)())BPy_IDGroup_View_reversed,
METH_NOARGS,
BPy_IDGroup_View_reversed_doc},
{nullptr, nullptr},

View File

@@ -240,7 +240,7 @@ static const char *pygpu_shader_check_compatibility(blender::gpu::Batch *batch)
}
/* Check batch compatibility with shader. */
for (auto vert : blender::Span(batch->verts, ARRAY_SIZE(batch->verts))) {
for (auto *vert : blender::Span(batch->verts, ARRAY_SIZE(batch->verts))) {
if (!vert) {
continue;
}

View File

@@ -13,6 +13,8 @@
#include <Python.h>
#include <algorithm>
#include "BLI_utildefines.h"
#include "MEM_guardedalloc.h"
@@ -327,15 +329,9 @@ static int pygpu_buffer_ass_slice(BPyGPUBuffer *self,
PyObject *item;
int count, err = 0;
if (begin < 0) {
begin = 0;
}
if (end > self->shape[0]) {
end = self->shape[0];
}
if (begin > end) {
begin = end;
}
begin = std::max<Py_ssize_t>(begin, 0);
end = std::min(end, self->shape[0]);
begin = std::min(begin, end);
if (!PySequence_Check(seq)) {
PyErr_Format(PyExc_TypeError,
@@ -447,15 +443,9 @@ static PyObject *pygpu_buffer_slice(BPyGPUBuffer *self, Py_ssize_t begin, Py_ssi
PyObject *list;
Py_ssize_t count;
if (begin < 0) {
begin = 0;
}
if (end > self->shape[0]) {
end = self->shape[0];
}
if (begin > end) {
begin = end;
}
begin = std::max<Py_ssize_t>(begin, 0);
end = std::min(end, self->shape[0]);
begin = std::min(begin, end);
list = PyList_New(end - begin);
@@ -636,7 +626,7 @@ static int pygpu_buffer__bf_getbuffer(BPyGPUBuffer *self, Py_buffer *view, int f
memset(view, 0, sizeof(*view));
view->obj = (PyObject *)self;
view->buf = (void *)self->buf.as_void;
view->buf = self->buf.as_void;
view->len = bpygpu_Buffer_size(self);
view->readonly = 0;
view->itemsize = GPU_texture_dataformat_size(eGPUDataFormat(self->format));

View File

@@ -13,7 +13,6 @@
#include "BLI_utildefines.h"
#include "GPU_capabilities.hh"
#include "GPU_shader.hh"
#include "GPU_texture.hh"
#include "GPU_uniform_buffer.hh"

View File

@@ -233,7 +233,7 @@ static bool pygpu_interface_info_get_args(BPyGPUStageInterfaceInfo *self,
}
#ifdef USE_GPU_PY_REFERENCES
PyList_Append(self->references, (PyObject *)py_name);
PyList_Append(self->references, py_name);
#endif
*r_type = (Type)pygpu_type.value_found;
@@ -425,7 +425,7 @@ static void pygpu_interface_info__tp_dealloc(PyObject *self)
}
#endif
Py_TYPE(self)->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free(self);
}
PyDoc_STRVAR(
@@ -1296,7 +1296,7 @@ static void pygpu_shader_info__tp_dealloc(PyObject *self)
#endif
Py_TYPE(self)->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free(self);
}
PyDoc_STRVAR(

View File

@@ -10,6 +10,8 @@
#include <Python.h>
#include <algorithm>
#include "bpy_app_timers.hh"
#include "../generic/python_compat.hh"
@@ -35,9 +37,7 @@ static double handle_returned_value(PyObject *function, PyObject *ret)
return -1;
}
if (value < 0.0) {
value = 0.0;
}
value = std::max(value, 0.0);
return value;
}

View File

@@ -338,7 +338,7 @@ static PyObject *app_translations_py_messages_register(BlenderAppTranslations *s
PyExc_ValueError,
"bpy.app.translations.register: translations message cache already contains some data for "
"addon '%s'",
(const char *)PyUnicode_AsUTF8(module_name));
PyUnicode_AsUTF8(module_name));
return nullptr;
}

View File

@@ -10,8 +10,6 @@
#include <Python.h>
#include <stdbool.h>
/**
* For faster execution we keep a special dictionary for py-drivers, with
* the needed modules and aliases.

View File

@@ -122,7 +122,7 @@ static PyObject *pyop_poll(PyObject * /*self*/, PyObject *args)
}
/* main purpose of this function */
ret = WM_operator_poll_context((bContext *)C, ot, context) ? Py_True : Py_False;
ret = WM_operator_poll_context(C, ot, context) ? Py_True : Py_False;
return Py_NewRef(ret);
}
@@ -204,7 +204,7 @@ static PyObject *pyop_call(PyObject * /*self*/, PyObject *args)
context = wmOperatorCallContext(context_int);
}
if (WM_operator_poll_context((bContext *)C, ot, context) == false) {
if (WM_operator_poll_context(C, ot, context) == false) {
bool msg_free = false;
const char *msg = CTX_wm_operator_poll_msg_get(C, &msg_free);
PyErr_Format(PyExc_RuntimeError,

View File

@@ -142,7 +142,7 @@ PyObject *PYOP_wrap_macro_define(PyObject * /*self*/, PyObject *args)
}
/* identifiers */
srna = pyrna_struct_as_srna((PyObject *)macro, false, "Macro Define:");
srna = pyrna_struct_as_srna(macro, false, "Macro Define:");
if (srna == nullptr) {
return nullptr;
}

View File

@@ -4174,7 +4174,7 @@ static void pyrna_dir_members_py(PyObject *list, PyObject *self)
PyObject *dict;
PyObject **dict_ptr;
dict_ptr = _PyObject_GetDictPtr((PyObject *)self);
dict_ptr = _PyObject_GetDictPtr(self);
if (dict_ptr && (dict = *dict_ptr)) {
pyrna_dir_members_py__add_keys(list, dict);
@@ -5675,7 +5675,7 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
((float *)array)[i] = float(PyFloat_AsDouble(item));
break;
case PROP_RAW_DOUBLE:
((double *)array)[i] = double(PyFloat_AsDouble(item));
((double *)array)[i] = PyFloat_AsDouble(item);
break;
case PROP_RAW_INT64:
((int64_t *)array)[i] = PyC_Long_AsI64(item);
@@ -5756,7 +5756,7 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
item = PyFloat_FromDouble(double(((float *)array)[i]));
break;
case PROP_RAW_DOUBLE:
item = PyFloat_FromDouble(double(((double *)array)[i]));
item = PyFloat_FromDouble(((double *)array)[i]);
break;
case PROP_RAW_BOOLEAN:
item = PyBool_FromLong(long(((bool *)array)[i]));
@@ -8552,7 +8552,7 @@ static PyObject *bpy_types_module_getattro(PyObject *self, PyObject *pyname)
return nullptr;
#endif
/* The error raised here will be displayed. */
ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
ret = PyObject_GenericGetAttr(self, pyname);
}
return ret;

View File

@@ -550,7 +550,7 @@ static int py_to_array(PyObject *seq,
if (prop_is_param_dyn_alloc) {
/* not freeing allocated mem, RNA_parameter_list_free() will do this */
ParameterDynAlloc *param_alloc = (ParameterDynAlloc *)param_data;
param_alloc->array_tot = int(totitem);
param_alloc->array_tot = totitem;
/* freeing param list will free */
param_alloc->array = MEM_callocN(item_size * totitem, "py_to_array dyn");

View File

@@ -85,7 +85,7 @@ static void cb_wm_cursor_draw(bContext *C, int x, int y, void *customdata)
PyObject *cb_func, *cb_args, *result;
PyGILState_STATE gilstate;
bpy_context_set((bContext *)C, &gilstate);
bpy_context_set(C, &gilstate);
cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
@@ -110,7 +110,7 @@ static void cb_wm_cursor_draw(bContext *C, int x, int y, void *customdata)
PyErr_Clear();
}
bpy_context_clear((bContext *)C, &gilstate);
bpy_context_clear(C, &gilstate);
}
#if 0
@@ -354,7 +354,7 @@ PyObject *pyrna_callback_classmethod_add(PyObject * /*self*/, PyObject *args)
* This reference is decremented in #BPY_callback_screen_free and #BPY_callback_wm_free. */
Py_INCREF(args);
PyObject *ret = PyCapsule_New((void *)handle, rna_capsual_id, nullptr);
PyObject *ret = PyCapsule_New(handle, rna_capsual_id, nullptr);
/* Store 'args' in context as well for simple access. */
PyCapsule_SetDestructor(ret, cb_rna_capsule_destructor);

View File

@@ -209,7 +209,7 @@ bool python_script_error_jump(
*r_lineno_end = -1;
*r_offset_end = 0;
PyErr_Fetch(&exception, &value, (PyObject **)&tb);
PyErr_Fetch(&exception, &value, &tb);
if (exception == nullptr) { /* Equivalent of `!PyErr_Occurred()`. */
return false;
}

View File

@@ -318,7 +318,7 @@ static PyObject *bpyunits_to_string(PyObject * /*self*/, PyObject *args, PyObjec
PyObject *result;
BKE_unit_value_as_string_adaptive(
buf1, sizeof(buf1), value, precision, usys, ucat, bool(split_unit), false);
buf1, sizeof(buf1), value, precision, usys, ucat, split_unit, false);
if (compatible_unit) {
BKE_unit_name_to_alt(buf2, sizeof(buf2), buf1, usys, ucat);

View File

@@ -89,18 +89,18 @@ PyMODINIT_FUNC PyInit_mathutils();
int EXPP_FloatsAreEqual(float af, float bf, int maxDiff);
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps);
typedef struct Mathutils_Callback Mathutils_Callback;
using Mathutils_Callback = struct Mathutils_Callback;
/** Checks the user is still valid. */
typedef int (*BaseMathCheckFunc)(BaseMathObject *);
using BaseMathCheckFunc = int (*)(BaseMathObject *);
/** Gets the vector from the user. */
typedef int (*BaseMathGetFunc)(BaseMathObject *, int);
using BaseMathGetFunc = int (*)(BaseMathObject *, int);
/** Sets the users vector values once its modified. */
typedef int (*BaseMathSetFunc)(BaseMathObject *, int);
using BaseMathSetFunc = int (*)(BaseMathObject *, int);
/** Same as #BaseMathGetFunc but only for an index. */
typedef int (*BaseMathGetIndexFunc)(BaseMathObject *, int, int);
using BaseMathGetIndexFunc = int (*)(BaseMathObject *, int, int);
/** Same as #BaseMathSetFunc but only for an index. */
typedef int (*BaseMathSetIndexFunc)(BaseMathObject *, int, int);
using BaseMathSetIndexFunc = int (*)(BaseMathObject *, int, int);
struct Mathutils_Callback {
BaseMathCheckFunc check;

View File

@@ -708,7 +708,7 @@ static PyObject *C_Matrix_Rotation(PyObject *cls, PyObject *args)
}
if (vec && PyUnicode_Check(vec)) {
axis = PyUnicode_AsUTF8((PyObject *)vec);
axis = PyUnicode_AsUTF8(vec);
if (axis == nullptr || axis[0] == '\0' || axis[1] != '\0' || axis[0] < 'X' || axis[0] > 'Z') {
PyErr_SetString(PyExc_ValueError,
"Matrix.Rotation(): "
@@ -2254,7 +2254,7 @@ static PyObject *Matrix_identity(MatrixObject *self)
/** Copy `Matrix.copy()` */
static PyObject *Matrix_copy_notest(MatrixObject *self, const float *matrix)
{
return Matrix_CreatePyObject((const float *)matrix, self->col_num, self->row_num, Py_TYPE(self));
return Matrix_CreatePyObject(matrix, self->col_num, self->row_num, Py_TYPE(self));
}
PyDoc_STRVAR(
@@ -3126,8 +3126,7 @@ static PyObject *Matrix_translation_get(MatrixObject *self, void * /*closure*/)
return nullptr;
}
ret = (PyObject *)Vector_CreatePyObject_cb(
(PyObject *)self, 3, mathutils_matrix_translation_cb_index, 3);
ret = Vector_CreatePyObject_cb((PyObject *)self, 3, mathutils_matrix_translation_cb_index, 3);
return ret;
}

View File

@@ -15,7 +15,7 @@
extern PyTypeObject matrix_Type;
extern PyTypeObject matrix_access_Type;
typedef unsigned short ushort;
using ushort = unsigned short;
#define MatrixObject_Check(v) PyObject_TypeCheck((v), &matrix_Type)
#define MatrixObject_CheckExact(v) (Py_TYPE(v) == &matrix_Type)

View File

@@ -99,7 +99,7 @@ static PyObject *vec__apply_to_copy(PyObject *(*vec_func)(VectorObject *), Vecto
PyObject *ret_dummy = vec_func((VectorObject *)ret);
if (ret_dummy) {
Py_DECREF(ret_dummy);
return (PyObject *)ret;
return ret;
}
/* error */
Py_DECREF(ret);
@@ -2813,7 +2813,7 @@ static int Vector_swizzle_set(VectorObject *self, PyObject *value, void *closure
size_from = axis_from;
}
else if ((void)PyErr_Clear(), /* run but ignore the result */
else if (PyErr_Clear(), /* run but ignore the result */
(size_from = size_t(mathutils_array_parse(
vec_assign, 2, 4, value, "Vector.**** = swizzle assignment"))) == size_t(-1))
{

View File

@@ -47,7 +47,7 @@
# include "../bmesh/bmesh_py_types.hh"
#endif /* MATH_STANDALONE */
#include "BLI_strict_flags.h" /* Keep last. */
#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
/* -------------------------------------------------------------------- */
/** \name Documentation String (snippets)
@@ -790,7 +790,7 @@ static PyObject *C_BVHTree_FromPolygons(PyObject * /*cls*/, PyObject *args, PyOb
plink = static_cast<PolyLink *>(BLI_memarena_alloc(
poly_arena, sizeof(*plink) + (sizeof(int) * size_t(py_tricoords_len))));
plink->len = uint(py_tricoords_len);
plink->len = py_tricoords_len;
*p_plink_prev = plink;
p_plink_prev = &plink->next;

View File

@@ -22,7 +22,7 @@
#include "mathutils.hh"
#include "mathutils_kdtree.hh" /* own include */
#include "BLI_strict_flags.h" /* Keep last. */
#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
struct PyKDTree {
PyObject_HEAD

View File

@@ -218,7 +218,7 @@ static float turb(
float amp, out, t;
int i;
amp = 1.0f;
out = float(2.0f * BLI_noise_generic_noise(1.0f, x, y, z, false, nb) - 1.0f);
out = (2.0f * BLI_noise_generic_noise(1.0f, x, y, z, false, nb) - 1.0f);
if (hard) {
out = fabsf(out);
}
@@ -227,7 +227,7 @@ static float turb(
x *= freqscale;
y *= freqscale;
z *= freqscale;
t = float(amp * (2.0f * BLI_noise_generic_noise(1.0f, x, y, z, false, nb) - 1.0f));
t = (amp * (2.0f * BLI_noise_generic_noise(1.0f, x, y, z, false, nb) - 1.0f));
if (hard) {
t = fabsf(t);
}