Pillow/Tests/test_file_im.py

111 lines
2.3 KiB
Python
Raw Normal View History

2020-02-26 12:15:23 +03:00
import filecmp
import pytest
2015-07-03 08:03:25 +03:00
from PIL import Image, ImImagePlugin
2015-06-07 18:01:34 +03:00
from .helper import assert_image_equal_tofile, hopper, is_pypy
2015-06-07 18:01:34 +03:00
# sample im
TEST_IM = "Tests/images/hopper.im"
2020-02-17 01:03:27 +03:00
def test_sanity():
with Image.open(TEST_IM) as im:
im.load()
assert im.mode == "RGB"
assert im.size == (128, 128)
assert im.format == "IM"
2020-02-26 12:15:23 +03:00
def test_name_limit(tmp_path):
out = str(tmp_path / ("name_limit_test" * 7 + ".im"))
with Image.open(TEST_IM) as im:
im.save(out)
assert filecmp.cmp(out, "Tests/images/hopper_long_name.im")
2020-02-17 01:03:27 +03:00
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file():
def open():
im = Image.open(TEST_IM)
im.load()
pytest.warns(ResourceWarning, open)
def test_closed_file():
with pytest.warns(None) as record:
2020-02-17 01:03:27 +03:00
im = Image.open(TEST_IM)
im.load()
im.close()
assert not record
2020-02-17 01:03:27 +03:00
def test_context_manager():
with pytest.warns(None) as record:
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_IM) as im:
im.load()
2019-06-13 18:54:11 +03:00
assert not record
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-17 01:03:27 +03:00
def test_tell():
# Arrange
with Image.open(TEST_IM) as im:
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-17 01:03:27 +03:00
# 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-17 01:03:27 +03:00
# Assert
assert frame == 0
2018-11-17 13:56:06 +03:00
2017-08-12 08:54:54 +03:00
2020-02-17 01:03:27 +03:00
def test_n_frames():
with Image.open(TEST_IM) as im:
assert im.n_frames == 1
assert not im.is_animated
2017-08-12 08:54:54 +03:00
2020-02-17 01:03:27 +03:00
def test_eoferror():
with Image.open(TEST_IM) as im:
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)
2015-06-07 18:01:34 +03:00
2017-09-06 06:19:33 +03:00
2020-02-17 01:03:27 +03:00
def test_roundtrip(tmp_path):
2020-02-19 12:56:23 +03:00
def roundtrip(mode):
2020-02-17 01:03:27 +03:00
out = str(tmp_path / "temp.im")
im = hopper(mode)
im.save(out)
assert_image_equal_tofile(im, out)
2017-09-06 06:19:33 +03:00
2020-02-19 12:56:23 +03:00
for mode in ["RGB", "P", "PA"]:
roundtrip(mode)
2020-02-17 01:03:27 +03:00
def test_save_unsupported_mode(tmp_path):
out = str(tmp_path / "temp.im")
im = hopper("HSV")
with pytest.raises(ValueError):
im.save(out)
2017-09-01 13:36:51 +03:00
2015-06-07 18:01:34 +03:00
2020-02-17 01:03:27 +03:00
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
2015-06-07 18:01:34 +03:00
2020-02-17 01:03:27 +03:00
with pytest.raises(SyntaxError):
ImImagePlugin.ImImageFile(invalid_file)
2015-07-03 09:22:56 +03:00
2015-07-03 08:03:25 +03:00
2020-02-17 01:03:27 +03:00
def test_number():
assert ImImagePlugin.number("1.2") == 1.2