Skip to content

Commit d716afa

Browse files
committed
resolve minor resource error
1 parent bf9f44d commit d716afa

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

tdl/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,27 @@ def _to_tcod_color(color):
163163

164164
def _getImageSize(filename):
165165
"""Try to get the width and height of a bmp of png image file"""
166+
result = None
166167
file = open(filename, 'rb')
167168
if file.read(8) == b'\x89PNG\r\n\x1a\n': # PNG
168169
while 1:
169170
length, = _struct.unpack('>i', file.read(4))
170171
chunkID = file.read(4)
171172
if chunkID == '': # EOF
172-
return None
173+
break
173174
if chunkID == b'IHDR':
174175
# return width, height
175-
return _struct.unpack('>ii', file.read(8))
176+
result = _struct.unpack('>ii', file.read(8))
177+
break
176178
file.seek(4 + length, 1)
179+
file.close()
180+
return result
177181
file.seek(0)
178182
if file.read(8) == b'BM': # Bitmap
179183
file.seek(18, 0) # skip to size data
180-
# return width, height
181-
return _struct.unpack('<ii', file.read(8))
182-
# return None on error, unknown file
184+
result = _struct.unpack('<ii', file.read(8))
185+
file.close()
186+
return result # (width, height), or None
183187

184188
class TDLError(Exception):
185189
"""

0 commit comments

Comments
 (0)