summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2018-07-06 17:55:46 -0400
committerTim Graham <timograham@gmail.com>2018-07-09 11:46:55 -0400
commit37835883ad55b3c4c3340eb8721b41fffe3ee0ef (patch)
tree0d356918e051bd2202e8b568f48b6ea39450440c
parent2d75509bcb1db910da3c568f99b6ac778a79a98f (diff)
Fixed #29550 -- Eased overriding pickle.dumps() protocol in cache backends and session serializer.
-rw-r--r--django/contrib/sessions/serializers.py4
-rw-r--r--django/core/cache/backends/db.py4
-rw-r--r--django/core/cache/backends/filebased.py5
-rw-r--r--django/core/cache/backends/locmem.py8
4 files changed, 14 insertions, 7 deletions
diff --git a/django/contrib/sessions/serializers.py b/django/contrib/sessions/serializers.py
index 1df6d9c49e..6ac4aab6a4 100644
--- a/django/contrib/sessions/serializers.py
+++ b/django/contrib/sessions/serializers.py
@@ -8,8 +8,10 @@ class PickleSerializer:
Simple wrapper around pickle to be used in signing.dumps and
signing.loads.
"""
+ protocol = pickle.HIGHEST_PROTOCOL
+
def dumps(self, obj):
- return pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
+ return pickle.dumps(obj, self.protocol)
def loads(self, data):
return pickle.loads(data)
diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py
index d782f2191d..76aff9c582 100644
--- a/django/core/cache/backends/db.py
+++ b/django/core/cache/backends/db.py
@@ -46,6 +46,8 @@ class DatabaseCache(BaseDatabaseCache):
# conversion and adaptation infrastructure is then used to avoid comparing
# aware and naive datetimes accidentally.
+ pickle_protocol = pickle.HIGHEST_PROTOCOL
+
def get(self, key, default=None, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
@@ -130,7 +132,7 @@ class DatabaseCache(BaseDatabaseCache):
exp = exp.replace(microsecond=0)
if num > self._max_entries:
self._cull(db, cursor, now)
- pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
+ pickled = pickle.dumps(value, self.pickle_protocol)
# The DB column is expecting a string, so make sure the value is a
# string, not bytes. Refs #19274.
b64encoded = base64.b64encode(pickled).decode('latin1')
diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
index 4ea7bca050..ca8b006577 100644
--- a/django/core/cache/backends/filebased.py
+++ b/django/core/cache/backends/filebased.py
@@ -15,6 +15,7 @@ from django.core.files.move import file_move_safe
class FileBasedCache(BaseCache):
cache_suffix = '.djcache'
+ pickle_protocol = pickle.HIGHEST_PROTOCOL
def __init__(self, dir, params):
super().__init__(params)
@@ -39,8 +40,8 @@ class FileBasedCache(BaseCache):
def _write_content(self, file, timeout, value):
expiry = self.get_backend_timeout(timeout)
- file.write(pickle.dumps(expiry, pickle.HIGHEST_PROTOCOL))
- file.write(zlib.compress(pickle.dumps(value, pickle.HIGHEST_PROTOCOL)))
+ file.write(pickle.dumps(expiry, self.pickle_protocol))
+ file.write(zlib.compress(pickle.dumps(value, self.pickle_protocol)))
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
self._createdir() # Cache dir can be deleted at any time.
diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
index 093144f04a..6058bf5f7e 100644
--- a/django/core/cache/backends/locmem.py
+++ b/django/core/cache/backends/locmem.py
@@ -14,6 +14,8 @@ _locks = {}
class LocMemCache(BaseCache):
+ pickle_protocol = pickle.HIGHEST_PROTOCOL
+
def __init__(self, name, params):
super().__init__(params)
self._cache = _caches.setdefault(name, OrderedDict())
@@ -23,7 +25,7 @@ class LocMemCache(BaseCache):
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
- pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
+ pickled = pickle.dumps(value, self.pickle_protocol)
with self._lock:
if self._has_expired(key):
self._set(key, pickled, timeout)
@@ -51,7 +53,7 @@ class LocMemCache(BaseCache):
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
- pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
+ pickled = pickle.dumps(value, self.pickle_protocol)
with self._lock:
self._set(key, pickled, timeout)
@@ -73,7 +75,7 @@ class LocMemCache(BaseCache):
pickled = self._cache[key]
value = pickle.loads(pickled)
new_value = value + delta
- pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
+ pickled = pickle.dumps(new_value, self.pickle_protocol)
self._cache[key] = pickled
self._cache.move_to_end(key, last=False)
return new_value