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

Move away from APIs that use borrowed references under the free-threaded build #8216

Merged
merged 10 commits into from
Jul 13, 2024
9 changes: 9 additions & 0 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,11 @@
ImagingColorItem *v = &items[i];
PyObject *item = Py_BuildValue(
"iN", v->count, getpixel(self->image, self->access, v->x, v->y));
if (item == NULL) {
Py_DECREF(out);
free(items);
return NULL;

Check warning on line 2257 in src/_imaging.c

View check run for this annotation

Codecov / codecov/patch

src/_imaging.c#L2255-L2257

Added lines #L2255 - L2257 were not covered by tests
}
PyList_SetItem(out, i, item);
}
}
Expand Down Expand Up @@ -4448,5 +4453,9 @@
return NULL;
}

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

return m;
}
4 changes: 4 additions & 0 deletions src/_imagingcms.c
Original file line number Diff line number Diff line change
Expand Up @@ -1538,5 +1538,9 @@ PyInit__imagingcms(void) {

PyDateTime_IMPORT;

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

return m;
}
45 changes: 41 additions & 4 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "thirdparty/pythoncapi_compat.h"
#include "libImaging/Imaging.h"

#include <ft2build.h>
Expand Down Expand Up @@ -1209,30 +1210,49 @@
return NULL;
}

int *list_names_filled = PyMem_Malloc(num_namedstyles * sizeof(int));
hugovk marked this conversation as resolved.
Show resolved Hide resolved
if (list_names_filled == NULL) {
Py_DECREF(list_names);
FT_Done_MM_Var(library, master);
return PyErr_NoMemory();

Check warning on line 1217 in src/_imagingft.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingft.c#L1215-L1217

Added lines #L1215 - L1217 were not covered by tests
}

for (int i = 0; i < num_namedstyles; i++) {

Check warning on line 1220 in src/_imagingft.c

View workflow job for this annotation

GitHub Actions / ubuntu-latest Python 3.12

comparison of integer expressions of different signedness: ‘int’ and ‘FT_UInt’ {aka ‘unsigned int’} [-Wsign-compare]
list_names_filled[i] = 0;
}

name_count = FT_Get_Sfnt_Name_Count(self->face);
for (i = 0; i < name_count; i++) {
error = FT_Get_Sfnt_Name(self->face, i, &name);
if (error) {
PyMem_Free(list_names_filled);

Check warning on line 1228 in src/_imagingft.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingft.c#L1228

Added line #L1228 was not covered by tests
Py_DECREF(list_names);
FT_Done_MM_Var(library, master);
return geterror(error);
}

for (j = 0; j < num_namedstyles; j++) {
if (PyList_GetItem(list_names, j) != NULL) {
if (list_names_filled[j]) {
continue;
}

if (master->namedstyle[j].strid == name.name_id) {
list_name = Py_BuildValue("y#", name.string, name.string_len);
if (list_name == NULL) {
PyMem_Free(list_names_filled);
Py_DECREF(list_names);
FT_Done_MM_Var(library, master);
return NULL;

Check warning on line 1245 in src/_imagingft.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingft.c#L1242-L1245

Added lines #L1242 - L1245 were not covered by tests
}
PyList_SetItem(list_names, j, list_name);
list_names_filled[j] = 1;
break;
}
}
}

PyMem_Free(list_names_filled);
FT_Done_MM_Var(library, master);

return list_names;
}

Expand Down Expand Up @@ -1289,9 +1309,15 @@

if (name.name_id == axis.strid) {
axis_name = Py_BuildValue("y#", name.string, name.string_len);
if (axis_name == NULL) {
Py_DECREF(list_axis);
Py_DECREF(list_axes);
FT_Done_MM_Var(library, master);
return NULL;

Check warning on line 1316 in src/_imagingft.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingft.c#L1313-L1316

Added lines #L1313 - L1316 were not covered by tests
}
PyDict_SetItemString(
list_axis, "name", axis_name ? axis_name : Py_None);
Py_XDECREF(axis_name);
Py_DECREF(axis_name);
break;
}
}
Expand Down Expand Up @@ -1345,18 +1371,25 @@
return PyErr_NoMemory();
}
for (i = 0; i < num_coords; i++) {
item = PyList_GET_ITEM(axes, i);
item = PyList_GetItemRef(axes, i);
if (item == NULL) {
free(coords);
return NULL;

Check warning on line 1377 in src/_imagingft.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingft.c#L1376-L1377

Added lines #L1376 - L1377 were not covered by tests
}

if (PyFloat_Check(item)) {
coord = PyFloat_AS_DOUBLE(item);
} else if (PyLong_Check(item)) {
coord = (float)PyLong_AS_LONG(item);
} else if (PyNumber_Check(item)) {
coord = PyFloat_AsDouble(item);
} else {
Py_DECREF(item);

Check warning on line 1387 in src/_imagingft.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingft.c#L1387

Added line #L1387 was not covered by tests
free(coords);
PyErr_SetString(PyExc_TypeError, "list must contain numbers");
return NULL;
}
Py_DECREF(item);
coords[i] = coord * 65536;
}

