Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Created January 9, 2020 01:55
Show Gist options
  • Save jarshwah/84964e23542ce4ec39e0a1ffc2a06d3b to your computer and use it in GitHub Desktop.
Save jarshwah/84964e23542ce4ec39e0a1ffc2a06d3b to your computer and use it in GitHub Desktop.
import pickle
import six
from django_redis.serializers.pickle import PickleSerializer
def compat_pickle_loads(value, **kwargs):
if six.PY2:
return _py2_pickle_loads(value)
return _py3_pickle_loads(value, **kwargs)
def _py2_pickle_loads(value):
return pickle.loads(value)
def _py3_pickle_loads(value, **kwargs):
try:
return pickle.loads(value, **kwargs)
except UnicodeDecodeError:
return pickle.loads(value, encoding="latin1", **kwargs)
class SixPickleSerializer(PickleSerializer):
# for https://github.com/niwinz/django-redis
def loads(self, value):
return compat_pickle_loads(value)
CACHES = {
"default": {
"LOCATION": [
"redis://{0}:{1}/1".format(self._REDIS_HOST, self._REDIS_PORT),
"redis://{0}:{1}/1".format(self._REDIS_HOST_READONLY, self._REDIS_PORT),
],
"OPTIONS": {
"PARSER_CLASS": "redis.connection.HiredisParser",
"PICKLE_VERSION": 2, # compat with py2 and py3
"SERIALIZER": "core.serializers.SixPickleSerializer",
"REDIS_CLIENT_KWARGS": {
"health_check_interval": 30 # https://github.com/andymccurdy/redis-py/issues/1186
},
},
}
}
from core.serializers import compat_pickle_loads
...
class TestPickleCompat(TestCase):
def test_compat(self):
value = b"\x80\x02}q\x01U\x0e_in_stock_dateq\x02cdatetime\ndate\nq\x03U\x04\x07\xe4\x01\t\x85Rq\x04s."
self.assertEqual(compat_pickle_loads(value), {"_in_stock_date": datetime.date(2020, 1, 9)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment