2019-07-06 23:40:53 +03:00
|
|
|
import tempfile
|
2019-09-27 22:58:17 +03:00
|
|
|
from io import BytesIO
|
2014-05-26 21:09:12 +04:00
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImageSequence, SpiderImagePlugin
|
2014-05-26 21:09:12 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
from .helper import assert_image_equal, hopper, is_pypy
|
2017-07-29 03:20:19 +03:00
|
|
|
|
2014-09-04 13:28:37 +04:00
|
|
|
TEST_FILE = "Tests/images/hopper.spider"
|
2014-05-26 21:09:12 +04:00
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_sanity():
|
|
|
|
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():
|
|
|
|
def open():
|
|
|
|
im = Image.open(TEST_FILE)
|
|
|
|
im.load()
|
|
|
|
|
|
|
|
pytest.warns(ResourceWarning, open)
|
|
|
|
|
|
|
|
|
|
|
|
def test_closed_file():
|
|
|
|
def open():
|
|
|
|
im = Image.open(TEST_FILE)
|
|
|
|
im.load()
|
|
|
|
im.close()
|
|
|
|
|
|
|
|
pytest.warns(None, open)
|
|
|
|
|
|
|
|
|
|
|
|
def test_context_manager():
|
|
|
|
def 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
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
im.load()
|
2019-06-13 18:54:11 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
pytest.warns(None, 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
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_save(tmp_path):
|
|
|
|
# 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
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_tempfile():
|
|
|
|
# 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():
|
|
|
|
assert SpiderImagePlugin.isSpiderImage(TEST_FILE)
|
|
|
|
|
|
|
|
|
|
|
|
def test_tell():
|
|
|
|
# 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
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_n_frames():
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_load_image_series():
|
|
|
|
# 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
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_load_image_series_no_input():
|
|
|
|
# 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
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_is_int_not_a_number():
|
|
|
|
# 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
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_invalid_file():
|
|
|
|
invalid_file = "Tests/images/invalid.spider"
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-23 00:03:01 +03:00
|
|
|
Image.open(invalid_file)
|
2015-11-17 17:18:01 +03:00
|
|
|
|
2016-04-04 11:48:21 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
def test_nonstack_file():
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
def test_nonstack_dos():
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
for i, frame in enumerate(ImageSequence.Iterator(im)):
|
|
|
|
assert i <= 1, "Non-stack DOS file test failed"
|
|
|
|
|
|
|
|
|
|
|
|
# for issue #4093
|
|
|
|
def test_odd_size():
|
|
|
|
data = BytesIO()
|
|
|
|
width = 100
|
|
|
|
im = Image.new("F", (width, 64))
|
|
|
|
im.save(data, format="SPIDER")
|
|
|
|
|
|
|
|
data.seek(0)
|
|
|
|
with Image.open(data) as im2:
|
|
|
|
assert_image_equal(im, im2)
|