Commit Graph

26 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
Hugo
f792ab6c02 RST uses double backticks for code (MD uses 1) 2019-09-13 08:56:33 +03:00
Andrew Murray
38df9bef09 Added import [ci skip] 2019-05-06 08:38:43 +10:00
Andrew Murray
fa8ecffff2 Corrected seek documentation 2019-03-17 00:00:10 +11:00
Andrew Murray
bd600c6966 Crop is no longer lazy [ci skip] 2018-09-29 15:31:10 +10:00
Andrew Murray
6072e2e595 Changed note to be more generic [ci skip] 2018-09-29 15:12:48 +10:00
Andrew Murray
454ffe4a01 Changed tutorial code to use docstring [ci skip] 2018-01-12 20:46:16 +11:00
Andrew Murray
5eabd731f0 Changed tutorial code to use with for opening files [ci skip] 2017-05-29 19:41:48 +10:00
Andrew Murray
cafc311943 Added import statements to make examples self-contained [ci skip] 2017-05-29 19:41:30 +10:00
Andrew Murray
424a09ecad Updated references to point to existing files [ci skip] 2017-05-06 23:20:34 +10:00
Andrew Murray
3e99ef8a43 Added import to docs [ci skip] 2016-09-29 20:11:38 +10:00
Andrew Murray
5c0580adc2 Added note to docs regarding draft mode formats [ci skip] 2016-09-29 20:11:19 +10:00
Andrew Murray
44c5d0175e Fixed code and added explanation to a tutorial example [ci skip] 2016-05-02 18:57:46 +10:00
Andrew Murray
ea3dcfda0c Changed documentation to refer to ImageSequence Iterator class [ci skip] 2016-04-16 11:43:28 +10:00
Andrew Murray
c3e0a685be Corrected documentation [ci skip] 2016-02-28 23:29:12 +11:00
Charles Merriam
f211601ecd In tutorial of pasting images, add to mask text.
Using an RGBA image as its own mask is a common question.  It shows up in a dozen Stack Overflow questions, e.g.,  (http://stackoverflow.com/questions/5324647/how-to-merge-a-transparent-png-image-with-another-image-using-pil).    Adding a sentence to the tutorial gives people a chance of noticing this.
2015-08-23 18:30:39 -07:00
hugovk
1b8ba191e9 Update docs to remove reference to textsize() 2014-12-27 23:05:34 +02:00
Mike Driscoll
7bed246e85 Update tutorial.rst
Just a couple grammatical fixes
2014-10-03 10:00:50 -05:00
wiredfool
8c6a4c0299 Docs changes for close/context manager 2014-04-17 21:53:49 -07:00
Marek Šuppa
a6336e7ecc Removed empty line
It seems that this empty line is what makes this code in the tutorial not highlighted 

http://pillow.readthedocs.org/en/latest/handbook/tutorial.html#a-sequence-iterator-class
2014-01-16 12:20:19 +01:00
Steve Johnson
55913cab1b Print function in tutorial 2013-10-06 22:54:39 -07:00
Steve Johnson
ff6951f2a4 Fix a tutorial section heading title case 2013-10-06 21:59:08 -07:00
Steve Johnson
f75f83beb0 Fix minor typo 2013-10-06 21:53:26 -07:00
Steve Johnson
c38a858a6f Concepts 2013-10-06 21:39:20 -07:00
Steve Johnson
9dc6f83803 Convert Tutorial section of PIL handbook 2013-10-06 21:39:08 -07:00
Steve Johnson
9ea731287d Stub docs/handbook 2013-10-06 20:27:59 -07:00