Move fp handling into ImageFile

This commit is contained in:
Eric Soroos 2017-11-13 09:34:37 +00:00
parent af9c0a1cfd
commit 2e94a2e649
2 changed files with 35 additions and 18 deletions

View File

@ -607,28 +607,11 @@ class Image(object):
had their file read and closed by the
:py:meth:`~PIL.Image.Image.load` method.
"""
try:
self.fp.close()
self.fp = None
except Exception as msg:
logger.debug("Error closing: %s", msg)
if getattr(self, 'map', None):
self.map = None
# Instead of simply setting to None, we're setting up a
# deferred error that will better explain that the core image
# object is gone.
self.im = deferred_error(ValueError("Operation on closed image"))
if sys.version_info >= (3, 4, 0):
def __del__(self):
# type: () -> None
if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
and self.fp and self._exclusive_fp):
self.fp.close()
self.fp = None
def _copy(self):
# type: () -> None
self.load()

View File

@ -33,6 +33,9 @@ import io
import os
import sys
import struct
import logging
logger = logging.getLogger(__name__)
MAXBLOCK = 65536
@ -85,6 +88,7 @@ class ImageFile(Image.Image):
self.decoderconfig = ()
self.decodermaxblock = MAXBLOCK
self.fp = None # type: Optional[file]
if isPath(fp):
# filename
@ -113,6 +117,36 @@ class ImageFile(Image.Image):
if not self.mode or self.size[0] <= 0:
raise SyntaxError("not identified by this driver")
if sys.version_info >= (3, 4, 0):
def __del__(self):
# type: () -> None
if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
and self.fp and self._exclusive_fp):
self.fp.close()
self.fp = None
def close(self):
"""
Closes the file pointer, if possible.
This operation will destroy the image core and release its memory.
The image data will be unusable afterward.
This function is only required to close images that have not
had their file read and closed by the
:py:meth:`~PIL.Image.Image.load` method.
"""
try:
self.fp.close()
self.fp = None
except Exception as msg:
logger.debug("Error closing: %s", msg)
if getattr(self, 'map', None):
self.map = None
Image.Image.close(self)
def draft(self, mode, size):
"Set draft mode"