From bdbf1694fc00838fb9e9606b75cb65fb2d129d25 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 14 Jan 2021 21:31:25 +1100 Subject: [PATCH 1/2] Allow PixelAccess to use Python __int__ when parsing x and y --- Tests/test_image_access.py | 12 ++++++++++++ src/_imaging.c | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index e86dc8530..5fa0e2cbf 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -23,6 +23,11 @@ else: except ImportError: cffi = None +try: + import numpy +except ImportError: + numpy = None + class AccessTest: # initial value @@ -109,6 +114,13 @@ class TestImagePutPixel(AccessTest): assert_image_equal(im1, im2) + @pytest.mark.skipif(numpy is None, reason="NumPy not installed") + def test_numpy(self): + im = hopper() + pix = im.load() + + assert pix[numpy.int32(1), numpy.int32(2)] == (18, 20, 59) + class TestImageGetPixel(AccessTest): @staticmethod diff --git a/src/_imaging.c b/src/_imaging.c index 01dd22486..3072dcb21 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1109,7 +1109,12 @@ _getxy(PyObject *xy, int *x, int *y) { } else if (PyFloat_Check(value)) { *x = (int)PyFloat_AS_DOUBLE(value); } else { - goto badval; + PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL); + if (int_value != NULL && PyLong_Check(int_value)) { + *x = PyLong_AS_LONG(int_value); + } else { + goto badval; + } } value = PyTuple_GET_ITEM(xy, 1); @@ -1118,7 +1123,12 @@ _getxy(PyObject *xy, int *x, int *y) { } else if (PyFloat_Check(value)) { *y = (int)PyFloat_AS_DOUBLE(value); } else { - goto badval; + PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL); + if (int_value != NULL && PyLong_Check(int_value)) { + *y = PyLong_AS_LONG(int_value); + } else { + goto badval; + } } return 0; From 7b4b356fc087068fee3316e569b3d3e7ada731f0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 14 Jan 2021 22:09:11 +1100 Subject: [PATCH 2/2] Test for incorrect PixelAccess arguments --- Tests/test_image_access.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 5fa0e2cbf..78d04946e 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -71,6 +71,10 @@ class TestImagePutPixel(AccessTest): pix1 = im1.load() pix2 = im2.load() + for x, y in ((0, "0"), ("0", 0)): + with pytest.raises(TypeError): + pix1[x, y] + for y in range(im1.size[1]): for x in range(im1.size[0]): pix2[x, y] = pix1[x, y]