Commit Graph

155 Commits

Author SHA1 Message Date
Jon Dufresne
4cd4adddc3 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-10-12 08:27:17 -07:00
nulano
cf1f8b0498 Tests.helper cleanup 2019-09-25 11:58:02 +02:00
Hugo van Kemenade
8e3c4e3658
Merge pull request #3965 from radarhere/truncated
Raise the same error if a truncated image is loaded a second time
2019-09-20 22:53:52 +03:00
Andrew Murray
023dcf27c2 Raise the same error if a truncated image is loaded a second time 2019-07-13 08:37:17 +10:00
Jon Dufresne
d50445ff30 Introduce isort to automate import ordering and formatting
Similar to the recent adoption of Black. isort is a Python utility to
sort imports alphabetically and automatically separate into sections. By
using isort, contributors can quickly and automatically conform to the
projects style without thinking. Just let the tool do it.

Uses the configuration recommended by the Black to avoid conflicts of
style.

Rewrite TestImageQt.test_deprecated to no rely on import order.
2019-07-06 16:11:35 -07:00
Hugo
f87821e010 Format with Black 2019-06-13 18:54:11 +03:00
Andrew Murray
2274c2afc7 Fixed reading APP13 marker without Photoshop data 2019-04-05 20:02:45 +11:00
Andrew Murray
c96cdb5e77 Consistent DPI rounding 2019-03-30 15:03:57 +11:00
Andrew Murray
636ad68f5e Read Photoshop resolution data 2019-03-06 10:28:45 +11:00
Hugo
dc3c64a0e3
Merge pull request #3631 from jdufresne/test-main
Remove unnecessary unittest.main() boilerplate from test files
2019-02-12 18:12:29 +02:00
Hugo
4ccdbf25cf
Merge pull request #3584 from radarhere/ifd_offset
Seek to IFD offset
2019-02-12 18:10:25 +02:00
Jon Dufresne
4de5477b61 Remove unnecessary unittest.main() boilerplate from test files
With the introduction and use of pytest, it is simple and easy to
execute specific tests in isolation through documented command line
arguments. Either by specifying the module path or through the `-k
EXPRESSION` argument. There is no longer any need to provide the
boilerplate:

    if __name__ == '__main__':
        unittest.main()

To every test file. It is simply noise.

The pattern remains in test files that aren't named with `test_*` as
those files are not discovered and executed by pytest by default.
2019-02-03 10:10:16 -08:00
Andrew Murray
0481d0ac1e Seek to IFD offset 2019-01-16 06:55:37 +11:00
Jon Dufresne
7da17ad41e Improve pytest configuration to allow specific tests as CLI args
The previous test configuration made it difficult to run a single test
with the pytest CLI. There were two major issues:

- The Tests directory was not a package. It now includes a __init__.py
  file and imports from other tests modules are done with relative
  imports.

- setup.cfg always specified the Tests directory. So even if a specific
  test were specified as a CLI arg, this configuration would also always
  include all tests. This configuration has been removed to allow
  specifying a single test on the command line.

Contributors can now run specific tests with a single command such as:

  $ tox -e py37 -- Tests/test_file_pdf.py::TestFilePdf.test_rgb

This makes it easy and faster to iterate on a single test failure and is
very familiar to those that have previously used tox and pytest.

When running tox or pytest with no arguments, they still discover and
runs all tests in the Tests directory.
2019-01-13 09:00:12 -08:00
Jon Dufresne
875e8c4bda Avoid catching unexpected exceptions in tests
Instead, allow exceptions to bubble up to the unittest exception
handler.

