Pillow/Tests/test_file_spider.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

165 lines
3.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
import tempfile
import warnings
2019-09-27 22:58:17 +03:00
from io import BytesIO
from pathlib import Path
import pytest
from PIL import Image, ImageSequence, SpiderImagePlugin
2024-03-02 05:12:17 +03:00
from .helper import assert_image_equal, hopper, is_pypy
2017-07-29 03:20:19 +03:00
TEST_FILE = "Tests/images/hopper.spider"
def test_sanity() -> None:
2020-02-23 00:03:01 +03:00
with Image.open(TEST_FILE) as im:
im.load()
assert im.mode == "F"
assert im.size == (128, 128)
assert im.format == "SPIDER"
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file() -> None:
def open() -> None:
2020-02-23 00:03:01 +03:00
im = Image.open(TEST_FILE)
im.load()
2023-03-02 23:50:52 +03:00
with pytest.warns(ResourceWarning):
open()
2020-02-23 00:03:01 +03:00
def test_closed_file() -> None:
with warnings.catch_warnings():
2020-02-23 00:03:01 +03:00
im = Image.open(TEST_FILE)
im.load()
im.close()
def test_context_manager() -> None:
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:
im.load()
2019-06-13 18:54:11 +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
def test_save(tmp_path: Path) -> None:
2020-02-23 00:03:01 +03:00
# Arrange
temp = str(tmp_path / "temp.spider")
im = hopper()
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-23 00:03:01 +03:00
# Act
im.save(temp, "SPIDER")
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-23 00:03:01 +03:00
# Assert
with Image.open(temp) as im2:
assert im2.mode == "F"
assert im2.size == (128, 128)
assert im2.format == "SPIDER"
2018-11-17 13:56:06 +03:00
2014-05-26 22:00:20 +04:00
def test_tempfile() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
im = hopper()
# Act
with tempfile.TemporaryFile() as fp:
im.save(fp, "SPIDER")
2014-05-26 22:00:20 +04:00
2014-06-10 13:10:47 +04:00
# Assert
2020-02-23 00:03:01 +03:00
fp.seek(0)
with Image.open(fp) as reloaded:
assert reloaded.mode == "F"
assert reloaded.size == (128, 128)
assert reloaded.format == "SPIDER"
2014-05-26 22:00:20 +04:00
2020-02-23 00:03:01 +03:00
def test_is_spider_image() -> None:
2020-02-23 00:03:01 +03:00
assert SpiderImagePlugin.isSpiderImage(TEST_FILE)
def test_tell() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
with Image.open(TEST_FILE) as im:
2017-07-29 03:20:19 +03:00
# Act
2020-02-23 00:03:01 +03:00
index = im.tell()
# Assert
assert index == 0
2017-07-29 03:20:19 +03:00
def test_n_frames() -> None:
2020-02-23 00:03:01 +03:00
with Image.open(TEST_FILE) as im:
assert im.n_frames == 1
assert not im.is_animated
2014-07-15 13:23:02 +04:00
def test_load_image_series() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
not_spider_file = "Tests/images/hopper.ppm"
file_list = [TEST_FILE, not_spider_file, "path/not_found.ext"]
2014-07-15 13:23:02 +04:00
2020-02-23 00:03:01 +03:00
# Act
img_list = SpiderImagePlugin.loadImageSeries(file_list)
2014-07-15 13:23:02 +04:00
2020-02-23 00:03:01 +03:00
# Assert
assert len(img_list) == 1
assert isinstance(img_list[0], Image.Image)
assert img_list[0].size == (128, 128)
2015-06-07 18:01:34 +03:00
2014-07-15 13:23:02 +04:00
def test_load_image_series_no_input() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
file_list = None
2014-07-15 13:23:02 +04:00
2020-02-23 00:03:01 +03:00
# Act
img_list = SpiderImagePlugin.loadImageSeries(file_list)
2014-07-15 13:23:02 +04:00
2020-02-23 00:03:01 +03:00
# Assert
assert img_list is None
2014-07-15 13:23:02 +04:00
def test_is_int_not_a_number() -> None:
2020-02-23 00:03:01 +03:00
# Arrange
not_a_number = "a"
2014-07-15 13:23:02 +04:00
2020-02-23 00:03:01 +03:00
# Act
ret = SpiderImagePlugin.isInt(not_a_number)
2014-07-15 13:23:02 +04:00
2020-02-23 00:03:01 +03:00
# Assert
assert ret == 0
2014-07-15 13:23:02 +04:00
2014-05-26 22:00:20 +04:00
def test_invalid_file() -> None:
2020-02-23 00:03:01 +03:00
invalid_file = "Tests/images/invalid.spider"
2015-12-10 01:35:35 +03:00
with pytest.raises(OSError):
2021-02-11 13:43:54 +03:00
with Image.open(invalid_file):
pass
2016-04-04 11:48:21 +03:00
def test_nonstack_file() -> None:
2020-02-23 00:03:01 +03:00
with Image.open(TEST_FILE) as im:
with pytest.raises(EOFError):
im.seek(0)
def test_nonstack_dos() -> None:
2020-02-23 00:03:01 +03:00
with Image.open(TEST_FILE) as im:
for i, frame in enumerate(ImageSequence.Iterator(im)):
assert i <= 1, "Non-stack DOS file test failed"
2024-03-03 22:37:56 +03:00
# for issue #4093
def test_odd_size() -> None:
2020-02-23 00:03:01 +03:00
data = BytesIO()
width = 100
im = Image.new("F", (width, 64))
im.save(data, format="SPIDER")
data.seek(0)
2024-03-02 05:12:17 +03:00
with Image.open(data) as im2:
assert_image_equal(im, im2)