Skip to content

fix unpickling py2 objects #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion django_pylibmc/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from django.conf import settings
from django.core.cache.backends.base import InvalidCacheBackendError
from django.core.cache.backends.memcached import BaseMemcachedCache, DEFAULT_TIMEOUT
import sys
from pickle import load as pickle_load
from io import BytesIO

try:
import pylibmc
Expand All @@ -25,6 +28,14 @@
raise InvalidCacheBackendError('Could not import pylibmc.')


if sys.version_info.major == 3 and sys.version_info.minor < 8:
# for the upgrade from python version earlier than 3.8
try:
from pickle5 import load as pickle_load
except ImportError:
pass


log = logging.getLogger('django.pylibmc')


Expand All @@ -47,6 +58,16 @@
'compress_level': COMPRESS_LEVEL,
}

class MemcachedClient(pylibmc.Client):

if sys.version_info.major >= 3:
PYLIBMC_FLAG_PICKLE = 1

def deserialize(self, value, flags):
if flags & self.PYLIBMC_FLAG_PICKLE:
return pickle_load(BytesIO(value), encoding=u"latin1")
return super(MemcachedClient, self).deserialize(value, flags)


class PyLibMCCache(BaseMemcachedCache):

Expand Down Expand Up @@ -75,7 +96,7 @@ def _cache(self):
'username': self._username,
'password': self._password
})
client = self._lib.Client(self._servers, **client_kwargs)
client = MemcachedClient(self._servers, **client_kwargs)
if self._options:
client.behaviors = self._options

Expand Down Expand Up @@ -144,6 +165,10 @@ def get_many(self, *args, **kwargs):
def set_many(self, *args, **kwargs):
try:
return super(PyLibMCCache, self).set_many(*args, **kwargs)
except pylibmc.ServerError:
log.error('ServerError set_many args = %s kwargs = %s' % (str(args), str(kwargs)),
exc_info=True)
return False
except MemcachedError as e:
log.error('MemcachedError: %s', e, exc_info=True)
return False
Expand Down