From 0aa018337afbc061633d9046109f12d4dd929c25 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Sat, 17 Oct 2015 11:50:14 -0400 Subject: [PATCH] Fixing logic in json cache plugin get() Fixes #12722 --- lib/ansible/plugins/cache/jsonfile.py | 29 ++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/ansible/plugins/cache/jsonfile.py b/lib/ansible/plugins/cache/jsonfile.py index bf60fcf7f7..f45c629c34 100644 --- a/lib/ansible/plugins/cache/jsonfile.py +++ b/lib/ansible/plugins/cache/jsonfile.py @@ -53,29 +53,26 @@ class CacheModule(BaseCacheModule): def get(self, key): + if self.has_expired(key) or key == "": + raise KeyError + if key in self._cache: return self._cache.get(key) - if self.has_expired(key): - raise KeyError - cachefile = "%s/%s" % (self._cache_dir, key) try: - f = codecs.open(cachefile, 'r', encoding='utf-8') + with codecs.open(cachefile, 'r', encoding='utf-8') as f: + try: + value = json.load(f) + self._cache[key] = value + return value + except ValueError as e: + self._display.warning("error while trying to read %s : %s. Most likely a corrupt file, so erasing and failing." % (cachefile, to_bytes(e))) + self.delete(key) + raise AnsibleError("The JSON cache file %s was corrupt, or did not otherwise contain valid JSON data. It has been removed, so you can re-run your command now." % cachefile) except (OSError,IOError) as e: self._display.warning("error while trying to read %s : %s" % (cachefile, to_bytes(e))) - pass - else: - try: - value = json.load(f) - self._cache[key] = value - return value - except ValueError as e: - self._display.warning("error while trying to read %s : %s. Most likely a corrupt file, so erasing and failing." % (cachefile, to_bytes(e))) - self.delete(key) - raise AnsibleError("The JSON cache file %s was corrupt, or did not otherwise contain valid JSON data. It has been removed, so you can re-run your command now." % cachefile) - finally: - f.close() + raise KeyError def set(self, key, value):