#33361: Redis cache backend doesn't allow storing bool values
-------------------------------------+------------------------------------
     Reporter:  Jeremy Lainé         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Core (Cache system)  |                  Version:  4.0
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

 * cc: Nick Pope, Daniyal Abbasi (added)
 * stage:  Unreviewed => Accepted
 * severity:  Normal => Release blocker


Old description:

> The following code raises an exception:  `redis.exceptions.DataError:
> Invalid input of type: 'bool'. Convert to a bytes, string, int or float
> first.`
>
> {{{#!python
> from django.core.cache import cache
> cache.set("foo", True)
> }}}
>
> This contradicts the documentation which states that any data type
> supported by pickle can be stored to the cache.
>
> The root cause seems to be because instances of `int` are special-cased
> and not send through pickle, but redis-py cannot send booleans to redis:
>
> https://github.com/django/django/blob/2f73e5406d54cb8945e187eff302a3a3373350be/django/core/cache/backends/redis.py#L14
>
> What was the rationale behind the `int` special-case? `django-redis` for
> instance consistently sends all data through pickle.

New description:

 The following code raises an exception:  `redis.exceptions.DataError:
 Invalid input of type: 'bool'. Convert to a bytes, string, int or float
 first.`

 {{{#!python
 from django.core.cache import cache
 cache.set("foo", True)
 }}}

 This contradicts the documentation which states that any data type
 supported by pickle can be stored to the cache.

 The root cause seems to be because instances of `int` are special-cased
 and not send through pickle, but redis-py cannot send booleans to redis:

 
https://github.com/django/django/blob/2f73e5406d54cb8945e187eff302a3a3373350be/django/core/cache/backends/redis.py#L14

 What was the rationale behind the `int` special-case? `django-redis` for
 instance consistently sends all data through pickle.

--

Comment:

 Thanks for report! It should be enough to special-case `bool`, e.g.
 {{{#!diff
 diff --git a/django/core/cache/backends/redis.py
 b/django/core/cache/backends/redis.py
 index 16556b1ded..bbb0e0320d 100644
 --- a/django/core/cache/backends/redis.py
 +++ b/django/core/cache/backends/redis.py
 @@ -11,7 +11,7 @@ from django.utils.module_loading import import_string

  class RedisSerializer(PickleSerializer):
      def dumps(self, obj):
 -        if isinstance(obj, int):
 +        if isinstance(obj, int) and not isinstance(obj, bool):
              return obj
          return super().dumps(obj)

 }}}

 Would you like to prepare a patch?

 > What was the rationale behind the `int` special-case? `django-redis` for
 instance consistently sends all data through pickle.

 We do this to have better atomicity of `incr()` and `decr()` operations
 (see [https://github.com/django/django/pull/14437#issuecomment-915046037
 discussion]).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33361#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.a683e5223f6b3e1271380310d9802bca%40djangoproject.com.

Reply via email to