summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcaleb logan <clogan202@gmail.com>2017-08-30 21:22:26 -0700
committerTim Graham <timograham@gmail.com>2017-09-03 20:11:49 -0400
commit68f0e8d8b1f9bdf9fc1c26ca5ec62a88c691ca20 (patch)
tree2fb7aa1ffc842e3f2dfc1faa369b9155963ad279
parent2dacc2ccd9593678f19b61ecfe1bc6ee826c2a88 (diff)
Fixed #28500 -- Fixed crash in FileBasedCache._is_expired() if the cache file is empty.
-rw-r--r--django/core/cache/backends/filebased.py5
-rw-r--r--tests/cache/tests.py7
2 files changed, 11 insertions, 1 deletions
diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
index 09a68b34bb..deceffaa0d 100644
--- a/django/core/cache/backends/filebased.py
+++ b/django/core/cache/backends/filebased.py
@@ -116,7 +116,10 @@ class FileBasedCache(BaseCache):
"""
Take an open cache file `f` and delete it if it's expired.
"""
- exp = pickle.load(f)
+ try:
+ exp = pickle.load(f)
+ except EOFError:
+ exp = 0 # An empty file is considered expired.
if exp is not None and exp < time.time():
f.close() # On Windows a file has to be closed before deleting
self._delete(f.name)
diff --git a/tests/cache/tests.py b/tests/cache/tests.py
index eb5865f08d..cef000cb02 100644
--- a/tests/cache/tests.py
+++ b/tests/cache/tests.py
@@ -1366,6 +1366,13 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
with self.assertRaises(IOError):
cache.get('foo')
+ def test_empty_cache_file_considered_expired(self):
+ cache_file = cache._key_to_file('foo')
+ with open(cache_file, 'wb') as fh:
+ fh.write(b'')
+ with open(cache_file, 'rb') as fh:
+ self.assertIs(cache._is_expired(fh), True)
+
@override_settings(CACHES={
'default': {