Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClangFormat to pre-commit #8015

Merged
merged 11 commits into from May 19, 2024
17 changes: 12 additions & 5 deletions .pre-commit-config.yaml
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
rev: v0.4.1
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.3.0
rev: 24.4.1
hooks:
- id: black

Expand All @@ -23,13 +23,20 @@ repos:
- id: remove-tabs
exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.gd$|\.opt$)

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.4
hooks:
- id: clang-format
types: [c]
exclude: ^src/thirdparty/

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: rst-backticks

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
Expand All @@ -43,7 +50,7 @@ repos:
exclude: ^.github/.*TEMPLATE|^Tests/(fonts|images)/

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.1
rev: 0.28.2
hooks:
- id: check-github-workflows
- id: check-readthedocs
Expand All @@ -55,7 +62,7 @@ repos:
- id: sphinx-lint

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 1.7.0
rev: 1.8.0
hooks:
- id: pyproject-fmt

Expand Down
19 changes: 6 additions & 13 deletions src/Tk/tkImaging.c
Expand Up @@ -128,14 +128,7 @@ PyImagingPhotoPut(
block.pixelPtr = (unsigned char *)im->block;

TK_PHOTO_PUT_BLOCK(
interp,
photo,
&block,
0,
0,
block.width,
block.height,
TK_PHOTO_COMPOSITE_SET);
interp, photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET);

return TCL_OK;
}
Expand Down Expand Up @@ -287,7 +280,7 @@ load_tkinter_funcs(void) {
* Return 0 for success, non-zero for failure.
*/

HMODULE* hMods = NULL;
HMODULE *hMods = NULL;
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
Expand All @@ -313,7 +306,7 @@ load_tkinter_funcs(void) {
#endif
return 1;
}
if (!(hMods = (HMODULE*) malloc(cbNeeded))) {
if (!(hMods = (HMODULE *)malloc(cbNeeded))) {
PyErr_NoMemory();
return 1;
}
Expand Down Expand Up @@ -345,7 +338,7 @@ load_tkinter_funcs(void) {
} else if (found_tk == 0) {
PyErr_SetString(PyExc_RuntimeError, "Could not find Tk routines");
}
return (int) ((found_tcl != 1) || (found_tk != 1));
return (int)((found_tcl != 1) || (found_tk != 1));
}

#else /* not Windows */
Expand Down Expand Up @@ -400,8 +393,8 @@ _func_loader(void *lib) {
return 1;
}
return (
(TK_PHOTO_PUT_BLOCK =
(Tk_PhotoPutBlock_t)_dfunc(lib, "Tk_PhotoPutBlock")) == NULL);
(TK_PHOTO_PUT_BLOCK = (Tk_PhotoPutBlock_t)_dfunc(lib, "Tk_PhotoPutBlock")) ==
NULL);
}

int
Expand Down
54 changes: 33 additions & 21 deletions src/_imaging.c
Expand Up @@ -110,7 +110,7 @@

#define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1])
#define L16(p, i) ((((int)p[(i) + 1]) << 8) + p[(i)])
#define S16(v) ((v) < 32768 ? (v) : ((v)-65536))
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))

