From 086bfb58512162437d11e6693a4060c181bc04ff Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sat, 14 Apr 2018 14:33:37 +0300 Subject: [PATCH] fix redis sessions and redis for python3 --- gluon/contrib/redis_cache.py | 4 ++-- gluon/contrib/redis_session.py | 12 +++++++----- gluon/contrib/redis_utils.py | 4 ++-- gluon/globals.py | 6 ++++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/gluon/contrib/redis_cache.py b/gluon/contrib/redis_cache.py index 05aa53fc..110360eb 100644 --- a/gluon/contrib/redis_cache.py +++ b/gluon/contrib/redis_cache.py @@ -10,7 +10,7 @@ except: import time import re import logging -import thread +from threading import Lock import random from gluon import current from gluon.cache import CacheAbstract @@ -19,7 +19,7 @@ from gluon.contrib.redis_utils import register_release_lock, RConnectionError logger = logging.getLogger("web2py.cache.redis") -locker = thread.allocate_lock() +locker = Lock() def RedisCache(redis_conn=None, debug=False, with_lock=False, fail_gracefully=False, db=None): diff --git a/gluon/contrib/redis_session.py b/gluon/contrib/redis_session.py index 1273e6de..b26b6b21 100644 --- a/gluon/contrib/redis_session.py +++ b/gluon/contrib/redis_session.py @@ -8,7 +8,7 @@ Redis-backed sessions """ import logging -import thread +from threading import Lock from gluon import current from gluon.storage import Storage from gluon.contrib.redis_utils import acquire_lock, release_lock @@ -16,7 +16,7 @@ from gluon.contrib.redis_utils import register_release_lock logger = logging.getLogger("web2py.session.redis") -locker = thread.allocate_lock() +locker = Lock() def RedisSession(redis_conn, session_expiry=False, with_lock=False, db=None): @@ -43,7 +43,7 @@ def RedisSession(redis_conn, session_expiry=False, with_lock=False, db=None): try: instance_name = 'redis_instance_' + current.request.application if not hasattr(RedisSession, instance_name): - setattr(RedisSession, instance_name, + setattr(RedisSession, instance_name, RedisClient(redis_conn, session_expiry=session_expiry, with_lock=with_lock)) return getattr(RedisSession, instance_name) finally: @@ -185,8 +185,10 @@ class MockQuery(object): if rtn: if self.unique_key: # make sure the id and unique_key are correct - if rtn['unique_key'] == self.unique_key: - rtn['update_record'] = self.update # update record support + if not isinstance(self.unique_key, bytes): + self.unique_key = bytes(self.unique_key, 'utf8') + if rtn[b'unique_key'] == self.unique_key: + rtn[b'update_record'] = self.update # update record support else: rtn = None return [Storage(rtn)] if rtn else [] diff --git a/gluon/contrib/redis_utils.py b/gluon/contrib/redis_utils.py index 217ca952..8dc7b2d3 100644 --- a/gluon/contrib/redis_utils.py +++ b/gluon/contrib/redis_utils.py @@ -11,7 +11,7 @@ to ensure compatibility with another - similar - library """ import logging -import thread +from threading import Lock import time from gluon import current @@ -26,7 +26,7 @@ except ImportError: raise RuntimeError('Needs redis library to work') -locker = thread.allocate_lock() +locker = Lock() def RConn(*args, **vars): diff --git a/gluon/globals.py b/gluon/globals.py index 95e310c6..edf0e504 100644 --- a/gluon/globals.py +++ b/gluon/globals.py @@ -967,7 +967,7 @@ class Session(Storage): if row: # rows[0].update_record(locked=True) # Unpickle the data - session_data = pickle.loads(row.session_data) + session_data = pickle.loads(row[b'session_data']) self.update(session_data) response.session_new = False else: @@ -1049,7 +1049,9 @@ class Session(Storage): if record_id.isdigit() and long(record_id) > 0: new_unique_key = web2py_uuid() row = table(record_id) - if row and row.unique_key == unique_key: + if not isinstance(unique_key, bytes): + unique_key = bytes(unique_key, 'utf8') + if row and row[b'unique_key'] == unique_key: table._db(table.id == record_id).update(unique_key=new_unique_key) else: record_id = None