Fixed #28500 -- Fixed crash in FileBasedCache._is_expired() if the cache file is empty.

This commit is contained in:
caleb logan 2017-08-30 21:22:26 -07:00 committed by Tim Graham
parent 2dacc2ccd9
commit 68f0e8d8b1
2 changed files with 11 additions and 1 deletions

View File

@ -116,7 +116,10 @@ class FileBasedCache(BaseCache):
""" """
Take an open cache file `f` and delete it if it's expired. Take an open cache file `f` and delete it if it's expired.
""" """
try:
exp = pickle.load(f) exp = pickle.load(f)
except EOFError:
exp = 0 # An empty file is considered expired.
if exp is not None and exp < time.time(): if exp is not None and exp < time.time():
f.close() # On Windows a file has to be closed before deleting f.close() # On Windows a file has to be closed before deleting
self._delete(f.name) self._delete(f.name)

View File

@ -1366,6 +1366,13 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
with self.assertRaises(IOError): with self.assertRaises(IOError):
cache.get('foo') 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={ @override_settings(CACHES={
'default': { 'default': {