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
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import DcxImagePlugin, Image
|
2014-07-17 02:20:19 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
from .helper import assert_image_equal, hopper, is_pypy
|
2014-07-17 02:20:19 +04:00
|
|
|
|
2014-09-04 14:24:53 +04:00
|
|
|
# Created with ImageMagick: convert hopper.ppm hopper.dcx
|
|
|
|
TEST_FILE = "Tests/images/hopper.dcx"
|
2014-07-17 02:20:19 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
2014-07-17 02:20:19 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
# Assert
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert isinstance(im, DcxImagePlugin.DcxImageFile)
|
|
|
|
orig = hopper()
|
|
|
|
assert_image_equal(im, orig)
|
2014-07-17 02:20:19 +04:00
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
@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()
|
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_closed_file() -> None:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(TEST_FILE)
|
|
|
|
im.load()
|
|
|
|
im.close()
|
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
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
|
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():
|
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:
|
2020-02-12 19:29:19 +03:00
|
|
|
im.load()
|
2014-07-17 02:20:19 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_invalid_file() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with open("Tests/images/flower.jpg", "rb") as fp:
|
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
DcxImagePlugin.DcxImageFile(fp)
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2017-09-06 06:19:33 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_tell() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
# Act
|
|
|
|
frame = im.tell()
|
2017-09-06 06:19:33 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
|
|
|
assert frame == 0
|
2015-06-18 17:49:18 +03:00
|
|
|
|
2014-07-17 02:20:19 +04: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(TEST_FILE) as im:
|
2024-09-09 15:34:46 +03:00
|
|
|
assert isinstance(im, DcxImagePlugin.DcxImageFile)
|
2020-02-12 19:29:19 +03:00
|
|
|
assert im.n_frames == 1
|
|
|
|
assert not im.is_animated
|
|
|
|
|
|
|
|
|
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:
|
2024-09-09 15:34:46 +03:00
|
|
|
assert isinstance(im, DcxImagePlugin.DcxImageFile)
|
2020-02-12 19:29:19 +03:00
|
|
|
n_frames = im.n_frames
|
|
|
|
|
|
|
|
# Test seeking past the last frame
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(n_frames)
|
|
|
|
assert im.tell() < n_frames
|
|
|
|
|
|
|
|
# Test that seeking to the last frame does not raise an error
|
|
|
|
im.seek(n_frames - 1)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_too_far() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
frame = 999 # too big on purpose
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(frame)
|