Skip to content

Commit

Permalink
Split into x and y errors
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 1, 2023
1 parent b84c29a commit a4965a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
15 changes: 8 additions & 7 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,18 +1504,19 @@ def test_polygon2():
assert_image_similar_tofile(im, expected, 1)


def test_incorrectly_ordered_coordinates():
@pytest.mark.parametrize("xy", ((1, 1, 0, 1), (1, 1, 1, 0)))
def test_incorrectly_ordered_coordinates(xy):
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
with pytest.raises(ValueError):
draw.arc((1, 1, 0, 0), 10, 260)
draw.arc(xy, 10, 260)
with pytest.raises(ValueError):
draw.chord((1, 1, 0, 0), 10, 260)
draw.chord(xy, 10, 260)
with pytest.raises(ValueError):
draw.ellipse((1, 1, 0, 0))
draw.ellipse(xy)
with pytest.raises(ValueError):
draw.pieslice((1, 1, 0, 0), 10, 260)
draw.pieslice(xy, 10, 260)
with pytest.raises(ValueError):
draw.rectangle((1, 1, 0, 0))
draw.rectangle(xy)
with pytest.raises(ValueError):
draw.rounded_rectangle((1, 1, 0, 0))
draw.rounded_rectangle(xy)
10 changes: 5 additions & 5 deletions src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ def rounded_rectangle(
(x0, y0), (x1, y1) = xy
else:
x0, y0, x1, y1 = xy
if x1 < x0 or y1 < y0:
msg = (
"x1 must be greater than or equal to x0,"
" and y1 must be greater than or equal to y0"
)
if x1 < x0:
msg = "x1 must be greater than or equal to x0"
raise ValueError(msg)
if y1 < y0:
msg = "y1 must be greater than or equal to y0"
raise ValueError(msg)
if corners is None:
corners = (True, True, True, True)
Expand Down
51 changes: 39 additions & 12 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) {
static const char *must_be_sequence = "argument must be a sequence";
static const char *must_be_two_coordinates =
"coordinate list must contain exactly 2 coordinates";
static const char *incorrectly_ordered_coordinates =
"x1 must be greater than or equal to x0, and y1 must be greater than or equal to y0";
static const char *incorrectly_ordered_x_coordinate =
"x1 must be greater than or equal to x0";
static const char *incorrectly_ordered_y_coordinate =
"y1 must be greater than or equal to y0";
static const char *wrong_mode = "unrecognized image mode";
static const char *wrong_raw_mode = "unrecognized raw mode";
static const char *outside_image = "image index out of range";
Expand Down Expand Up @@ -2807,8 +2809,13 @@ _draw_arc(ImagingDrawObject *self, PyObject *args) {
free(xy);
return NULL;
}
if (xy[2] < xy[0] || xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_coordinates);
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
Expand Down Expand Up @@ -2893,8 +2900,13 @@ _draw_chord(ImagingDrawObject *self, PyObject *args) {
free(xy);
return NULL;
}
if (xy[2] < xy[0] || xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_coordinates);
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
Expand Down Expand Up @@ -2944,8 +2956,13 @@ _draw_ellipse(ImagingDrawObject *self, PyObject *args) {
free(xy);
return NULL;
}
if (xy[2] < xy[0] || xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_coordinates);
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
Expand Down Expand Up @@ -3118,8 +3135,13 @@ _draw_pieslice(ImagingDrawObject *self, PyObject *args) {
free(xy);
return NULL;
}
if (xy[2] < xy[0] || xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_coordinates);
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
Expand Down Expand Up @@ -3219,8 +3241,13 @@ _draw_rectangle(ImagingDrawObject *self, PyObject *args) {
free(xy);
return NULL;
}
if (xy[2] < xy[0] || xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_coordinates);
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
Expand Down

0 comments on commit a4965a7

Please sign in to comment.