Commit Graph

15 Commits

Author SHA1 Message Date
Andrew Murray
675c5e1a9c Apply black formatting to code examples 2021-12-04 07:50:08 +11:00
Andrew Murray
dd08584f7a
Updated syntax
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
2021-10-17 10:37:19 +11:00
Andrew Murray
669512c2ac __exit__ does not destroy the core image object 2021-10-16 23:26:43 +11:00
Andrew Murray
fa74f2272b Copied images are not associated with the original file 2021-10-16 23:09:04 +11:00
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
30841fe808
Merge pull request #3737 from jdufresne/quotes
Add backticks around Python classes and modules in open_files.rst
2019-03-23 08:31:15 +00:00
Jon Dufresne
d31bee5e35 Add backticks around Python classes and modules in open_files.rst 2019-03-22 06:12:08 -07:00
Jon Dufresne
deb8a7aadd Remove additional references to removed handles_eof
handles_eof was removed in 90760a5f30.
2019-03-22 05:58:22 -07:00
Andrew Murray
b0330047ff Minor edit 2018-12-12 00:53:31 +11:00
Hugo
94f4c4a682
Apply suggestions from code review
Minor edits

Co-Authored-By: radarhere <3112309+radarhere@users.noreply.github.com>
2018-12-12 00:50:32 +11:00
Andrew Murray
91f727051b Updated open files documentation 2018-12-11 14:39:10 +11:00
Andrew Murray
d9653a48c7 Added file handling links in documentation 2018-06-30 16:44:59 +10:00
Metallicow
b13025129b Trim trailing whitespace docs dir 2018-01-27 00:04:46 -06:00
wiredfool
cf85e8f1c7 design docs for file closing 2016-12-01 08:28:09 -08:00
wiredfool
1fcde0bd9f design docs for file closing 2016-12-01 08:28:09 -08:00