Browse Source

Fix unclosed file handles

pull/203/head
Oliverpool 9 years ago
parent
commit
d3a57ef1a6
  1. 8
      patacrep/songs/__init__.py

8
patacrep/songs/__init__.py

@ -152,16 +152,16 @@ class Song:
def filehash(self): def filehash(self):
"""Compute (and cache) the md5 hash of the file""" """Compute (and cache) the md5 hash of the file"""
if self._filehash is None: if self._filehash is None:
self._filehash = hashlib.md5( with open(self.fullpath, 'rb') as songfile:
open(self.fullpath, 'rb').read() self._filehash = hashlib.md5(songfile.read()).hexdigest()
).hexdigest()
return self._filehash return self._filehash
def _cache_retrieved(self): def _cache_retrieved(self):
"""If relevant, retrieve self from the cache.""" """If relevant, retrieve self from the cache."""
if self.use_cache and os.path.exists(self.cached_name): if self.use_cache and os.path.exists(self.cached_name):
try: try:
cached = pickle.load(open(self.cached_name, 'rb',)) with open(self.cached_name, 'rb',) as cachefile:
cached = pickle.load(cachefile)
if ( if (
cached['_filehash'] == self.filehash cached['_filehash'] == self.filehash
and cached['_version'] == self.CACHE_VERSION and cached['_version'] == self.CACHE_VERSION

Loading…
Cancel
Save