Added context manager when opening files in WalImageFile

This commit is contained in:
Andrew Murray 2016-11-26 11:29:20 +11:00
parent 06895b6fa4
commit 87de178e0b

View File

@ -47,11 +47,7 @@ def open(filename):
# FIXME: modify to return a WalImageFile instance instead of # FIXME: modify to return a WalImageFile instance instead of
# plain Image object ? # plain Image object ?
if hasattr(filename, "read"): def imopen(fp):
fp = filename
else:
fp = builtins.open(filename, "rb")
# read header fields # read header fields
header = fp.read(32+24+32+12) header = fp.read(32+24+32+12)
size = i32(header, 32), i32(header, 36) size = i32(header, 32), i32(header, 36)
@ -74,6 +70,11 @@ def open(filename):
return im return im
if hasattr(filename, "read"):
return imopen(filename)
else:
with builtins.open(filename, "rb") as fp:
return imopen(fp)
quake2palette = ( quake2palette = (
# default palette taken from piffo 0.93 by Hans Häggström # default palette taken from piffo 0.93 by Hans Häggström