2019-07-06 23:40:53 +03:00
|
|
|
import array
|
|
|
|
import struct
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImagePath
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImagePath:
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_path(self):
|
|
|
|
|
|
|
|
p = ImagePath.Path(list(range(10)))
|
|
|
|
|
|
|
|
# sequence interface
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(p) == 5
|
|
|
|
assert p[0] == (0.0, 1.0)
|
|
|
|
assert p[-1] == (8.0, 9.0)
|
|
|
|
assert list(p[:1]) == [(0.0, 1.0)]
|
|
|
|
with pytest.raises(TypeError) as cm:
|
2019-06-13 18:54:46 +03:00
|
|
|
p["foo"]
|
2020-02-22 16:06:21 +03:00
|
|
|
assert str(cm.value) == "Path indices must be integers, not str"
|
|
|
|
assert list(p) == [(0.0, 1.0), (2.0, 3.0), (4.0, 5.0), (6.0, 7.0), (8.0, 9.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# method sanity check
|
2020-02-22 16:06:21 +03:00
|
|
|
assert p.tolist() == [
|
|
|
|
(0.0, 1.0),
|
|
|
|
(2.0, 3.0),
|
|
|
|
(4.0, 5.0),
|
|
|
|
(6.0, 7.0),
|
|
|
|
(8.0, 9.0),
|
|
|
|
]
|
|
|
|
assert p.tolist(1) == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert p.getbbox() == (0.0, 1.0, 8.0, 9.0)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert p.compact(5) == 2
|
|
|
|
assert list(p) == [(0.0, 1.0), (4.0, 5.0), (8.0, 9.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
p.transform((1, 0, 1, 0, 1, 1))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(1.0, 2.0), (5.0, 6.0), (9.0, 10.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# alternative constructors
|
|
|
|
p = ImagePath.Path([0, 1])
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path([0.0, 1.0])
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path([0, 1])
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path([(0, 1)])
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path(p)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path(p.tolist(0))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path(p.tolist(1))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path(array.array("f", [0, 1]))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
arr = array.array("f", [0, 1])
|
2019-06-13 18:54:46 +03:00
|
|
|
if hasattr(arr, "tobytes"):
|
2014-06-10 13:10:47 +04:00
|
|
|
p = ImagePath.Path(arr.tobytes())
|
|
|
|
else:
|
|
|
|
p = ImagePath.Path(arr.tostring())
|
2020-02-22 16:06:21 +03:00
|
|
|
assert list(p) == [(0.0, 1.0)]
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2016-03-15 22:56:40 +03:00
|
|
|
def test_overflow_segfault(self):
|
2016-11-06 19:58:45 +03:00
|
|
|
# Some Pythons fail getting the argument as an integer, and it falls
|
|
|
|
# through to the sequence. Seeing this on 32-bit Windows.
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises((TypeError, MemoryError)):
|
2016-03-15 22:56:40 +03:00
|
|
|
# post patch, this fails with a memory error
|
|
|
|
x = evil()
|
2016-05-10 15:23:23 +03:00
|
|
|
|
2016-03-15 22:56:40 +03:00
|
|
|
# This fails due to the invalid malloc above,
|
|
|
|
# and segfaults
|
2016-03-16 13:03:38 +03:00
|
|
|
for i in range(200000):
|
2019-09-26 15:12:28 +03:00
|
|
|
x[i] = b"0" * 16
|
2016-03-15 22:56:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class evil:
|
2016-05-10 15:23:23 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.corrupt = Image.core.path(0x4000000000000000)
|
2016-03-15 22:56:40 +03:00
|
|
|
|
2016-05-10 15:23:23 +03:00
|
|
|
def __getitem__(self, i):
|
|
|
|
x = self.corrupt[i]
|
|
|
|
return struct.pack("dd", x[0], x[1])
|
2016-03-15 22:56:40 +03:00
|
|
|
|
2016-05-10 15:23:23 +03:00
|
|
|
def __setitem__(self, i, x):
|
|
|
|
self.corrupt[i] = struct.unpack("dd", x)
|