Merge pull request #751 from akolpakov/issue_750

#750 CompilerFilter fails with UnicodeDecodeError when cache is used
This commit is contained in:
karyon 2016-04-27 22:31:16 +02:00
commit f5c1469c8e
2 changed files with 12 additions and 1 deletions

View File

@ -221,7 +221,7 @@ class CachedCompilerFilter(CompilerFilter):
key = self.get_cache_key()
data = cache.get(key)
if data is not None:
return data
return smart_text(data)
filtered = super(CachedCompilerFilter, self).input(**kwargs)
cache.set(key, filtered, settings.COMPRESS_REBUILD_TIMEOUT)
return filtered

View File

@ -3,8 +3,10 @@ from collections import defaultdict
import io
import os
import sys
import mock
from django.utils import six
from django.utils.encoding import smart_text
from django.test import TestCase
from django.test.utils import override_settings
@ -112,6 +114,15 @@ class PrecompilerTestCase(TestCase):
self.assertEqual("body { color:#990; }", compiler.input())
self.assertIsNotNone(compiler.infile) # Not cached
@mock.patch('django.core.cache.backends.locmem.LocMemCache.get')
def test_precompiler_cache_issue750(self, mock_cache):
# emulate memcached and return string
mock_cache.side_effect = (lambda key: str("body { color:#990; }"))
command = '%s %s -f {infile} -o {outfile}' % (sys.executable, self.test_precompiler)
compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args)
self.assertEqual("body { color:#990; }", compiler.input())
self.assertEqual(type(compiler.input()), type(smart_text("body { color:#990; }")))
def test_precompiler_not_cacheable(self):
command = '%s %s -f {infile} -o {outfile}' % (sys.executable, self.test_precompiler)
self.cached_precompiler_args['mimetype'] = 'text/different'