Skip to content

Commit 6944e9e

Browse files
authored
Merge pull request #8216 from hugovk/free-threading
2 parents 8c9eea9 + 851868c commit 6944e9e

10 files changed

+1462
-12
lines changed

src/_imaging.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,11 @@ _getcolors(ImagingObject *self, PyObject *args) {
22512251
ImagingColorItem *v = &items[i];
22522252
PyObject *item = Py_BuildValue(
22532253
"iN", v->count, getpixel(self->image, self->access, v->x, v->y));
2254+
if (item == NULL) {
2255+
Py_DECREF(out);
2256+
free(items);
2257+
return NULL;
2258+
}
22542259
PyList_SetItem(out, i, item);
22552260
}
22562261
}
@@ -4448,5 +4453,9 @@ PyInit__imaging(void) {
44484453
return NULL;
44494454
}
44504455

4456+
#ifdef Py_GIL_DISABLED
4457+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
4458+
#endif
4459+
44514460
return m;
44524461
}

src/_imagingcms.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,5 +1538,9 @@ PyInit__imagingcms(void) {
15381538

15391539
PyDateTime_IMPORT;
15401540

1541+
#ifdef Py_GIL_DISABLED
1542+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
1543+
#endif
1544+
15411545
return m;
15421546
}

src/_imagingft.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#define PY_SSIZE_T_CLEAN
2222
#include "Python.h"
23+
#include "thirdparty/pythoncapi_compat.h"
2324
#include "libImaging/Imaging.h"
2425

2526
#include <ft2build.h>
@@ -1209,30 +1210,49 @@ font_getvarnames(FontObject *self) {
12091210
return NULL;
12101211
}
12111212

1213+
int *list_names_filled = PyMem_Malloc(num_namedstyles * sizeof(int));
1214+
if (list_names_filled == NULL) {
1215+
Py_DECREF(list_names);
1216+
FT_Done_MM_Var(library, master);
1217+
return PyErr_NoMemory();
1218+
}
1219+
1220+
for (int i = 0; i < num_namedstyles; i++) {
1221+
list_names_filled[i] = 0;
1222+
}
1223+
12121224
name_count = FT_Get_Sfnt_Name_Count(self->face);
12131225
for (i = 0; i < name_count; i++) {
12141226
error = FT_Get_Sfnt_Name(self->face, i, &name);
12151227
if (error) {
1228+
PyMem_Free(list_names_filled);
12161229
Py_DECREF(list_names);
12171230
FT_Done_MM_Var(library, master);
12181231
return geterror(error);
12191232
}
12201233

12211234
for (j = 0; j < num_namedstyles; j++) {
1222-
if (PyList_GetItem(list_names, j) != NULL) {
1235+
if (list_names_filled[j]) {
12231236
continue;
12241237
}
12251238

12261239
if (master->namedstyle[j].strid == name.name_id) {
12271240
list_name = Py_BuildValue("y#", name.string, name.string_len);
1241+
if (list_name == NULL) {
1242+
PyMem_Free(list_names_filled);
1243+
Py_DECREF(list_names);
1244+
FT_Done_MM_Var(library, master);
1245+
return NULL;
1246+
}
12281247
PyList_SetItem(list_names, j, list_name);
1248+
list_names_filled[j] = 1;
12291249
break;
12301250
}
12311251
}
12321252
}
12331253

1254+
PyMem_Free(list_names_filled);
12341255
FT_Done_MM_Var(library, master);
1235-
12361256
return list_names;
12371257
}
12381258

@@ -1289,9 +1309,15 @@ font_getvaraxes(FontObject *self) {
12891309

12901310
if (name.name_id == axis.strid) {
12911311
axis_name = Py_BuildValue("y#", name.string, name.string_len);
1312+
if (axis_name == NULL) {
1313+
Py_DECREF(list_axis);
1314+
Py_DECREF(list_axes);
1315+
FT_Done_MM_Var(library, master);
1316+
return NULL;
1317+
}
12921318
PyDict_SetItemString(
12931319
list_axis, "name", axis_name ? axis_name : Py_None);
1294-
Py_XDECREF(axis_name);
1320+
Py_DECREF(axis_name);
12951321
break;
12961322
}
12971323
}
@@ -1345,18 +1371,25 @@ font_setvaraxes(FontObject *self, PyObject *args) {
13451371
return PyErr_NoMemory();
13461372
}
13471373
for (i = 0; i < num_coords; i++) {
1348-
item = PyList_GET_ITEM(axes, i);
1374+
item = PyList_GetItemRef(axes, i);
1375+
if (item == NULL) {
1376+
free(coords);
1377+
return NULL;
1378+
}
1379+
13491380
if (PyFloat_Check(item)) {
13501381
coord = PyFloat_AS_DOUBLE(item);
13511382
} else if (PyLong_Check(item)) {
13521383
coord = (float)PyLong_AS_LONG(item);
13531384
} else if (PyNumber_Check(item)) {
13541385
coord = PyFloat_AsDouble(item);
13551386
} else {
1387+
Py_DECREF(item);
13561388
free(coords);
13571389
PyErr_SetString(PyExc_TypeError, "list must contain numbers");
13581390
return NULL;
13591391
}
1392+
Py_DECREF(item);
13601393
coords[i] = coord * 65536;
13611394
}
13621395

