Skip to content

Commit

Permalink
Fixed various resource leaks and an issue with addExternalWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
Podshot committed May 30, 2018
1 parent cba330e commit 8c8b398
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
6 changes: 5 additions & 1 deletion leveleditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ def _resize_selection_box(self, new_box):
def addExternalWidget(self, obj):
if isinstance(obj, Widget):
return self.addExternalWidget_Widget(obj)
elif isinstance(obj, dict):
elif isinstance(obj, (dict, tuple, list)):
return self.addExternalWidget_Dict(obj)

def addExternalWidget_Widget(self, obj):
Expand Down Expand Up @@ -1078,6 +1078,10 @@ def addNumField(wid, name, val, minimum=None, maximum=None, increment=0.1):
cols = []
max_height = self.mainViewport.height
widget.inputDict = {}

if isinstance(provided_fields, dict):
provided_fields = [(key, value) for key, value in provided_fields.iteritems()]

for inputName, inputType in provided_fields:
if isinstance(inputType, tuple) and inputType != ():
if isinstance(inputType[0], (int, long, float)):
Expand Down
16 changes: 10 additions & 6 deletions pymclevel/_nbt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,9 @@ cdef class _TAG_Compound(TAG_Value):
return data

if isinstance(filename_or_buf, basestring):
f = file(filename_or_buf, "wb")
f = open(filename_or_buf, "wb")
f.write(data)
f.close()
else:
filename_or_buf.write(data)

Expand Down Expand Up @@ -601,19 +602,22 @@ def load(filename="", buf=None):
:rtype: TAG_Compound
"""
if filename:
buf = file(filename, "rb")
buf = open(filename, "rb")

if hasattr(buf, "read"):
buf = buf.read()
data = buf.read()

buf = try_gunzip(buf)
if hasattr(buf, "close"):
buf.close()

data = try_gunzip(data)

cdef load_ctx ctx = load_ctx()
ctx.offset = 1
ctx.buffer = buf
ctx.buffer = data
ctx.size = len(buf)

if len(buf) < 1:
if len(data) < 1:
raise NBTFormatError("NBT Stream too short!")

cdef unsigned int * magic_no = <unsigned int *> ctx.buffer
Expand Down
17 changes: 12 additions & 5 deletions pymclevel/nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def load_from(cls, ctx):
lcd=len(ctx.data), lrd=len(data)
)
)
open(dump_fName, 'a').write(msg)
fp1 = open(dump_fName, 'a')
fp1.write(msg)
fp1.close()
added_n_lines = len(msg.splitlines())
log.warning("Could not unpack NBT data: information written in {fn}, from line {b} to line {e}".format(fn=dump_fName, b=n_lines, e=(n_lines + added_n_lines - 1)))
else:
Expand Down Expand Up @@ -374,8 +376,9 @@ def save(self, filename_or_buf=None, compressed=True):
return data

if isinstance(filename_or_buf, basestring):
f = file(filename_or_buf, "wb")
f = open(filename_or_buf, "wb")
f.write(data)
f.close()
else:
filename_or_buf.write(data)

Expand Down Expand Up @@ -558,12 +561,16 @@ def load(filename="", buf=None):
containing NBT data.
"""
if filename:
buf = file(filename, "rb")
buf = open(filename, "rb")
data = buf

if hasattr(buf, "read"):
buf = buf.read()
data = buf.read()

if hasattr(buf, "close"):
buf.close()

return _load_buffer(try_gunzip(buf))
return _load_buffer(try_gunzip(data))


class load_ctx(object):
Expand Down

0 comments on commit 8c8b398

Please sign in to comment.