2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
from io import BytesIO
|
2024-02-15 12:20:42 +03:00
|
|
|
from typing import Any
|
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
|
|
|
|
2022-05-04 14:25:40 +03:00
|
|
|
from PIL import (
|
|
|
|
BmpImagePlugin,
|
|
|
|
EpsImagePlugin,
|
|
|
|
Image,
|
|
|
|
ImageFile,
|
|
|
|
UnidentifiedImageError,
|
|
|
|
_binary,
|
|
|
|
features,
|
|
|
|
)
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image,
|
|
|
|
assert_image_equal,
|
|
|
|
assert_image_similar,
|
|
|
|
fromstring,
|
|
|
|
hopper,
|
2020-02-18 01:03:32 +03:00
|
|
|
skip_unless_feature,
|
2020-01-30 17:56:07 +03:00
|
|
|
tostring,
|
|
|
|
)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
# save original block sizes
|
|
|
|
MAXBLOCK = ImageFile.MAXBLOCK
|
|
|
|
SAFEBLOCK = ImageFile.SAFEBLOCK
|
|
|
|
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImageFile:
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_parser(self) -> None:
|
2024-02-17 07:00:38 +03:00
|
|
|
def roundtrip(format: str) -> tuple[Image.Image, Image.Image]:
|
2022-01-15 01:02:31 +03:00
|
|
|
im = hopper("L").resize((1000, 1000), Image.Resampling.NEAREST)
|
2014-06-10 13:10:47 +04:00
|
|
|
if format in ("MSP", "XBM"):
|
|
|
|
im = im.convert("1")
|
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = BytesIO()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-05-13 09:39:25 +03:00
|
|
|
im.copy().save(test_file, format)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
data = test_file.getvalue()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
parser = ImageFile.Parser()
|
|
|
|
parser.feed(data)
|
2022-04-10 21:23:31 +03:00
|
|
|
im_out = parser.close()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-04-10 21:23:31 +03:00
|
|
|
return im, im_out
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(*roundtrip("BMP"))
|
2015-05-13 09:39:25 +03:00
|
|
|
im1, im2 = roundtrip("GIF")
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(im1.convert("P"), im2, 1)
|
|
|
|
assert_image_equal(*roundtrip("IM"))
|
|
|
|
assert_image_equal(*roundtrip("MSP"))
|
2020-02-18 01:03:32 +03:00
|
|
|
if features.check("zlib"):
|
2014-06-10 13:10:47 +04:00
|
|
|
try:
|
|
|
|
# force multiple blocks in PNG driver
|
|
|
|
ImageFile.MAXBLOCK = 8192
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(*roundtrip("PNG"))
|
2014-06-10 13:10:47 +04:00
|
|
|
finally:
|
|
|
|
ImageFile.MAXBLOCK = MAXBLOCK
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(*roundtrip("PPM"))
|
|
|
|
assert_image_equal(*roundtrip("TIFF"))
|
|
|
|
assert_image_equal(*roundtrip("XBM"))
|
|
|
|
assert_image_equal(*roundtrip("TGA"))
|
|
|
|
assert_image_equal(*roundtrip("PCX"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if EpsImagePlugin.has_ghostscript():
|
|
|
|
im1, im2 = roundtrip("EPS")
|
2014-09-30 09:14:26 +04:00
|
|
|
# This test fails on Ubuntu 12.04, PPC (Bigendian) It
|
|
|
|
# appears to be a ghostscript 9.05 bug, since the
|
|
|
|
# ghostscript rendering is wonky and the file is identical
|
2015-04-24 02:26:52 +03:00
|
|
|
# to that written on ubuntu 12.04 x64
|
2014-09-30 09:14:26 +04:00
|
|
|
# md5sum: ba974835ff2d6f3f2fd0053a23521d4a
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# EPS comes back in RGB:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(im1, im2.convert("L"), 20)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
if features.check("jpg"):
|
2014-06-10 13:10:47 +04:00
|
|
|
im1, im2 = roundtrip("JPEG") # lossy compression
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im1, im2.mode, im2.size)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
roundtrip("PDF")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ico(self) -> None:
|
2019-06-13 18:54:46 +03:00
|
|
|
with open("Tests/images/python.ico", "rb") as f:
|
2014-06-10 13:10:47 +04:00
|
|
|
data = f.read()
|
2017-10-07 15:18:23 +03:00
|
|
|
with ImageFile.Parser() as p:
|
|
|
|
p.feed(data)
|
2024-07-08 13:09:45 +03:00
|
|
|
assert p.image is not None
|
2020-02-22 16:06:21 +03:00
|
|
|
assert (48, 48) == p.image.size
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2021-04-14 13:01:56 +03:00
|
|
|
@skip_unless_feature("webp")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_incremental_webp(self) -> None:
|
2021-04-14 13:01:56 +03:00
|
|
|
with ImageFile.Parser() as p:
|
|
|
|
with open("Tests/images/hopper.webp", "rb") as f:
|
|
|
|
p.feed(f.read(1024))
|
|
|
|
|
|
|
|
# Check that insufficient data was given in the first feed
|
|
|
|
assert not p.image
|
|
|
|
|
|
|
|
p.feed(f.read())
|
2024-07-08 13:09:45 +03:00
|
|
|
assert p.image is not None
|
2021-04-14 13:01:56 +03:00
|
|
|
assert (128, 128) == p.image.size
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_safeblock(self) -> None:
|
2015-07-31 15:49:46 +03:00
|
|
|
im1 = hopper()
|
|
|
|
|
2013-04-21 12:13:33 +04:00
|
|
|
try:
|
2014-06-10 13:10:47 +04:00
|
|
|
ImageFile.SAFEBLOCK = 1
|
|
|
|
im2 = fromstring(tostring(im1, "PNG"))
|
2013-04-21 12:13:33 +04:00
|
|
|
finally:
|
2014-06-10 13:10:47 +04:00
|
|
|
ImageFile.SAFEBLOCK = SAFEBLOCK
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im1, im2)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_raise_oserror(self) -> None:
|
2023-12-11 00:32:27 +03:00
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
ImageFile.raise_oserror(1)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_raise_typeerror(self) -> None:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(TypeError):
|
2017-05-13 19:07:05 +03:00
|
|
|
parser = ImageFile.Parser()
|
2024-07-12 14:16:56 +03:00
|
|
|
parser.feed(1) # type: ignore[arg-type]
|
2017-05-13 19:07:05 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_negative_stride(self) -> None:
|
2019-09-29 07:16:30 +03:00
|
|
|
with open("Tests/images/raw_negative_stride.bin", "rb") as f:
|
|
|
|
input = f.read()
|
|
|
|
p = ImageFile.Parser()
|
|
|
|
p.feed(input)
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2019-09-29 07:16:30 +03:00
|
|
|
p.close()
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_no_format(self) -> None:
|
2022-02-18 09:24:39 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
|
|
|
|
|
|
|
class DummyImageFile(ImageFile.ImageFile):
|
2024-01-31 12:12:58 +03:00
|
|
|
def _open(self) -> None:
|
2023-07-29 02:28:18 +03:00
|
|
|
self._mode = "RGB"
|
2022-02-18 09:24:39 +03:00
|
|
|
self._size = (1, 1)
|
|
|
|
|
|
|
|
im = DummyImageFile(buf)
|
|
|
|
assert im.format is None
|
|
|
|
assert im.get_format_mimetype() is None
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_oserror(self) -> None:
|
2022-02-18 09:24:39 +03:00
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.save(BytesIO(), "JPEG2000", num_resolutions=2)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_truncated(self) -> None:
|
2021-12-07 03:40:44 +03:00
|
|
|
b = BytesIO(
|
|
|
|
b"BM000000000000" # head_data
|
|
|
|
+ _binary.o32le(
|
|
|
|
ImageFile.SAFEBLOCK + 1 + 4
|
|
|
|
) # header_size, so BmpImagePlugin will try to read SAFEBLOCK + 1 bytes
|
|
|
|
+ (
|
|
|
|
b"0" * ImageFile.SAFEBLOCK
|
|
|
|
) # only SAFEBLOCK bytes, so that the header is truncated
|
|
|
|
)
|
|
|
|
with pytest.raises(OSError) as e:
|
|
|
|
BmpImagePlugin.BmpImageFile(b)
|
|
|
|
assert str(e.value) == "Truncated File Read"
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_truncated_with_errors(self) -> None:
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open("Tests/images/truncated_image.png") as im:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
im.load()
|
2015-07-31 15:49:46 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
# Test that the error is raised if loaded a second time
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
im.load()
|
2019-07-13 01:37:17 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_truncated_without_errors(self) -> None:
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/truncated_image.png") as im:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
|
|
|
try:
|
|
|
|
im.load()
|
|
|
|
finally:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_broken_datastream_with_errors(self) -> None:
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/broken_data_stream.png") as im:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2019-11-25 23:03:23 +03:00
|
|
|
im.load()
|
2015-09-15 20:12:16 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_broken_datastream_without_errors(self) -> None:
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/broken_data_stream.png") as im:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
|
|
|
try:
|
|
|
|
im.load()
|
|
|
|
finally:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
2015-09-15 20:12:16 +03:00
|
|
|
|
2017-03-11 18:35:03 +03:00
|
|
|
|
|
|
|
class MockPyDecoder(ImageFile.PyDecoder):
|
2024-05-29 15:51:02 +03:00
|
|
|
last: MockPyDecoder
|
|
|
|
|
2024-02-15 12:20:42 +03:00
|
|
|
def __init__(self, mode: str, *args: Any) -> None:
|
|
|
|
MockPyDecoder.last = self
|
|
|
|
|
|
|
|
super().__init__(mode, *args)
|
|
|
|
|
2024-06-24 14:04:33 +03:00
|
|
|
def decode(self, buffer: bytes) -> tuple[int, int]:
|
2017-04-20 14:14:23 +03:00
|
|
|
# eof
|
2018-10-02 11:55:28 +03:00
|
|
|
return -1, 0
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
class MockPyEncoder(ImageFile.PyEncoder):
|
2024-05-29 15:51:02 +03:00
|
|
|
last: MockPyEncoder | None
|
|
|
|
|
2024-02-15 12:20:42 +03:00
|
|
|
def __init__(self, mode: str, *args: Any) -> None:
|
|
|
|
MockPyEncoder.last = self
|
|
|
|
|
|
|
|
super().__init__(mode, *args)
|
|
|
|
|
2024-06-24 14:04:33 +03:00
|
|
|
def encode(self, bufsize: int) -> tuple[int, int, bytes]:
|
2022-02-25 08:07:01 +03:00
|
|
|
return 1, 1, b""
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def cleanup(self) -> None:
|
2022-02-28 06:12:56 +03:00
|
|
|
self.cleanup_called = True
|
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
2017-03-11 18:35:03 +03:00
|
|
|
xoff, yoff, xsize, ysize = 10, 20, 100, 100
|
2017-04-20 14:14:23 +03:00
|
|
|
|
|
|
|
|
2017-03-11 18:35:03 +03:00
|
|
|
class MockImageFile(ImageFile.ImageFile):
|
2024-01-31 12:12:58 +03:00
|
|
|
def _open(self) -> None:
|
2019-06-13 18:54:46 +03:00
|
|
|
self.rawmode = "RGBA"
|
2023-07-29 02:28:18 +03:00
|
|
|
self._mode = "RGBA"
|
2018-09-30 05:58:02 +03:00
|
|
|
self._size = (200, 200)
|
2019-06-13 18:54:46 +03:00
|
|
|
self.tile = [("MOCK", (xoff, yoff, xoff + xsize, yoff + ysize), 32, None)]
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
class CodecsTest:
|
|
|
|
@classmethod
|
2024-01-31 12:12:58 +03:00
|
|
|
def setup_class(cls) -> None:
|
2024-02-15 12:20:42 +03:00
|
|
|
Image.register_decoder("MOCK", MockPyDecoder)
|
|
|
|
Image.register_encoder("MOCK", MockPyEncoder)
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
|
|
|
class TestPyDecoder(CodecsTest):
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_setimage(self) -> None:
|
2019-06-13 18:54:46 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
2017-03-11 18:35:03 +03:00
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
|
|
|
|
|
|
|
im.load()
|
|
|
|
|
2024-02-15 12:20:42 +03:00
|
|
|
assert MockPyDecoder.last.state.xoff == xoff
|
|
|
|
assert MockPyDecoder.last.state.yoff == yoff
|
|
|
|
assert MockPyDecoder.last.state.xsize == xsize
|
|
|
|
assert MockPyDecoder.last.state.ysize == ysize
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-02-15 12:20:42 +03:00
|
|
|
MockPyDecoder.last.set_as_raw(b"\x00")
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_extents_none(self) -> None:
|
2019-06-13 18:54:46 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
2017-03-11 19:43:44 +03:00
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
|
|
|
im.tile = [("MOCK", None, 32, None)]
|
|
|
|
|
|
|
|
im.load()
|
|
|
|
|
2024-02-15 12:20:42 +03:00
|
|
|
assert MockPyDecoder.last.state.xoff == 0
|
|
|
|
assert MockPyDecoder.last.state.yoff == 0
|
|
|
|
assert MockPyDecoder.last.state.xsize == 200
|
|
|
|
assert MockPyDecoder.last.state.ysize == 200
|
2017-03-11 19:43:44 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_negsize(self) -> None:
|
2019-06-13 18:54:46 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
2017-03-11 18:35:03 +03:00
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
2019-06-13 18:54:46 +03:00
|
|
|
im.tile = [("MOCK", (xoff, yoff, -10, yoff + ysize), 32, None)]
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.load()
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
im.tile = [("MOCK", (xoff, yoff, xoff + xsize, -10), 32, None)]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.load()
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_oversize(self) -> None:
|
2019-06-13 18:54:46 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
2017-03-11 18:35:03 +03:00
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
2019-06-13 18:54:46 +03:00
|
|
|
im.tile = [("MOCK", (xoff, yoff, xoff + xsize + 100, yoff + ysize), 32, None)]
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.load()
|
2017-03-11 18:35:03 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
im.tile = [("MOCK", (xoff, yoff, xoff + xsize, yoff + ysize + 100), 32, None)]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.load()
|
2022-02-25 08:07:01 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_decode(self) -> None:
|
2024-07-12 14:16:56 +03:00
|
|
|
decoder = ImageFile.PyDecoder("")
|
2022-02-25 08:07:01 +03:00
|
|
|
with pytest.raises(NotImplementedError):
|
2024-07-05 20:56:24 +03:00
|
|
|
decoder.decode(b"")
|
2022-02-25 08:07:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestPyEncoder(CodecsTest):
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_setimage(self) -> None:
|
2022-02-25 08:07:01 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
|
|
|
|
|
|
|
fp = BytesIO()
|
|
|
|
ImageFile._save(
|
2024-08-05 08:20:34 +03:00
|
|
|
im,
|
|
|
|
fp,
|
|
|
|
[
|
|
|
|
ImageFile._Tile(
|
|
|
|
"MOCK", (xoff, yoff, xoff + xsize, yoff + ysize), 0, "RGB"
|
|
|
|
)
|
|
|
|
],
|
2022-02-25 08:07:01 +03:00
|
|
|
)
|
|
|
|
|
2024-05-29 15:51:02 +03:00
|
|
|
assert MockPyEncoder.last
|
2024-02-15 12:20:42 +03:00
|
|
|
assert MockPyEncoder.last.state.xoff == xoff
|
|
|
|
assert MockPyEncoder.last.state.yoff == yoff
|
|
|
|
assert MockPyEncoder.last.state.xsize == xsize
|
|
|
|
assert MockPyEncoder.last.state.ysize == ysize
|
2022-02-25 08:07:01 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_extents_none(self) -> None:
|
2022-02-25 08:07:01 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
|
|
|
im.tile = [("MOCK", None, 32, None)]
|
|
|
|
|
|
|
|
fp = BytesIO()
|
2024-08-05 08:20:34 +03:00
|
|
|
ImageFile._save(im, fp, [ImageFile._Tile("MOCK", None, 0, "RGB")])
|
2022-02-25 08:07:01 +03:00
|
|
|
|
2024-05-29 15:51:02 +03:00
|
|
|
assert MockPyEncoder.last
|
2024-02-15 12:20:42 +03:00
|
|
|
assert MockPyEncoder.last.state.xoff == 0
|
|
|
|
assert MockPyEncoder.last.state.yoff == 0
|
|
|
|
assert MockPyEncoder.last.state.xsize == 200
|
|
|
|
assert MockPyEncoder.last.state.ysize == 200
|
2022-02-25 08:07:01 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_negsize(self) -> None:
|
2022-02-25 08:07:01 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
|
|
|
|
|
|
|
fp = BytesIO()
|
2024-02-15 12:20:42 +03:00
|
|
|
MockPyEncoder.last = None
|
2022-02-25 08:07:01 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImageFile._save(
|
2024-08-05 08:20:34 +03:00
|
|
|
im,
|
|
|
|
fp,
|
|
|
|
[ImageFile._Tile("MOCK", (xoff, yoff, -10, yoff + ysize), 0, "RGB")],
|
2022-02-25 08:07:01 +03:00
|
|
|
)
|
2024-06-23 23:59:00 +03:00
|
|
|
last: MockPyEncoder | None = MockPyEncoder.last
|
|
|
|
assert last
|
|
|
|
assert last.cleanup_called
|
2022-02-25 08:07:01 +03:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImageFile._save(
|
2024-08-05 08:20:34 +03:00
|
|
|
im,
|
|
|
|
fp,
|
|
|
|
[ImageFile._Tile("MOCK", (xoff, yoff, xoff + xsize, -10), 0, "RGB")],
|
2022-02-25 08:07:01 +03:00
|
|
|
)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_oversize(self) -> None:
|
2022-02-25 08:07:01 +03:00
|
|
|
buf = BytesIO(b"\x00" * 255)
|
|
|
|
|
|
|
|
im = MockImageFile(buf)
|
|
|
|
|
|
|
|
fp = BytesIO()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImageFile._save(
|
|
|
|
im,
|
|
|
|
fp,
|
2024-08-05 08:20:34 +03:00
|
|
|
[
|
|
|
|
ImageFile._Tile(
|
|
|
|
"MOCK", (xoff, yoff, xoff + xsize + 100, yoff + ysize), 0, "RGB"
|
|
|
|
)
|
|
|
|
],
|
2022-02-25 08:07:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImageFile._save(
|
|
|
|
im,
|
|
|
|
fp,
|
2024-08-05 08:20:34 +03:00
|
|
|
[
|
|
|
|
ImageFile._Tile(
|
|
|
|
"MOCK", (xoff, yoff, xoff + xsize, yoff + ysize + 100), 0, "RGB"
|
|
|
|
)
|
|
|
|
],
|
2022-02-25 08:07:01 +03:00
|
|
|
)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_encode(self) -> None:
|
2024-07-12 14:16:56 +03:00
|
|
|
encoder = ImageFile.PyEncoder("")
|
2022-02-25 08:07:01 +03:00
|
|
|
with pytest.raises(NotImplementedError):
|
2024-06-22 03:09:11 +03:00
|
|
|
encoder.encode(0)
|
2022-02-25 08:07:01 +03:00
|
|
|
|
|
|
|
bytes_consumed, errcode = encoder.encode_to_pyfd()
|
|
|
|
assert bytes_consumed == 0
|
|
|
|
assert ImageFile.ERRORS[errcode] == "bad configuration"
|
|
|
|
|
|
|
|
encoder._pushes_fd = True
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
encoder.encode_to_pyfd()
|
|
|
|
|
2024-07-08 13:09:45 +03:00
|
|
|
fh = BytesIO()
|
2022-02-25 08:07:01 +03:00
|
|
|
with pytest.raises(NotImplementedError):
|
2024-07-08 13:09:45 +03:00
|
|
|
encoder.encode_to_file(fh, 0)
|
2022-05-04 14:25:40 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_zero_height(self) -> None:
|
2022-05-04 14:25:40 +03:00
|
|
|
with pytest.raises(UnidentifiedImageError):
|
|
|
|
Image.open("Tests/images/zero_height.j2k")
|