summaryrefslogtreecommitdiff
path: root/redis/connection.py
diff options
context:
space:
mode:
authorTim Savage <tim.savage@poweredbypenguins.org>2016-03-29 18:15:34 +1100
committerTim Savage <tim.savage@poweredbypenguins.org>2016-03-29 18:15:34 +1100
commit8e6f655d069fd3b31de63ae7f9ef967a7bf6de14 (patch)
tree14510ac419a08d6ee2e966592498027e381e32d8 /redis/connection.py
parentb40875d553ab6d6db69e64eef134e5fac652b033 (diff)
downloadredis-py-8e6f655d069fd3b31de63ae7f9ef967a7bf6de14.tar.gz
Extend ConnectionPool.to_url to parse querystring arguments to correct type.
Previously if a value for socket_timeout was supplied as part fo the URL an error would be raised when a socket was created with an invalid type, this change fixes that by parsing `socket_timeout`, `socket_connect_timeout` to float values. In addition the boolean values `socket_keepalive` and `retry_on_timeout` are parsed to bool types taking into account the usage of True/False, Yes/No strings.
Diffstat (limited to 'redis/connection.py')
-rwxr-xr-xredis/connection.py33
1 files changed, 30 insertions, 3 deletions
diff --git a/redis/connection.py b/redis/connection.py
index 91730ea..fb90e93 100755
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -735,6 +735,22 @@ class UnixDomainSocketConnection(Connection):
(exception.args[0], self.path, exception.args[1])
+def to_bool(value):
+ if value is None or value == '':
+ return None
+ if isinstance(value, basestring) and value.upper() in ('0', 'F', 'FALSE', 'N', 'NO'):
+ return False
+ return bool(value)
+
+
+URL_QUERY_PARAMETER_TYPES = {
+ 'socket_timeout': float,
+ 'socket_connect_timeout': float,
+ 'socket_keepalive': to_bool,
+ 'retry_on_timeout': to_bool
+}
+
+
class ConnectionPool(object):
"Generic connection pool"
@classmethod
@@ -769,8 +785,13 @@ class ConnectionPool(object):
``path``, and ``password`` components.
Any additional querystring arguments and keyword arguments will be
- passed along to the ConnectionPool class's initializer. In the case
- of conflicting arguments, querystring arguments always win.
+ passed along to the ConnectionPool class's initializer. The querystring
+ arguments ``socket_connect_timeout`` and ``socket_timeout`` if supplied
+ are parsed as float values. The arguments ``socket_keepalive`` and
+ ``retry_on_timeout`` are parsed to boolean values that accept
+ True/False, Yes/No values to indicate state. Invalid types cause a
+ ``UserWarning`` to be raised. In the case of conflicting arguments,
+ querystring arguments always win.
"""
url_string = url
url = urlparse(url)
@@ -790,7 +811,13 @@ class ConnectionPool(object):
for name, value in iteritems(parse_qs(qs)):
if value and len(value) > 0:
- url_options[name] = value[0]
+ if name in URL_QUERY_PARAMETER_TYPES:
+ try:
+ url_options[name] = URL_QUERY_PARAMETER_TYPES[name](value[0])
+ except (TypeError, ValueError):
+ warnings.warn(UserWarning("Invalid value for `%s` in connection URL." % name))
+ else:
+ url_options[name] = value[0]
if decode_components:
password = unquote(url.password) if url.password else None