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

Raise an error if path is compacted during mapping #8416

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 212 in Tests/test_imagepath.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_imagepath.py#L212

Added line #L212 was not covered by tests

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
Loading