Prevents replacing the exception trace with a less informative
message. As the exceptions are always unexpected, should not need to
catch them explicitly in tests.
2018-09-04 19:43:20 -07:00
Andrew Murray
aeab86c005 Too many blank lines 2018-07-02 19:21:44 +10:00
Andrew Murray
6793b5bbd5 Added ImageFile get_format_mimetype method 2018-06-30 21:08:41 +10:00
Andrew Murray
9a3d554c1d Changed Exception tests to be more specific 2018-06-12 04:41:31 +10:00
Hugo
ecc4c7fecc Remove unittest regex deprecation warnings 2018-04-14 21:54:40 +03:00
wiredfool
d173e81798
Merge pull request #3023 from kkopachev/issue-3022
Certain corrupted jpegs can result in no data read
2018-03-21 07:55:17 +00:00
Konstantin Kopachev
5269bbc268
Add truncated jpeg tests 2018-03-07 21:31:51 -08:00
Andrew Murray
c13ccf13a1 Added missing whitespace 2018-03-06 19:53:07 +11:00
Andrew Murray
cca94054aa Changed assert statements to unittest calls 2018-01-17 22:01:37 +11:00
Andrew Murray
30b2ddd070 Removed duplicate imports 2018-01-04 15:40:46 +11:00
wiredfool
2611b2caa5 Merge pull request #2689 from radarhere/tests
Added tests
2017-09-04 11:15:28 +01:00
Andrew Murray
9e843a2d9b Changed format of lambda calls 2017-09-01 21:05:40 +10:00
Alexander
4d56db3a34 Fix wrong formula for subsampling=2 while JPEG saving 2017-08-27 20:03:36 +03:00
Vytis Banaitis
7c8e0e4457 Fix ZeroDivisionError when EXIF contains invalid DPI (0/0). 2017-08-09 16:16:14 +03:00
wiredfool
908fffdd2c Test for https://github.com/python-pillow/Pillow/issues/2628#issuecomment-315962612 2017-07-18 01:06:54 -07:00
Fran Barba
29c622dd3d Fixed typo biger > bigger 2017-06-29 11:21:19 +02:00
Fran Barba
a7d75c4b21 Applied Hugovk recommendations 2017-06-28 12:12:44 +02:00
Fran Barba
ebaa509d66 Adjust buffer size when icc_profile > MAXBLOCK (issue #148)
We were encountering some errors when saving specific JPEG images.
The error was shown in stderr as:
  IOError: encoder error -2 when writing image file

And on stdout it printed:
    Suspension not allowed here

The problem was the bufsize not contemplating the icc_profile block.
2017-06-28 12:11:33 +02:00
wiredfool
c3e041e9e6 Merge branch 'master' into rm-deprecated-fn 2017-06-13 13:32:38 +01:00
wiredfool
c16ba722f2 Merge pull request #2551 from jdufresne/keys
Remove unnecessary calls to dict.keys()
2017-06-13 13:12:48 +01:00
Andrew Murray
e8cba27bb1 Changed assert checks to more specific versions 2017-06-03 14:04:54 +10:00
Jon Dufresne
d244fbb299 Remove unnecessary calls to dict.keys()
iter(dict) is equivalent to iter(dict.keys()), so simply act on the dict
instead of adding the extra call.

Inspired by Lennart Regebro's PyCon 2017 presentation "Prehistoric
Patterns in Python". Available at:

https://www.youtube.com/watch?v=V5-JH23Vk0I
2017-05-28 09:55:38 -07:00
hugovk
e4d6223c94 Remove deprecated code due for removal in Pillow 4.2 (per PR #2010) 2017-05-27 23:20:03 +03:00
Hugo
a4bfe1a1d5 Test file with DPI in EXIF but not metadata, and XResolution is an int rather than tuple 2017-04-11 13:53:01 +03:00
wiredfool
0834e9491e Merge pull request #2330 from wiredfool/pr_2032
Close files after loading when possible.
2017-04-03 23:32:06 +01:00
Hugo
53df62647a DPI is a tuple (#2472)
* DPI is a tuple

* Some EXIF only contains an X resolution for DPI

* Refactor

* Test with no DPI in EXIF

* Handle EXIF with no DPI

* Created with: exiftool "-*resolution*"= photoshop-200dpi.jpg

* Test when not in EXIF, DPI==72,72

* Use X resolution for Y, default to 72,72 dpi

* Created with: exiftool -exif:ResolutionUnit=cm photoshop-200dpi.jpg

* Test for EXIF with dpcm instead of dpi

* Convert dpcm to dpi, and default to inches if unit unknown
2017-04-03 23:28:33 +01:00
wiredfool
db338ca19a open file check for windows, on jpeg where we don't mmap the file 2017-04-03 08:06:59 -07:00
Hugo
92acfca46f Test getting DPI from EXIF 2017-03-14 11:26:11 +02:00
Andrew Murray
798fde57ca Removed unused imports 2017-03-03 21:38:19 +11:00
wiredfool
7e2bd28a7c Fix for issue 2272, CMYK images miss the heuristic for the maxblock buffer scaling 2016-12-03 14:45:45 +00:00
wiredfool
4b57345683 Refactor random image 2016-12-03 14:45:05 +00:00
Tim Graham
0c66b80a95 Fix "invalid escape sequence" warning in Python 3.6
http://bugs.python.org/issue27364
2016-09-27 19:26:57 -04:00
Andrew Murray
d3d29ab2e3 Use truth value when checking for optimize option on save 2016-09-25 21:20:54 +10:00
Andrew Murray
de6158f5f1 Read value when checking for JPEG progressive key 2016-09-23 20:32:21 +10:00
Andrew Murray
051a410859 Convert DPI to ints when saving as JPEG 2016-09-22 19:41:32 +10:00
wiredfool
e980ca7896 Merge pull request #2010 from uploadcare/jpeg-raise-on-alpha
Show warning when trying to save RGBA image as JPEG
2016-08-25 11:41:49 +01:00
homm
193c756139 return implicit RGBA to JPEG save, raise warning 2016-08-09 03:11:35 +03:00
homm
1ba4e9e5ba remove end of file banner from all files 2016-07-10 14:11:28 +03:00
homm
c1da18e0ad do not allow to save images discarding alpha channel 2016-07-03 05:40:34 +03:00
Konstantin Kopachev
c8f93c979c
Skip extra 0xff00 in jpeg 2016-06-22 13:36:23 -07:00
Stephen Arthur
7872501c5b Added test cases to exhibit issues with custom qtables 2016-05-22 17:54:03 -07:00
Stephen Arthur
5025bdd487 Valid range for baseline jpeg qtables 1 to 255, fixed overflow from using signed char instead of unsigned char. added test 2016-04-07 09:35:14 -07:00
Stephen Arthur
b38c32ed70 Valid range for baseline jpeg qtables 0 to 255, fixed issue from using signed char instead of unsigned char. added test 2016-04-06 18:47:51 -07:00
Andrew Murray
677b958a7f Health fixes 2016-04-01 21:49:30 +11:00
wiredfool
a6c1331fdd Rolling back exif support to pre-3.0 format 2016-01-01 04:14:24 -08:00
Andrew Murray
a83bcec169 Flake8 fixes 2015-12-31 07:27:27 +11:00
wiredfool
253bc038c5 Python 3.2, sigh 2015-12-30 00:45:48 +00:00
wiredfool
31428b7009 Py3 fixes 2015-12-29 23:56:47 +00:00
hugovk
d9c90bba91 Test passes for 2.9.0, fails for 3.0.0 2015-12-29 22:57:36 +00:00
homm
0f87b1f125 suppress and check warning during tests 2015-09-15 04:07:02 +03:00
wiredfool
07d95c3861 Added test for typeerror test for gpsexif 2015-09-10 05:32:11 -07:00
Andrew Murray
3a25b1cd9b Added test for _getexif TypeError 2015-07-29 23:38:26 +10:00
Andrew Murray
2a3a34d2ff Treat MPO with unknown header as base JPEG file 2015-07-19 22:56:04 +10:00
Andrew Murray
309ab1fc3d Various Flake8 fixes 2015-07-03 16:22:56 +10:00
Andrew Murray
ee34d6843b Further health fixes 2015-04-24 18:24:52 +10:00
Andrew Murray
d1c182cadc Various Flake8 fixes 2015-04-24 09:26:52 +10:00
wiredfool
1e040feb1b Adjust buffer size when quality=keep, fixes #148 (again) 2015-01-18 10:56:29 -08:00
Christoph Gohlke
2538fa9e0f Fix AssertionError in TestFileJpeg.test_save_cjpeg 2014-09-29 11:35:46 -07:00
wiredfool
306ad74324 qtables error handling tests 2014-09-24 15:46:16 -07:00
wiredfool
1f377c2ace Merge from master 2014-09-23 09:52:03 -07:00
hugovk
1de128d6b6 Merge branch 'master' into flake8 2014-09-14 10:23:51 +03:00
etienne
416d8e340e Fixed issue #857.
When saving a JPEG and specifying 'keep' for quality or subsampling,
if the source JPEG image is in grayscale mode, don't try to find the
subsampling of the source, because grayscale images don't have any
subsampling (it's only for color components).

For the moment the fix also ignores subsampling of CMYK JPEG because
currently Pillow doesn't support encoding JPEG in YCCK mode (and
subsampling doesn't make sense in CMYK, but Pillow permits saving CMYK
JPEG with subsampling, that's a bug). This fix pass those errors
silently, i.e. it doesn't raise an error when 'keep' is used but it's
not possible to keep the subsampling (because the image is grayscale
or CMYK). I think it's the proper behavior but I'm not sure.
2014-09-02 14:52:25 -07:00
hugovk
42610c0ecd flake8 2014-08-28 17:18:54 +03:00
hugovk
c2f0903627 Correct the correction: RelatedImageWidth=0x1001, RelatedImageLength=0x1002 2014-08-27 11:06:34 +03:00
hugovk
a428321bee flake8 2014-08-27 10:57:40 +03:00
hugovk
d866a29bd8 Correct duplicate EXIF tags: http://www.exiv2.org/tags.html 2014-08-27 10:46:34 +03:00
Eric W. Brown
53b7f6294b First steps toward MPO support.
Allows Pillow to distinguish between JPEGs and MPOs, and provides some
MPO metadata handling. Does not yet handle multiple frames.
2014-07-16 11:36:56 -04:00
hugovk
cf04a9a0d2 Remove unused tearDownModule 2014-07-07 20:03:50 +03:00
Michael Brown
8b365f542a Skip tests if external commands aren't found 2014-06-27 19:11:01 -04:00
Michael Brown
d283f77884 Tests for _save_netpbm, _save_cjpeg and load_djpeg 2014-06-27 19:07:20 -04:00
wiredfool
dfe7ff515f Additional jpeg qtables tests 2014-06-20 01:09:59 -07:00
wiredfool
fcd4c662bf Fixed JPEG qtables test 2014-06-20 00:41:58 -07:00
wiredfool
7dc4dcc9fd Merge from master 2014-06-20 00:34:32 -07:00
hugovk
3ec505958e Convert old tests to use unittest 2014-06-10 12:10:47 +03:00
hugovk
c9a4272af6 Replace python-imaging with python-pillow (but yet not Coveralls) 2014-06-03 13:02:44 +03:00
Chris Sinchok
70a50907c2 This patch allows a JPEG image to be saved with a specific qtables
value (in dictionary format).

Previously, this would throw a TypeError when checking if the qtables
value was actually a preset. By adding an isStringType check, we can
avoid this error.
2014-05-28 17:21:58 -05:00
hugovk
0c1c620e30 Add test for #647. Fails without fix. 2014-05-21 14:33:28 +03:00
hugovk
aed2646dc2 pep8/pyflakes 2014-05-21 14:32:24 +03:00
wiredfool
5a4808d2d2 test cleanup: don't mask file builtin, data is unused 2014-03-27 16:44:58 -07:00
wiredfool
c7af2bf5b0 Test for #577 2014-03-27 16:39:58 -07:00
Joaquín Cuenca Abela
96ca9ff6ff Another attempt to achieve python 3 compatibility. 2014-02-04 17:37:18 +01:00
Joaquín Cuenca Abela
97b23af7e5 Try to fix test_file_jpeg in Python3 2014-02-04 17:23:29 +01:00
Joaquín Cuenca Abela
a0af87e524 Make test_file_jpeg compatible with Python 3 2014-01-28 08:48:40 +01:00
Joaquín Cuenca Abela
bab068a337 Improve heuristic used when saving progressive and optimized JPEGs with high quality values. 2014-01-27 20:27:03 +01:00
Joaquín Cuenca Abela
c68044bf7f Fix IOError when saving progressive JPEGs.
when the jpeg encoder sees the flags optimize or progressive (or progression)
it will write the full image in one shot.

The bufsize needs to be big enough to hold the entire image. The current heuristic
is that the entire compressed image will fit in width * height bytes, but this
heuristic is only applied to save operations with the flag "optimize" and not to
save operations with the flag "progressive".

This patch fixes this oversight.

(Btw, it will probably be a good idea to have a loop that retries with a bigger
bufsize in case this guess is not big enough.)
2013-08-02 14:36:46 +02:00
wiredfool
37359369ce Code to ensure buffer space for exif write and jpeg_write_scanline calls, tests, corrected buffer length calculation 2013-05-14 22:29:31 -07:00