Pillow/Tests
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
..
fonts add test for #3777 2019-06-25 22:16:00 +10:00
icc Corrected filename [ci skip] 2016-05-03 19:23:51 +10:00
images Merge pull request #4104 from radarhere/overrun 2019-09-30 23:33:28 +10:00
__init__.py Improve pytest configuration to allow specific tests as CLI args 2019-01-13 09:00:12 -08:00
32bit_segfault_check.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
bench_cffi_access.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
bench_get.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
check_fli_overflow.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
check_icns_dos.py Use bytes literals instead of bytes(str) 2019-10-06 19:26:55 -07:00
check_imaging_leaks.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
check_j2k_dos.py Use bytes literals instead of bytes(str) 2019-10-06 19:26:55 -07:00
check_j2k_leaks.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
check_j2k_overflow.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
check_jpeg_leaks.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
check_large_memory_numpy.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
check_large_memory.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
check_libtiff_segfault.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
check_png_dos.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
createfontdatachunk.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
helper.py Merge branch 'master' into gha-win 2019-10-08 12:56:43 +01:00
import_all.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
make_hash.py Format with Black 2019-06-13 18:53:42 +03:00
README.rst Corrected underline [ci skip] 2019-08-17 20:32:24 +10:00
test_000_sanity.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_binary.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_bmp_reference.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_box_blur.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_color_lut.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_core_resources.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
test_decompression_bomb.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_features.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_blp.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_bmp.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_bufrstub.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_container.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_cur.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_dcx.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_dds.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_eps.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_fitsstub.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_fli.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_fpx.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_ftex.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_gbr.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_gd.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_gif.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_gimpgradient.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_gimppalette.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_gribstub.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_hdf5stub.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_icns.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_ico.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_im.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_iptc.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_jpeg.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_jpeg2k.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_libtiff_small.py Move several imports to the top-level of the file 2019-10-07 06:28:36 -07:00
test_file_libtiff.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_mcidas.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_mic.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_mpo.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_msp.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_palm.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_pcd.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_pcx.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_pdf.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_pixar.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_png.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_ppm.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_psd.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_sgi.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_spider.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_sun.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_tar.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_tga.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_tiff_metadata.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_tiff.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_wal.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_webp_alpha.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_webp_animated.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_webp_lossless.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_file_webp_metadata.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_webp.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_wmf.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_xbm.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_xpm.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_file_xvthumb.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_font_bdf.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_font_leaks.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
test_font_pcf.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_format_hsv.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_format_lab.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_access.py Merge branch 'master' into gha-win 2019-10-08 12:56:43 +01:00
test_image_array.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_convert.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_image_copy.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_crop.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_draft.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_entropy.py Lint fixes 2019-06-26 19:16:36 +10:00
test_image_filter.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_frombytes.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_fromqimage.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_image_getbands.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_getbbox.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_getcolors.py Format with Black 2019-06-13 18:54:24 +03:00
test_image_getdata.py Format with Black 2019-06-13 18:54:24 +03:00
test_image_getextrema.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_getim.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_getpalette.py Format with Black 2019-06-13 18:54:24 +03:00
test_image_getprojection.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_histogram.py Format with Black 2019-06-13 18:54:24 +03:00
test_image_load.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_mode.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_image_paste.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_point.py Format with Black 2019-06-13 18:54:24 +03:00
test_image_putalpha.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_putdata.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_putpalette.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_quantize.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_resample.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_resize.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_image_rotate.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_split.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_thumbnail.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_image_tobitmap.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image_tobytes.py Format with Black 2019-06-13 18:54:24 +03:00
test_image_transform.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_image_transpose.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_image.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imagechops.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagecms.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imagecolor.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagedraw.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imagedraw2.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imageenhance.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagefile.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imagefont_bitmap.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagefont.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
test_imagefontctl.py Added text stroking 2019-07-29 06:40:03 +10:00
test_imagegrab.py rename parameter, add note to docs 2019-09-20 17:35:08 +02:00
test_imagemath.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagemorph.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imageops_usm.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imageops.py Do not calculate the crop width if it is already known 2019-09-29 14:26:32 +10:00
test_imagepalette.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagepath.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imageqt.py Removed deprecation test 2019-10-02 22:01:31 +10:00
test_imagesequence.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imageshow.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_imagestat.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagetk.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_imagewin_pointers.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
test_imagewin.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
test_lib_image.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_lib_pack.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_locale.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_main.py Tests.helper cleanup 2019-09-25 11:58:02 +02:00
test_map.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_mode_i16.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_numpy.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_pdfparser.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_pickle.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_psdraw.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_pyroma.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_qt_image_fromqpixmap.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_qt_image_toqimage.py Drop support for EOL PyQt4 and PySide 2019-09-30 17:58:31 +03:00
test_qt_image_toqpixmap.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_shell_injection.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_tiff_ifdrational.py Improve handling of file resources 2019-10-12 08:27:17 -07:00
test_uploader.py Format with Black 2019-06-13 18:54:46 +03:00
test_util.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
test_webp_leaks.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
threaded_save.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00
versions.py Introduce isort to automate import ordering and formatting 2019-07-06 16:11:35 -07:00

Pillow Tests
============

Test scripts are named ``test_xxx.py`` and use the ``unittest`` module. A base class and helper functions can be found in ``helper.py``.

Dependencies
------------

Install::

    pip install pytest pytest-cov

Execution
---------

To run an individual test::

    pytest Tests/test_image.py

Or::

    pytest -k test_image.py

Run all the tests from the root of the Pillow source distribution::

    pytest

Or with coverage::

    pytest --cov PIL --cov-report term
    coverage html
    open htmlcov/index.html