From 0506facd86082f48cc3cc3d3ea253e4ebbcd0ebe Mon Sep 17 00:00:00 2001 From: Paul McMillan Date: Mon, 21 Nov 2011 22:25:49 +0000 Subject: [PATCH] Fixed #16378. Locmem now uses pickle.HIGHEST_PROTOCOL for better compatibility with other hash backends. Thanks aaugustin for the initial patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17136 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/cache/backends/db.py | 3 ++- django/core/cache/backends/locmem.py | 9 ++++++--- docs/releases/1.4.txt | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index 2baa12893e..62ea5c420b 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -102,7 +102,8 @@ class DatabaseCache(BaseDatabaseCache): exp = exp.replace(microsecond=0) if num > self._max_entries: self._cull(db, cursor, now) - encoded = base64.encodestring(pickle.dumps(value, 2)).strip() + pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) + encoded = base64.encodestring(pickled).strip() cursor.execute("SELECT cache_key, expires FROM %s " "WHERE cache_key = %%s" % table, [key]) try: diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py index fa3b4b7f18..7421b9b158 100644 --- a/django/core/cache/backends/locmem.py +++ b/django/core/cache/backends/locmem.py @@ -31,7 +31,8 @@ class LocMemCache(BaseCache): exp = self._expire_info.get(key) if exp is None or exp <= time.time(): try: - self._set(key, pickle.dumps(value), timeout) + pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) + self._set(key, pickled, timeout) return True except pickle.PickleError: pass @@ -49,7 +50,8 @@ class LocMemCache(BaseCache): return default elif exp > time.time(): try: - return pickle.loads(self._cache[key]) + pickled = self._cache[key] + return pickle.loads(pickled) except pickle.PickleError: return default finally: @@ -78,7 +80,8 @@ class LocMemCache(BaseCache): self.validate_key(key) self._lock.writer_enters() try: - self._set(key, pickle.dumps(value), timeout) + pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) + self._set(key, pickled, timeout) except pickle.PickleError: pass finally: diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt index b611626099..9275ac6ffe 100644 --- a/docs/releases/1.4.txt +++ b/docs/releases/1.4.txt @@ -501,6 +501,10 @@ Django 1.4 also includes several smaller improvements worth noting: * Changed the default value for ``httponly`` on session cookies to ``True`` to help reduce the impact of potential XSS attacks. +* Changed the ``locmem`` cache backend to use + ``pickle.HIGHEST_PROTOCOL`` for better compatibility with the other + cache backends. + .. _backwards-incompatible-changes-1.4: Backwards incompatible changes in 1.4