diff --git a/django/contrib/sessions/serializers.py b/django/contrib/sessions/serializers.py index 1df6d9c49e8..6ac4aab6a44 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 d782f2191d1..76aff9c582a 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 4ea7bca0505..ca8b0065771 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 093144f04a1..6058bf5f7e1 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