Skip to content

Commit

Permalink
Merge pull request #8416 from radarhere/compact_within_map
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 1, 2024
2 parents c9c8d45 + a99361a commit 07389b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Tests/test_imagepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def test_overflow_segfault() -> None:
x[i] = b"0" * 16


def test_compact_within_map() -> None:
p = ImagePath.Path([0, 1])

def map_func(x: float, y: float) -> tuple[float, float]:
p.compact()
return 0, 0

with pytest.raises(ValueError):
p.map(map_func)


class Evil:
def __init__(self) -> None:
self.corrupt = Image.core.path(0x4000000000000000)
Expand Down
9 changes: 9 additions & 0 deletions src/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view);
typedef struct {
PyObject_HEAD Py_ssize_t count;
double *xy;
int mapping;
} PyPathObject;

static PyTypeObject PyPathType;
Expand Down Expand Up @@ -91,6 +92,7 @@ path_new(Py_ssize_t count, double *xy, int duplicate) {

path->count = count;
path->xy = xy;
path->mapping = 0;

return path;
}
Expand Down Expand Up @@ -276,6 +278,10 @@ path_compact(PyPathObject *self, PyObject *args) {

double cityblock = 2.0;

if (self->mapping) {
PyErr_SetString(PyExc_ValueError, "Path compacted during mapping");
return NULL;
}
if (!PyArg_ParseTuple(args, "|d:compact", &cityblock)) {
return NULL;
}
Expand Down Expand Up @@ -393,18 +399,21 @@ path_map(PyPathObject *self, PyObject *args) {
xy = self->xy;

/* apply function to coordinate set */
self->mapping = 1;
for (i = 0; i < self->count; i++) {
double x = xy[i + i];
double y = xy[i + i + 1];
PyObject *item = PyObject_CallFunction(function, "dd", x, y);
if (!item || !PyArg_ParseTuple(item, "dd", &x, &y)) {
self->mapping = 0;
Py_XDECREF(item);
return NULL;
}
xy[i + i] = x;
xy[i + i + 1] = y;
Py_DECREF(item);
}
self->mapping = 0;

Py_INCREF(Py_None);
return Py_None;
Expand Down

0 comments on commit 07389b2

Please sign in to comment.