#602 [frontend] Redis.setex swapped arguments in v3+
Merged 5 years ago by praiskup. Opened 5 years ago by praiskup.
Unknown source redis-sessions  into  master

@@ -8,7 +8,7 @@

  import pickle

  from datetime import timedelta

  from uuid import uuid4

- from redis import Redis

+ from redis import StrictRedis

  from werkzeug.datastructures import CallbackDict

  from flask.sessions import SessionInterface, SessionMixin

  
@@ -28,9 +28,8 @@

      serializer = pickle

      session_class = RedisSession

  

-     def __init__(self, redis=None, prefix='session:'):

-         if redis is None:

-             redis = Redis()

+     def __init__(self, redis, prefix='session:'):

+         assert isinstance(redis, StrictRedis)

          self.redis = redis

          self.prefix = prefix

  
@@ -68,10 +67,7 @@

  

          time_exp = int(redis_exp.total_seconds())

          key_exp = self.prefix + session.sid

-         if isinstance(self.redis, Redis):

-             self.redis.setex(key_exp, val, time_exp)

-         else:

-             self.redis.setex(key_exp, time_exp, val)

+         self.redis.setex(key_exp, time_exp, val)

  

          response.set_cookie(app.session_cookie_name, session.sid,

                              expires=cookie_exp, httponly=True,

To avoid additional version checking/API checking, use StrictRedis
which has consistent setex() API across v2/3.

Per upstream docs:
2.X users that are using the Redis class will have to make
changes if they use any of the following commands:
- SETEX: The argument order has changed. The new order is (name,
time, value).

rebased onto 8d0c4a486b54e17aa9372061e18dc25a9358157d

5 years ago

Until now, I didn't even know, that there is redis_session.py file, so don't take me too seriously on this, but ... can we remove the condition? I can pass my own redis object to the RedisSessionInterface init. If I pass Redis() there, the setext may IMHO fail.

Good point. We IMO pass the strict redis there, so no problem existed before (this pull request only silenced the tests).

So, I'm going to actually require external Redis now, and fail if that is not StrictRedis.

rebased onto a477487e7080006f5abcb0701ec779085419f3f1

5 years ago

Please take another look.

rebased onto b5c39cd5a0d865a18b0363abd0d7c7f3fa8a45aa

5 years ago

We IMO pass the strict redis there
Please take another look.

Then it should be alright. +1

Also, maybe instead of asserting the correct instance and othewise failing, can we possibly re-cast the object to the StrictRedis? I don't insist on this, it is just a thought.

I think that the check just protects us against programmer mistakes (not a user or other bugs, but a clear programmer misuse). This looks like a valid assert() use-case, but I might be wrong.

rebased onto c9836e3

5 years ago

Pull-Request has been merged by praiskup

5 years ago
Metadata