Commit Graph

50 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
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
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
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
Andrew Murray
6c126ca282 Change color table index background to tuple when saving 2018-11-20 19:50:14 +11:00
Hugo
94c94eb1f7 flake8: E128 continuation line under-indented for visual indent 2018-11-11 19:01:11 +02:00
Hugo
ed10276a6b
Merge pull request #3418 from radarhere/webp_fp
Only close existing fp in WebP if fp is exclusive
2018-10-31 09:16:53 +02:00
Andrew Murray
c7d924fd85 Too many blank lines 2018-10-21 18:11:50 +11:00
Andrew Murray
e84a78d828 Only close existing fp if fp is exclusive 2018-10-19 20:12:08 +11:00
Alexander
e26696cb8a skip old test 2018-10-18 10:33:52 +03:00
Alexander
e7aa44f58c add test for regression 2018-10-18 10:32:17 +03:00
Andrew Murray
82d942c3d8 Added warnings if image file identification fails due to lack of support 2018-09-30 15:34:27 +10:00
Andrew Murray
a8261a2e89 Line too long 2018-09-27 20:35:00 +10:00
Hugo
521ae77bb9
Merge pull request #3298 from kkopachev/webp-rgb
Read/save RGB webp as RGB (instead of RGBX)
2018-09-17 12:05:07 +03:00
Andrew Murray
9d9da79caa Close existing fp before setting new fp 2018-09-08 23:08:17 +10:00
Konstantin Kopachev
84b32a0388
Read/Save RGB webp as RGB (instead of RGBX) 2018-08-09 11:40:10 -07:00
Jason Douglas
cf31e70735 Fixing a typo in comments 2017-10-03 08:05:20 -07:00
Jason Douglas
28bec69e98 - flake8 formatting fixes
- webp => WebP doc and comment changes
2017-10-01 15:23:18 -07:00
Jason Douglas
5b2dd29f44 Fix invalid_args test for older versions of libwebp 2017-09-27 20:21:18 -07:00
Jason Douglas
80b96246c4 Fix tests to support different output modes (RGB vs RGBX) 2017-09-27 19:28:43 -07:00
Jason Douglas
e534991409 - Styling changes to be C89-conformant
- Change WebPAnimEncoder/Decoder to use RGBX mode instead of RGB (since internally it is using RGBA always)
2017-09-27 17:10:25 -07:00
Jason Douglas
cd12a48fe0 - Support non-alpha modes with WebPAnimDecoder
- Support writing metadata chunks with WebPAnimEncoder
- Add XMP metadata support to legacy WebPEncode wrapper
- Cleanup unused mux code in legacy WebPDecode wrapper
- Fix some bugs present when compiled without WebP Mux support
- Fix conversion from L/P/PA modes when saving WebP files
- Update existing tests, and add new ones for WebP animation and metadata support
2017-09-26 20:27:40 -07:00
Andrew Murray
9e843a2d9b Changed format of lambda calls 2017-09-01 21:05:40 +10:00
adw1n
6ce11c9e8c Added tests that call _webp.WebPEncode and _webp.WebPDecode with invalid number of arguments. 2017-05-12 17:11:58 +02:00
homm
1ba4e9e5ba remove end of file banner from all files 2016-07-10 14:11:28 +03:00
Andrew Murray
a06b59bd52 Added various tests 2015-07-03 15:03:25 +10:00
Hugo
ae7b765b53 Fix some Landscape.io errors and warnings 2014-10-01 16:50:33 +03:00
Hugo
e8aecc7484 Remove temporary save line 2014-09-22 19:01:50 +03:00
Hugo
04d402adae Replace lena with hopper 2014-09-22 19:00:22 +03:00
hugovk
cf04a9a0d2 Remove unused tearDownModule 2014-07-07 20:03:50 +03:00
hugovk
78003ca473 Update paths to Tests/images/ instead of Images/ 2014-06-23 09:19:29 +03:00
hugovk
3ec505958e Convert old tests to use unittest 2014-06-10 12:10:47 +03:00
wiredfool
78a51237c5 tests for #491/492, webp lossless abi 2014-01-20 13:05:30 -08:00
Alex Clark ☺
bcfc06d6d0 Merge pull request #469 from manisandro/test_webp
Use assert_image_similar in test_file_webp.py
2014-01-04 13:34:43 -08:00
Sandro Mani
4de31b2693 Use assert_image_similar instead of assert_image_equal in test_file_webp.py 2014-01-04 21:48:18 +01:00
wiredfool
2c1e800836 added new VP8 mode for lossless reading of RGBA, fixes failing tests 2013-10-22 12:44:09 -07:00
wiredfool
27b1054b77 Tests for writing lossless RGB and RGBA images 2013-10-22 12:32:31 -07:00
Alex Clark
bb1b3a532c Cleanup WS, courtesy of @Arfrever
find * -type f "-(" -name "*.bdf" -o -name "*.c" -o -name "*.h" -o -name "*.py" -o -name "*.rst" -o -name "*.txt" "-)" -exec sed -e "s/[[:space:]]*$//" -i {} \;
2013-06-30 18:42:19 -04:00
wiredfool
b3752ca053 merge from master 2013-05-21 21:29:58 -07:00
wiredfool
94239540db fixing deferring the rgba tests, whitespace 2013-05-13 22:21:52 -07:00
wiredfool
014ca1497d versions, feature checking 2013-05-13 21:28:18 -07:00
Euan Goddard
1344610a52 Added support for alpha transparent webp images #204 2013-05-13 17:01:42 +01:00
wiredfool
8295e33390 moved _webp into the PIL namespace 2013-04-08 21:53:59 -07:00
wiredfool
38b0bef464 webp is in import, not a codec 2013-03-27 10:00:35 -07:00
wiredfool
86bb25e735 reenable basic webp test, skip if support not installed 2013-03-27 09:52:47 -07:00
wiredfool
f432530df3 approximate test for lossy image writing 2013-03-22 21:15:45 -07:00
wiredfool
d678b97444 added write verification test 2013-03-19 13:49:01 -07:00
wiredfool
02931c4956 checking results of webp decoding 2013-03-19 13:40:10 -07:00
wiredfool
95353cff15 basic sanity test for webp 2013-03-19 11:43:45 -07:00