2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
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
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import FliImagePlugin, Image
|
2012-10-24 17:25:00 +04:00
|
|
|
|
2022-09-29 14:29:16 +03:00
|
|
|
from .helper import assert_image_equal, assert_image_equal_tofile, is_pypy
|
2012-10-24 17:25:00 +04:00
|
|
|
|
2014-09-29 21:40:33 +04:00
|
|
|
# created as an export of a palette image from Gimp2.6
|
|
|
|
# save as...-> hopper.fli, default options.
|
2017-08-04 14:41:28 +03:00
|
|
|
static_test_file = "Tests/images/hopper.fli"
|
|
|
|
|
|
|
|
# From https://samples.libav.org/fli-flc/
|
|
|
|
animated_test_file = "Tests/images/a.fli"
|
2012-10-24 17:25:00 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_sanity():
|
|
|
|
with Image.open(static_test_file) as im:
|
|
|
|
im.load()
|
|
|
|
assert im.mode == "P"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert im.format == "FLI"
|
|
|
|
assert not im.is_animated
|
|
|
|
|
|
|
|
with Image.open(animated_test_file) as im:
|
|
|
|
assert im.mode == "P"
|
|
|
|
assert im.size == (320, 200)
|
|
|
|
assert im.format == "FLI"
|
|
|
|
assert im.info["duration"] == 71
|
|
|
|
assert im.is_animated
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
|
|
|
def test_unclosed_file():
|
2023-03-02 23:50:52 +03:00
|
|
|
def open():
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(static_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
|
|
|
|
|
|
|
|
|
|
|
def test_closed_file():
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(static_test_file)
|
|
|
|
im.load()
|
|
|
|
im.close()
|
|
|
|
|
|
|
|
|
2022-04-17 05:14:53 +03:00
|
|
|
def test_seek_after_close():
|
|
|
|
im = Image.open(animated_test_file)
|
|
|
|
im.seek(1)
|
|
|
|
im.close()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.seek(0)
|
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_context_manager():
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
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(static_test_file) as im:
|
|
|
|
im.load()
|
2019-06-13 18:53:42 +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
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_tell():
|
|
|
|
# Arrange
|
|
|
|
with Image.open(static_test_file) as im:
|
|
|
|
# Act
|
|
|
|
frame = im.tell()
|
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
|
|
|
# Assert
|
|
|
|
assert frame == 0
|
2018-11-17 13:56:06 +03:00
|
|
|
|
2017-08-12 12:07:06 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_invalid_file():
|
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
2017-08-12 12:07:06 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
FliImagePlugin.FliImageFile(invalid_file)
|
2017-08-12 12:07:06 +03:00
|
|
|
|
2015-07-03 09:22:56 +03:00
|
|
|
|
2022-09-29 14:29:16 +03:00
|
|
|
def test_palette_chunk_second():
|
|
|
|
with Image.open("Tests/images/hopper_palette_chunk_second.fli") as im:
|
|
|
|
with Image.open(static_test_file) as expected:
|
|
|
|
assert_image_equal(im.convert("RGB"), expected.convert("RGB"))
|
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_n_frames():
|
|
|
|
with Image.open(static_test_file) as im:
|
|
|
|
assert im.n_frames == 1
|
|
|
|
assert not im.is_animated
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(animated_test_file) as im:
|
|
|
|
assert im.n_frames == 384
|
|
|
|
assert im.is_animated
|
|
|
|
|
|
|
|
|
|
|
|
def test_eoferror():
|
|
|
|
with Image.open(animated_test_file) as im:
|
|
|
|
n_frames = im.n_frames
|
2015-06-18 17:49:18 +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
|
2017-08-04 14:41:28 +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)
|
2017-08-12 12:07:06 +03:00
|
|
|
|
2017-09-06 06:19:33 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_seek_tell():
|
|
|
|
with Image.open(animated_test_file) as im:
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 0
|
2017-08-04 14:41:28 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im.seek(0)
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 0
|
2017-08-04 14:41:28 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im.seek(1)
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 1
|
2017-08-04 14:41:28 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im.seek(2)
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 2
|
2017-08-04 14:41:28 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
im.seek(1)
|
|
|
|
layer_number = im.tell()
|
|
|
|
assert layer_number == 1
|
2017-08-04 14:41:28 +03:00
|
|
|
|
2017-09-06 06:19:33 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_seek():
|
|
|
|
with Image.open(animated_test_file) as im:
|
|
|
|
im.seek(50)
|
2018-11-26 10:52:51 +03:00
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/a_fli.png")
|
2021-03-12 00:12:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_file",
|
|
|
|
[
|
|
|
|
"Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli",
|
|
|
|
"Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.timeout(timeout=3)
|
|
|
|
def test_timeouts(test_file):
|
|
|
|
with open(test_file, "rb") as f:
|
|
|
|
with Image.open(f) as im:
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.load()
|
2021-08-08 15:04:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_file",
|
|
|
|
[
|
|
|
|
"Tests/images/crash-5762152299364352.fli",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_crash(test_file):
|
|
|
|
with open(test_file, "rb") as f:
|
|
|
|
with Image.open(f) as im:
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.load()
|