From 779829662d48a54ac427574e5e0c279b69519e42 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Wed, 1 Jun 2016 12:29:24 -0700 Subject: [PATCH] Fixed #26694 -- Made FileBasedCache.get() reraise non-ENOENT IOErrors. --- django/core/cache/backends/filebased.py | 4 ++-- tests/cache/tests.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index bff912d85d..7c2c5c7edb 100644 --- a/django/core/cache/backends/filebased.py +++ b/django/core/cache/backends/filebased.py @@ -40,8 +40,8 @@ class FileBasedCache(BaseCache): if not self._is_expired(f): return pickle.loads(zlib.decompress(f.read())) except IOError as e: - if e.errno == errno.ENOENT: - pass # Cache file doesn't exist. + if e.errno != errno.ENOENT: + raise return default def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None): diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 1e74df1e56..e2044bbdad 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import copy +import io import os import re import shutil @@ -1261,6 +1262,17 @@ class FileBasedCacheTests(BaseCacheTests, TestCase): # This fails if not using the highest pickling protocol on Python 2. cache.set('unpicklable', UnpicklableType()) + def test_get_ignores_enoent(self): + cache.set('foo', 'bar') + os.unlink(cache._key_to_file('foo')) + # Returns the default instead of erroring. + self.assertEqual(cache.get('foo', 'baz'), 'baz') + + def test_get_does_not_ignore_non_enoent_errno_values(self): + with mock.patch.object(io, 'open', side_effect=IOError): + with self.assertRaises(IOError): + cache.get('foo') + @override_settings(CACHES={ 'default': {