summaryrefslogtreecommitdiff
path: root/redis/connection.py
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2018-11-14 14:53:40 -0800
committerAndy McCurdy <andy@andymccurdy.com>2018-11-14 14:53:40 -0800
commit40025ae073f7b376b496a54491fbb30d964dcb24 (patch)
tree1021807499151f6e53eae56a9eaec642f83037b7 /redis/connection.py
parenta750c7946d41862a38955c35b6928f098911c406 (diff)
downloadredis-py-40025ae073f7b376b496a54491fbb30d964dcb24.tar.gz
only accept bytes, strings, ints, longs and floats as user input
All input sent to Redis is coerced into bytes. This includes key names and values. Prior to this change, redis-py made an effort to cooerce all input into strings by calling str() (Python 3) or unicode() (Python 2). While this works for a handful of types like ints, longs and floats, it fails for other types like bools ('True' or 'False'), None ('None') and many user defined types. Starting with redis-py version 3.0, sending input of any other type is considered an error an a DataError exception will be raised. Fixes #471 Fixes #472 Fixes #321 Fixes #190
Diffstat (limited to 'redis/connection.py')
-rwxr-xr-xredis/connection.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/redis/connection.py b/redis/connection.py
index d473753..3ab9573 100755
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -19,6 +19,7 @@ from redis._compat import (xrange, imap, byte_to_chr, unicode, bytes, long,
LifoQueue, Empty, Full, urlparse, parse_qs,
recv, recv_into, select, unquote)
from redis.exceptions import (
+ DataError,
RedisError,
ConnectionError,
TimeoutError,
@@ -107,13 +108,17 @@ class Encoder(object):
return value.encoded_value
elif isinstance(value, bytes):
return value
- elif isinstance(value, (int, long)):
- value = str(value).encode()
- elif isinstance(value, float):
+ elif isinstance(value, bool):
+ # special case bool since it is a subclass of int
+ raise DataError("Invalid input of type: 'bool'. Convert to a "
+ "byte, string or number first.")
+ elif isinstance(value, (int, long, float)):
value = repr(value).encode()
elif not isinstance(value, basestring):
- # an object we don't know how to deal with. default to unicode()
- value = unicode(value)
+ # a value we don't know how to deal with. throw an error
+ typename = type(value).__name__
+ raise DataError("Invalid input of type: '%s'. Convert to a "
+ "byte, string or number first." % typename)
if isinstance(value, unicode):
value = value.encode(self.encoding, self.encoding_errors)
return value