diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index ba018f2d9a1..1e291ba7c46 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -216,6 +216,11 @@ def test_overflow_segfault() -> None: x[i] = b"0" * 16 +def test_compact_empty_path() -> None: + p = ImagePath.Path(0) + assert p.compact() == 0 + + def test_compact_within_map() -> None: p = ImagePath.Path([0, 1]) diff --git a/src/path.c b/src/path.c index d390247b292..b4a56d67134 100644 --- a/src/path.c +++ b/src/path.c @@ -326,6 +326,10 @@ path_compact(PyPathObject *self, PyObject *args) { PyErr_SetString(PyExc_ValueError, "Path compacted during mapping"); return NULL; } + if (self->count == 0) { + // no points to remove + return Py_BuildValue("i", 0); + } if (!PyArg_ParseTuple(args, "|d:compact", &cityblock)) { return NULL; }