Skip to content

Commit

Permalink
Close index file handle correctly on save/load
Browse files Browse the repository at this point in the history
  • Loading branch information
pkittenis committed Nov 11, 2016
1 parent 453f387 commit 811092f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions influxgraph/classes/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ def to_file(self, file_h):
"""Dump tree contents to file handle"""
data = bytes(json.dumps(self.to_array()), 'utf-8') \
if not isinstance(b'', str) else json.dumps(self.to_array())
file_h.write(data)
try:
file_h.write(data)
finally:
file_h.close()

def to_array(self):
"""Return array representation of tree index"""
Expand All @@ -127,4 +130,6 @@ def from_file(file_h):
"""Load tree index from file handle"""
data = file_h.read().decode('utf-8') \
if not isinstance(b'', str) else file_h.read()
return NodeTreeIndex.from_array(json.loads(data))
index = NodeTreeIndex.from_array(json.loads(data))
file_h.close()
return index

0 comments on commit 811092f

Please sign in to comment.