Skip to content

Commit

Permalink
Merge 3d35232 into b5d0cf5
Browse files Browse the repository at this point in the history
  • Loading branch information
wiredfool committed Apr 9, 2014
2 parents b5d0cf5 + 3d35232 commit aaddd61
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
24 changes: 23 additions & 1 deletion PIL/Image.py
Expand Up @@ -92,7 +92,7 @@ def __getattr__(self, id):

from PIL import ImageMode
from PIL._binary import i8, o8
from PIL._util import isPath, isStringType
from PIL._util import isPath, isStringType, deferred_error

import os, sys

Expand Down Expand Up @@ -497,6 +497,28 @@ def _new(self, im):

_makeself = _new # compatibility

# with compatibility
def __enter__(self):
return self
def __exit__(self, *args):
self.close()

def close(self):
""" Close the file pointer, if possible. Destroy the image core.
This releases memory, and the image will be unusable afterward
"""
try:
self.fp.close()
except Exception as msg:
if Image.DEBUG:
print ("Error closing: %s" %msg)

# 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"))


def _copy(self):
self.load()
self.im = self.im.copy()
Expand Down
4 changes: 2 additions & 2 deletions PIL/ImageCms.py
Expand Up @@ -89,8 +89,8 @@
except ImportError as ex:
# Allow error import for doc purposes, but error out when accessing
# anything in core.
from _util import import_err
_imagingcms = import_err(ex)
from _util import deferred_error
_imagingcms = deferred_error(ex)
from PIL._util import isStringType

core = _imagingcms
Expand Down
2 changes: 1 addition & 1 deletion PIL/_util.py
Expand Up @@ -15,7 +15,7 @@ def isPath(f):
def isDirectory(f):
return isPath(f) and os.path.isdir(f)

class import_err(object):
class deferred_error(object):
def __init__(self, ex):
self.ex = ex
def __getattr__(self, elt):
Expand Down

0 comments on commit aaddd61

Please sign in to comment.