/* -------------------------------------------------------------------- */
/* OBJECT ADMINISTRATION */
Expand Down Expand Up @@ -533,7 +533,9 @@ getink(PyObject *color, Imaging im, char *ink) {
/* unsigned integer, single layer */
if (rIsInt != 1) {
if (tupleSize != 1) {
PyErr_SetString(PyExc_TypeError, "color must be int or single-element tuple");
PyErr_SetString(
PyExc_TypeError,
"color must be int or single-element tuple");
return NULL;
} else if (!PyArg_ParseTuple(color, "L", &r)) {
return NULL;
Expand All @@ -552,15 +554,20 @@ getink(PyObject *color, Imaging im, char *ink) {
a = 255;
if (im->bands == 2) {
if (tupleSize != 1 && tupleSize != 2) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or two elements");
PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one or two elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
return NULL;
}
g = b = r;
} else {
if (tupleSize != 3 && tupleSize != 4) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one, three or four elements");
PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one, three or four "
"elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
return NULL;
Expand Down Expand Up @@ -599,7 +606,9 @@ getink(PyObject *color, Imaging im, char *ink) {
g = (UINT8)(r >> 8);
r = (UINT8)r;
} else if (tupleSize != 3) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or three elements");
PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one or three elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "iiL", &b, &g, &r)) {
return NULL;
Expand Down Expand Up @@ -1537,14 +1546,14 @@ _putdata(ImagingObject *self, PyObject *args) {
return NULL;
}

#define set_value_to_item(seq, i) \
op = PySequence_Fast_GET_ITEM(seq, i); \
if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, "sequence must be flattened"); \
return NULL; \
} else { \
value = PyFloat_AsDouble(op); \
}
#define set_value_to_item(seq, i) \
op = PySequence_Fast_GET_ITEM(seq, i); \
if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, "sequence must be flattened"); \
return NULL; \
} else { \
value = PyFloat_AsDouble(op); \
}
if (image->image8) {
if (PyBytes_Check(data)) {
unsigned char *p;
Expand Down Expand Up @@ -1596,8 +1605,10 @@ if (PySequence_Check(op)) { \
value = value * scale + offset;
}
if (image->type == IMAGING_TYPE_SPECIAL) {
image->image8[y][x * 2 + (bigendian ? 1 : 0)] = CLIP8((int)value % 256);
image->image8[y][x * 2 + (bigendian ? 0 : 1)] = CLIP8((int)value >> 8);
image->image8[y][x * 2 + (bigendian ? 1 : 0)] =
CLIP8((int)value % 256);
image->image8[y][x * 2 + (bigendian ? 0 : 1)] =
CLIP8((int)value >> 8);
} else {
image->image8[y][x] = (UINT8)CLIP8(value);
}
Expand Down Expand Up @@ -1639,8 +1650,7 @@ if (PySequence_Check(op)) { \
for (i = x = y = 0; i < n; i++) {
double value;
set_value_to_item(seq, i);
IMAGING_PIXEL_INT32(image, x, y) =
(INT32)(value * scale + offset);
IMAGING_PIXEL_INT32(image, x, y) = (INT32)(value * scale + offset);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
Expand Down Expand Up @@ -2785,8 +2795,8 @@ _font_getmask(ImagingFontObject *self, PyObject *args) {
glyph = &self->glyphs[text[i]];
if (i == 0 || text[i] != text[i - 1]) {
ImagingDelete(bitmap);
bitmap =
ImagingCrop(self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1);
bitmap = ImagingCrop(
self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1);
if (!bitmap) {
goto failed;
}
Expand Down Expand Up @@ -3315,7 +3325,8 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) {

free(xy);

if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) < 0) {
if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) <
0) {
free(ixy);
return NULL;
}
Expand Down Expand Up @@ -4411,7 +4422,8 @@ setup_module(PyObject *m) {
PyModule_AddObject(m, "HAVE_XCB", have_xcb);

PyObject *pillow_version = PyUnicode_FromString(version);
PyDict_SetItemString(d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None);
PyDict_SetItemString(
d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None);
Py_XDECREF(pillow_version);

return 0;
Expand Down
16 changes: 5 additions & 11 deletions src/_imagingcms.c
Expand Up @@ -213,11 +213,8 @@ cms_transform_dealloc(CmsTransformObject *self) {

static cmsUInt32Number
findLCMStype(char *PILmode) {
if (
strcmp(PILmode, "RGB") == 0 ||
strcmp(PILmode, "RGBA") == 0 ||
strcmp(PILmode, "RGBX") == 0
) {
if (strcmp(PILmode, "RGB") == 0 || strcmp(PILmode, "RGBA") == 0 ||
strcmp(PILmode, "RGBX") == 0) {
return TYPE_RGBA_8;
}
if (strcmp(PILmode, "RGBA;16B") == 0) {
Expand All @@ -232,10 +229,7 @@ findLCMStype(char *PILmode) {
if (strcmp(PILmode, "L;16B") == 0) {
return TYPE_GRAY_16_SE;
}
if (
strcmp(PILmode, "YCCA") == 0 ||
strcmp(PILmode, "YCC") == 0
) {
if (strcmp(PILmode, "YCCA") == 0 || strcmp(PILmode, "YCC") == 0) {
return TYPE_YCbCr_8;
}
if (strcmp(PILmode, "LAB") == 0) {
Expand Down Expand Up @@ -393,7 +387,7 @@ _buildTransform(

Py_END_ALLOW_THREADS
hugovk marked this conversation as resolved.
Show resolved Hide resolved

if (!hTransform) {
if (!hTransform) {
PyErr_SetString(PyExc_ValueError, "cannot build transform");
}
hugovk marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -427,7 +421,7 @@ _buildProofTransform(

Py_END_ALLOW_THREADS
hugovk marked this conversation as resolved.
Show resolved Hide resolved

if (!hTransform) {
if (!hTransform) {
PyErr_SetString(PyExc_ValueError, "cannot build proof transform");
}

Expand Down