2014-01-12 00:30:09 +04:00
|
|
|
import io
|
2019-11-21 05:42:52 +03:00
|
|
|
import unittest
|
2013-11-22 02:32:18 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-02-18 01:03:32 +03:00
|
|
|
from PIL import EpsImagePlugin, Image, features
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
from .helper import PillowTestCase, assert_image_similar, hopper, skip_unless_feature
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
HAS_GHOSTSCRIPT = EpsImagePlugin.has_ghostscript()
|
|
|
|
|
2014-05-08 12:43:04 +04:00
|
|
|
# Our two EPS test files (they are identical except for their bounding boxes)
|
2013-11-20 10:43:10 +04:00
|
|
|
file1 = "Tests/images/zero_bb.eps"
|
|
|
|
file2 = "Tests/images/non_zero_bb.eps"
|
|
|
|
|
2014-05-08 12:43:04 +04:00
|
|
|
# Due to palletization, we'll need to convert these to RGB after load
|
2013-11-20 10:43:10 +04:00
|
|
|
file1_compare = "Tests/images/zero_bb.png"
|
|
|
|
file1_compare_scale2 = "Tests/images/zero_bb_scale2.png"
|
|
|
|
|
|
|
|
file2_compare = "Tests/images/non_zero_bb.png"
|
|
|
|
file2_compare_scale2 = "Tests/images/non_zero_bb_scale2.png"
|
|
|
|
|
2014-05-15 01:14:55 +04:00
|
|
|
# EPS test files with binary preview
|
|
|
|
file3 = "Tests/images/binary_preview_map.eps"
|
2014-05-08 12:43:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
class TestFileEps(PillowTestCase):
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
|
|
|
# Regular scale
|
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(file1) as image1:
|
|
|
|
image1.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image1.mode == "RGB"
|
|
|
|
assert image1.size == (460, 352)
|
|
|
|
assert image1.format == "EPS"
|
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(file2) as image2:
|
|
|
|
image2.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image2.mode == "RGB"
|
|
|
|
assert image2.size == (360, 252)
|
|
|
|
assert image2.format == "EPS"
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# Double scale
|
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(file1) as image1_scale2:
|
|
|
|
image1_scale2.load(scale=2)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image1_scale2.mode == "RGB"
|
|
|
|
assert image1_scale2.size == (920, 704)
|
|
|
|
assert image1_scale2.format == "EPS"
|
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(file2) as image2_scale2:
|
|
|
|
image2_scale2.load(scale=2)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image2_scale2.mode == "RGB"
|
|
|
|
assert image2_scale2.size == (720, 504)
|
|
|
|
assert image2_scale2.format == "EPS"
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
def test_invalid_file(self):
|
2015-07-03 09:22:56 +03:00
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
EpsImagePlugin.EpsImageFile(invalid_file)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2016-04-13 11:27:46 +03:00
|
|
|
def test_cmyk(self):
|
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("Tests/images/pil_sample_cmyk.eps") as cmyk_image:
|
2016-04-19 18:20:15 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert cmyk_image.mode == "CMYK"
|
|
|
|
assert cmyk_image.size == (100, 100)
|
|
|
|
assert cmyk_image.format == "EPS"
|
2016-07-04 02:50:05 +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
|
|
|
cmyk_image.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert cmyk_image.mode == "RGB"
|
2016-04-19 18:20:15 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
if features.check("jpg"):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/pil_sample_rgb.jpg") as target:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(cmyk_image, target, 10)
|
2016-04-19 18:20:15 +03:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2017-07-19 22:54:16 +03:00
|
|
|
def test_showpage(self):
|
2017-09-09 12:52:06 +03:00
|
|
|
# See https://github.com/python-pillow/Pillow/issues/2615
|
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("Tests/images/reqd_showpage.eps") as plot_image:
|
|
|
|
with Image.open("Tests/images/reqd_showpage.png") as target:
|
|
|
|
# should not crash/hang
|
|
|
|
plot_image.load()
|
|
|
|
# fonts could be slightly different
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(plot_image, target, 6)
|
2017-07-19 22:54:16 +03:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_file_object(self):
|
|
|
|
# issue 479
|
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(file1) as image1:
|
|
|
|
with open(self.tempfile("temp_file.eps"), "wb") as fh:
|
|
|
|
image1.save(fh, "EPS")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_iobase_object(self):
|
|
|
|
# issue 479
|
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(file1) as image1:
|
2019-11-01 14:22:56 +03:00
|
|
|
with open(self.tempfile("temp_iobase.eps"), "wb") as fh:
|
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
|
|
|
image1.save(fh, "EPS")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2014-08-05 02:27:08 +04:00
|
|
|
def test_bytesio_object(self):
|
2019-06-13 18:53:42 +03:00
|
|
|
with open(file1, "rb") as f:
|
2014-08-05 02:27:08 +04:00
|
|
|
img_bytes = io.BytesIO(f.read())
|
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(img_bytes) as img:
|
|
|
|
img.load()
|
2014-08-05 02:27:08 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(file1_compare) as image1_scale1_compare:
|
|
|
|
image1_scale1_compare = image1_scale1_compare.convert("RGB")
|
|
|
|
image1_scale1_compare.load()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(img, image1_scale1_compare, 5)
|
2014-10-01 17:50:33 +04:00
|
|
|
|
2017-03-01 12:20:18 +03:00
|
|
|
def test_image_mode_not_supported(self):
|
|
|
|
im = hopper("RGBA")
|
2019-06-13 18:53:42 +03:00
|
|
|
tmpfile = self.tempfile("temp.eps")
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.save(tmpfile)
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_render_scale1(self):
|
|
|
|
# We need png support for these render test
|
|
|
|
|
|
|
|
# Zero bounding box
|
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(file1) as image1_scale1:
|
|
|
|
image1_scale1.load()
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(file1_compare) as image1_scale1_compare:
|
|
|
|
image1_scale1_compare = image1_scale1_compare.convert("RGB")
|
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
|
|
|
image1_scale1_compare.load()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(image1_scale1, image1_scale1_compare, 5)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# Non-Zero bounding box
|
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(file2) as image2_scale1:
|
|
|
|
image2_scale1.load()
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(file2_compare) as image2_scale1_compare:
|
|
|
|
image2_scale1_compare = image2_scale1_compare.convert("RGB")
|
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
|
|
|
image2_scale1_compare.load()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(image2_scale1, image2_scale1_compare, 10)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_render_scale2(self):
|
|
|
|
# We need png support for these render test
|
|
|
|
|
|
|
|
# Zero bounding box
|
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(file1) as image1_scale2:
|
|
|
|
image1_scale2.load(scale=2)
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(file1_compare_scale2) as image1_scale2_compare:
|
|
|
|
image1_scale2_compare = image1_scale2_compare.convert("RGB")
|
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
|
|
|
image1_scale2_compare.load()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(image1_scale2, image1_scale2_compare, 5)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# Non-Zero bounding box
|
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(file2) as image2_scale2:
|
|
|
|
image2_scale2.load(scale=2)
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(file2_compare_scale2) as image2_scale2_compare:
|
|
|
|
image2_scale2_compare = image2_scale2_compare.convert("RGB")
|
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
|
|
|
image2_scale2_compare.load()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(image2_scale2, image2_scale2_compare, 10)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_resize(self):
|
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
|
|
|
files = [file1, file2, "Tests/images/illu10_preview.eps"]
|
|
|
|
for fn in files:
|
|
|
|
with Image.open(fn) as im:
|
|
|
|
new_size = (100, 100)
|
|
|
|
im = im.resize(new_size)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == new_size
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_thumbnail(self):
|
|
|
|
# Issue #619
|
|
|
|
# Arrange
|
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
|
|
|
files = [file1, file2]
|
|
|
|
for fn in files:
|
|
|
|
with Image.open(file1) as im:
|
|
|
|
new_size = (100, 100)
|
|
|
|
im.thumbnail(new_size)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert max(im.size) == max(new_size)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_read_binary_preview(self):
|
|
|
|
# Issue 302
|
|
|
|
# open image with binary preview
|
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(file3):
|
|
|
|
pass
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
def _test_readline(self, t, ending):
|
2019-06-13 18:53:42 +03:00
|
|
|
ending = "Failure with line ending: %s" % (
|
|
|
|
"".join("%s" % ord(s) for s in ending)
|
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert t.readline().strip("\r\n") == "something", ending
|
|
|
|
assert t.readline().strip("\r\n") == "else", ending
|
|
|
|
assert t.readline().strip("\r\n") == "baz", ending
|
|
|
|
assert t.readline().strip("\r\n") == "bif", ending
|
2014-09-03 09:47:05 +04:00
|
|
|
|
2016-11-01 16:16:44 +03:00
|
|
|
def _test_readline_io_psfile(self, test_string, ending):
|
2019-06-13 18:53:42 +03:00
|
|
|
f = io.BytesIO(test_string.encode("latin-1"))
|
2016-11-01 16:16:44 +03:00
|
|
|
t = EpsImagePlugin.PSFile(f)
|
2014-09-03 09:47:05 +04:00
|
|
|
self._test_readline(t, ending)
|
2014-10-01 17:50:33 +04:00
|
|
|
|
2014-09-03 09:47:05 +04:00
|
|
|
def _test_readline_file_psfile(self, test_string, ending):
|
2019-06-13 18:53:42 +03:00
|
|
|
f = self.tempfile("temp.txt")
|
|
|
|
with open(f, "wb") as w:
|
|
|
|
w.write(test_string.encode("latin-1"))
|
2014-09-03 09:47:05 +04:00
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
with open(f, "rb") as r:
|
2014-09-03 09:47:05 +04:00
|
|
|
t = EpsImagePlugin.PSFile(r)
|
|
|
|
self._test_readline(t, ending)
|
2014-10-01 17:50:33 +04:00
|
|
|
|
2014-09-03 09:47:05 +04:00
|
|
|
def test_readline(self):
|
|
|
|
# check all the freaking line endings possible from the spec
|
2015-04-24 02:26:52 +03:00
|
|
|
# test_string = u'something\r\nelse\n\rbaz\rbif\n'
|
2019-06-13 18:53:42 +03:00
|
|
|
line_endings = ["\r\n", "\n", "\n\r", "\r"]
|
|
|
|
strings = ["something", "else", "baz", "bif"]
|
2014-09-03 09:47:05 +04:00
|
|
|
|
|
|
|
for ending in line_endings:
|
|
|
|
s = ending.join(strings)
|
2016-11-01 16:16:44 +03:00
|
|
|
self._test_readline_io_psfile(s, ending)
|
2014-09-03 09:47:05 +04:00
|
|
|
self._test_readline_file_psfile(s, ending)
|
2014-10-01 17:50:33 +04:00
|
|
|
|
2015-03-26 15:05:17 +03:00
|
|
|
def test_open_eps(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1104
|
|
|
|
# Arrange
|
2019-06-13 18:53:42 +03:00
|
|
|
FILES = [
|
|
|
|
"Tests/images/illu10_no_preview.eps",
|
|
|
|
"Tests/images/illu10_preview.eps",
|
|
|
|
"Tests/images/illuCS6_no_preview.eps",
|
|
|
|
"Tests/images/illuCS6_preview.eps",
|
|
|
|
]
|
2015-03-26 15:05:17 +03:00
|
|
|
|
2018-10-02 11:37:10 +03:00
|
|
|
# Act / Assert
|
2015-03-26 15:05:17 +03:00
|
|
|
for filename in FILES:
|
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(filename) as img:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert img.mode == "RGB"
|
2015-03-26 15:05:17 +03:00
|
|
|
|
2018-12-30 12:50:09 +03:00
|
|
|
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
|
2017-12-22 09:01:54 +03:00
|
|
|
def test_emptyline(self):
|
|
|
|
# Test file includes an empty line in the header data
|
|
|
|
emptyline_file = "Tests/images/zero_bb_emptyline.eps"
|
|
|
|
|
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(emptyline_file) as image:
|
|
|
|
image.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image.mode == "RGB"
|
|
|
|
assert image.size == (460, 352)
|
|
|
|
assert image.format == "EPS"
|