@@ -1576,5 +1609,9 @@ PyInit__imagingft(void) {
15761609
return NULL;
15771610
}
15781611

1612+
#ifdef Py_GIL_DISABLED
1613+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
1614+
#endif
1615+
15791616
return m;
15801617
}

src/_imagingmath.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,9 @@ PyInit__imagingmath(void) {
290290
return NULL;
291291
}
292292

293+
#ifdef Py_GIL_DISABLED
294+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
295+
#endif
296+
293297
return m;
294298
}

src/_imagingmorph.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,9 @@ PyInit__imagingmorph(void) {
269269

270270
m = PyModule_Create(&module_def);
271271

272+
#ifdef Py_GIL_DISABLED
273+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
274+
#endif
275+
272276
return m;
273277
}

src/_imagingtk.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@ PyInit__imagingtk(void) {
6262
Py_DECREF(m);
6363
return NULL;
6464
}
65+
66+
#ifdef Py_GIL_DISABLED
67+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
68+
#endif
69+
6570
return m;
6671
}

src/_webp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,5 +1005,9 @@ PyInit__webp(void) {
10051005
return NULL;
10061006
}
10071007

1008+
#ifdef Py_GIL_DISABLED
1009+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
1010+
#endif
1011+
10081012
return m;
10091013
}

src/encode.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define PY_SSIZE_T_CLEAN
2626
#include "Python.h"
2727

28+
#include "thirdparty/pythoncapi_compat.h"
2829
#include "libImaging/Imaging.h"
2930
#include "libImaging/Gif.h"
3031

@@ -671,11 +672,17 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
671672
tags_size = PyList_Size(tags);
672673
TRACE(("tags size: %d\n", (int)tags_size));
673674
for (pos = 0; pos < tags_size; pos++) {
674-
item = PyList_GetItem(tags, pos);
675+
item = PyList_GetItemRef(tags, pos);
676+
if (item == NULL) {
677+
return NULL;
678+
}
679+
675680
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) {
681+
Py_DECREF(item);
676682
PyErr_SetString(PyExc_ValueError, "Invalid tags list");
677683
return NULL;
678684
}
685+
Py_DECREF(item);
679686
}
680687
pos = 0;
681688
}
@@ -703,11 +710,17 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
703710

704711
num_core_tags = sizeof(core_tags) / sizeof(int);
705712
for (pos = 0; pos < tags_size; pos++) {
706-
item = PyList_GetItem(tags, pos);
713+
item = PyList_GetItemRef(tags, pos);
714+
if (item == NULL) {
715+
return NULL;
716+
}
717+
707718
// We already checked that tags is a 2-tuple list.
708-
key = PyTuple_GetItem(item, 0);
719+
key = PyTuple_GET_ITEM(item, 0);
709720
key_int = (int)PyLong_AsLong(key);
710-
value = PyTuple_GetItem(item, 1);
721+
value = PyTuple_GET_ITEM(item, 1);
722+
Py_DECREF(item);
723+
711724
status = 0;
712725
is_core_tag = 0;
713726
is_var_length = 0;
@@ -721,7 +734,10 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
721734
}
722735

723736
if (!is_core_tag) {
724-
PyObject *tag_type = PyDict_GetItem(types, key);
737+
PyObject *tag_type;
738+
if (PyDict_GetItemRef(types, key, &tag_type) < 0) {
739+
return NULL; // Exception has been already set
740+
}
725741
if (tag_type) {
726742
int type_int = PyLong_AsLong(tag_type);
727743
if (type_int >= TIFF_BYTE && type_int <= TIFF_DOUBLE) {

src/path.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include "Python.h"
29+
#include "thirdparty/pythoncapi_compat.h"
2930
#include "libImaging/Imaging.h"
3031

3132
#include <math.h>
@@ -179,14 +180,21 @@ PyPath_Flatten(PyObject *data, double **pxy) {
179180
} \
180181
free(xy); \
181182
return -1; \
183+
} \
184+
if (decref) { \
185+
Py_DECREF(op); \
182186
}
183187

184188
/* Copy table to path array */
185189
if (PyList_Check(data)) {
186190
for (i = 0; i < n; i++) {
187191
double x, y;
188-
PyObject *op = PyList_GET_ITEM(data, i);
189-
assign_item_to_array(op, 0);
192+
PyObject *op = PyList_GetItemRef(data, i);
193+
if (op == NULL) {
194+
free(xy);
195+
return -1;
196+
}
197+
assign_item_to_array(op, 1);
190198
}
191199
} else if (PyTuple_Check(data)) {
192200
for (i = 0; i < n; i++) {
@@ -209,7 +217,6 @@ PyPath_Flatten(PyObject *data, double **pxy) {
209217
}
210218
}
211219
assign_item_to_array(op, 1);
212-
Py_DECREF(op);
213220
}
214221
}
215222

0 commit comments

Comments
 (0)