Expand Down Expand Up @@ -1576,5 +1609,9 @@
return NULL;
}

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

return m;
}
4 changes: 4 additions & 0 deletions src/_imagingmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,9 @@ PyInit__imagingmath(void) {
return NULL;
}

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

return m;
}
4 changes: 4 additions & 0 deletions src/_imagingmorph.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,9 @@ PyInit__imagingmorph(void) {

m = PyModule_Create(&module_def);

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

return m;
}
5 changes: 5 additions & 0 deletions src/_imagingtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@
Py_DECREF(m);
return NULL;
}

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);

Check warning on line 67 in src/_imagingtk.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingtk.c#L67

Added line #L67 was not covered by tests
#endif

return m;
}
4 changes: 4 additions & 0 deletions src/_webp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1005,5 +1005,9 @@ PyInit__webp(void) {
return NULL;
}

#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

return m;
}
26 changes: 21 additions & 5 deletions src/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"

#include "thirdparty/pythoncapi_compat.h"
#include "libImaging/Imaging.h"
#include "libImaging/Gif.h"

Expand Down Expand Up @@ -671,11 +672,17 @@
tags_size = PyList_Size(tags);
TRACE(("tags size: %d\n", (int)tags_size));
for (pos = 0; pos < tags_size; pos++) {
item = PyList_GetItem(tags, pos);
item = PyList_GetItemRef(tags, pos);
if (item == NULL) {
return NULL;

Check warning on line 677 in src/encode.c

View check run for this annotation

Codecov / codecov/patch

src/encode.c#L677

Added line #L677 was not covered by tests
}

if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) {
Py_DECREF(item);

Check warning on line 681 in src/encode.c

View check run for this annotation

Codecov / codecov/patch

src/encode.c#L681

Added line #L681 was not covered by tests
PyErr_SetString(PyExc_ValueError, "Invalid tags list");
return NULL;
}
Py_DECREF(item);
}
pos = 0;
}
Expand Down Expand Up @@ -703,11 +710,17 @@

num_core_tags = sizeof(core_tags) / sizeof(int);
for (pos = 0; pos < tags_size; pos++) {
item = PyList_GetItem(tags, pos);
item = PyList_GetItemRef(tags, pos);
if (item == NULL) {
return NULL;

Check warning on line 715 in src/encode.c

View check run for this annotation

Codecov / codecov/patch

src/encode.c#L715

Added line #L715 was not covered by tests
}

// We already checked that tags is a 2-tuple list.
key = PyTuple_GetItem(item, 0);
key = PyTuple_GET_ITEM(item, 0);
key_int = (int)PyLong_AsLong(key);
value = PyTuple_GetItem(item, 1);
value = PyTuple_GET_ITEM(item, 1);
Py_DECREF(item);

status = 0;
is_core_tag = 0;
is_var_length = 0;
Expand All @@ -721,7 +734,10 @@
}

if (!is_core_tag) {
PyObject *tag_type = PyDict_GetItem(types, key);
PyObject *tag_type;
if (PyDict_GetItemRef(types, key, &tag_type) < 0) {
return NULL; // Exception has been already set

Check warning on line 739 in src/encode.c

View check run for this annotation

Codecov / codecov/patch

src/encode.c#L739

Added line #L739 was not covered by tests
}
if (tag_type) {
int type_int = PyLong_AsLong(tag_type);
if (type_int >= TIFF_BYTE && type_int <= TIFF_DOUBLE) {
Expand Down
13 changes: 10 additions & 3 deletions src/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include "Python.h"
#include "thirdparty/pythoncapi_compat.h"
#include "libImaging/Imaging.h"

#include <math.h>
Expand Down Expand Up @@ -179,14 +180,21 @@
} \
free(xy); \
return -1; \
} \
if (decref) { \
Py_DECREF(op); \
}

/* Copy table to path array */
if (PyList_Check(data)) {
for (i = 0; i < n; i++) {
double x, y;
PyObject *op = PyList_GET_ITEM(data, i);
assign_item_to_array(op, 0);
PyObject *op = PyList_GetItemRef(data, i);
if (op == NULL) {
free(xy);
return -1;

Check warning on line 195 in src/path.c

View check run for this annotation

Codecov / codecov/patch

src/path.c#L194-L195

Added lines #L194 - L195 were not covered by tests
}
assign_item_to_array(op, 1);
}
} else if (PyTuple_Check(data)) {
for (i = 0; i < n; i++) {
Expand All @@ -209,7 +217,6 @@
}
}
assign_item_to_array(op, 1);
Py_DECREF(op);
}
}

Expand Down
Loading
Loading