Skip to content

Commit

Permalink
fix s.close bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xlcnd committed Oct 20, 2015
1 parent 7b65113 commit 50c6e09
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions isbnlib/dev/_shelvecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __getitem__(self, key):
else:
return None
except ValueError:
self.new()
s = self._sh.open(self.filepath, 'n')
self._keys = []
return None
finally:
s.close()
Expand All @@ -85,7 +86,8 @@ def __delitem__(self, key):
del s[key]
self._keys.remove(key)
except ValueError:
self.new()
s = self._sh.open(self.filepath, 'n')
self._keys = []
return
finally:
s.close()
Expand Down Expand Up @@ -121,7 +123,8 @@ def ts(self, key):
fmt = '%Y-%m-%d %H:%M:%S'
return datetime.datetime.fromtimestamp(ts).strftime(fmt)
except ValueError:
self.new()
s = self._sh.open(self.filepath, 'n')
self._keys = []
return
finally:
s.close()
Expand All @@ -135,7 +138,8 @@ def hits(self, key):
hts = s[key]['hits'] if s[key] else None
return hts
except ValueError:
self.new()
s = self._sh.open(self.filepath, 'n')
self._keys = []
return
finally:
s.close()
Expand All @@ -149,9 +153,9 @@ def new(self):

def purge(self):
"""Purge the cache."""
if len(self.keys()) < self.MAXLEN:
return
try:
if len(self.keys()) < self.MAXLEN:
return
s = self._sh.open(self.filepath)
data = [(k, s[k]['timestamp'], s[k]['hits']) for k in s.keys()]
data.sort(key=lambda tup: (-tup[2], -tup[1]))
Expand Down

0 comments on commit 50c6e09

Please sign in to comment.