2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2024-05-23 10:50:50 +03:00
|
|
|
from PIL import Image, PsdImagePlugin
|
2012-10-24 17:24:26 +04:00
|
|
|
|
2022-08-05 16:29:58 +03:00
|
|
|
from .helper import assert_image_equal_tofile, assert_image_similar, hopper, is_pypy
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/hopper.psd"
|
2012-10-24 17:24:26 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
im.load()
|
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert im.format == "PSD"
|
2020-07-16 18:26:42 +03:00
|
|
|
assert im.get_format_mimetype() == "image/vnd.adobe.photoshop"
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
im2 = hopper()
|
|
|
|
assert_image_similar(im, im2, 4.8)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_unclosed_file() -> None:
|
|
|
|
def open() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(test_file)
|
|
|
|
im.load()
|
2023-03-02 23:50:52 +03:00
|
|
|
|
|
|
|
with pytest.warns(ResourceWarning):
|
|
|
|
open()
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_closed_file() -> None:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2024-10-26 08:15:46 +03:00
|
|
|
warnings.simplefilter("error")
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(test_file)
|
|
|
|
im.load()
|
|
|
|
im.close()
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_context_manager() -> None:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2024-10-26 08:15:46 +03:00
|
|
|
warnings.simplefilter("error")
|
|
|
|
|
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(test_file) as im:
|
|
|
|
im.load()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2019-06-19 09:30:10 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_invalid_file() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
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
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
PsdImagePlugin.PsdImageFile(invalid_file)
|
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
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_n_frames() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/hopper_merged.psd") as im:
|
|
|
|
assert im.n_frames == 1
|
|
|
|
assert not im.is_animated
|
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
|
|
|
|
2021-07-15 12:38:26 +03:00
|
|
|
for path in [test_file, "Tests/images/negative_layer_count.psd"]:
|
|
|
|
with Image.open(path) as im:
|
|
|
|
assert im.n_frames == 2
|
|
|
|
assert im.is_animated
|
2019-03-31 16:02:22 +03:00
|
|
|
|
2015-07-03 09:22:56 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_eoferror() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
# PSD seek index starts at 1 rather than 0
|
|
|
|
n_frames = im.n_frames + 1
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test seeking past the last frame
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(n_frames)
|
|
|
|
assert im.tell() < n_frames
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test that seeking to the last frame does not raise an error
|
|
|
|
im.seek(n_frames - 1)
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2015-06-18 17:49:18 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_tell() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 1
|
2015-06-18 17:49:18 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(0)
|
2017-02-27 17:09:05 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im.seek(1)
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 1
|
2017-02-27 17:09:05 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im.seek(2)
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 2
|
2017-02-27 17:09:05 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_eoferror() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(-1)
|
2017-02-27 17:09:05 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_open_after_exclusive_load() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
im.load()
|
|
|
|
im.seek(im.tell() + 1)
|
|
|
|
im.load()
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rgba() -> None:
|
2022-08-05 16:29:58 +03:00
|
|
|
with Image.open("Tests/images/rgba.psd") as im:
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_square.png")
|
|
|
|
|
|
|
|
|
2024-01-10 15:53:45 +03:00
|
|
|
def test_negative_top_left_layer() -> None:
|
|
|
|
with Image.open("Tests/images/negative_top_left_layer.psd") as im:
|
|
|
|
assert im.layers[0][2] == (-50, -50, 50, 50)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_layer_skip() -> None:
|
2023-10-20 08:09:13 +03:00
|
|
|
with Image.open("Tests/images/five_channels.psd") as im:
|
|
|
|
assert im.n_frames == 1
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_icc_profile() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert "icc_profile" in im.info
|
|
|
|
|
|
|
|
icc_profile = im.info["icc_profile"]
|
|
|
|
assert len(icc_profile) == 3144
|
2019-03-31 14:24:10 +03:00
|
|
|
|
2017-02-27 18:21:27 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_no_icc_profile() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/hopper_merged.psd") as im:
|
|
|
|
assert "icc_profile" not in im.info
|
2017-02-27 18:21:27 +03:00
|
|
|
|
2019-09-29 07:16:30 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_combined_larger_than_size() -> None:
|
2022-01-24 03:06:41 +03:00
|
|
|
# The combined size of the individual parts is larger than the
|
2020-02-12 19:29:19 +03:00
|
|
|
# declared 'size' of the extra data field, resulting in a backwards seek.
|
2019-09-29 07:16:30 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# If we instead take the 'size' of the extra data field as the source of truth,
|
|
|
|
# then the seek can't be negative
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2021-02-11 13:43:54 +03:00
|
|
|
with Image.open("Tests/images/combined_larger_than_size.psd"):
|
|
|
|
pass
|
2021-03-07 21:04:25 +03:00
|
|
|
|
2021-04-01 17:41:46 +03:00
|
|
|
|
2021-03-07 21:04:25 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_file,raises",
|
|
|
|
[
|
|
|
|
("Tests/images/timeout-c8efc3fded6426986ba867a399791bae544f59bc.psd", OSError),
|
|
|
|
("Tests/images/timeout-dedc7a4ebd856d79b4359bbcc79e8ef231ce38f6.psd", OSError),
|
|
|
|
],
|
|
|
|
)
|
2024-06-23 23:59:00 +03:00
|
|
|
def test_crashes(test_file: str, raises: type[Exception]) -> None:
|
2021-03-07 21:04:25 +03:00
|
|
|
with open(test_file, "rb") as f:
|
|
|
|
with pytest.raises(raises):
|
|
|
|
with Image.open(f):
|
|
|
|
pass
|
2024-05-23 10:50:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_file",
|
|
|
|
[
|
|
|
|
"Tests/images/timeout-1ee28a249896e05b83840ae8140622de8e648ba9.psd",
|
|
|
|
"Tests/images/timeout-598843abc37fc080ec36a2699ebbd44f795d3a6f.psd",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_layer_crashes(test_file: str) -> None:
|
|
|
|
with open(test_file, "rb") as f:
|
|
|
|
with Image.open(f) as im:
|
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
im.layers
|