summaryrefslogtreecommitdiff
path: root/redis
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2017-11-02 06:30:07 -0700
committerJon Dufresne <jon.dufresne@gmail.com>2018-11-03 03:21:49 -0700
commit5089cc275e6077a070017587001c22e490995d47 (patch)
treeeb8e52047d04f36f1a9562fa47e0a853ffab79eb /redis
parent7a0e4c7f99463c79b456a48837b5687072e1485d (diff)
downloadredis-py-5089cc275e6077a070017587001c22e490995d47.tar.gz
Use unicode literals throughout project
Remove workaround for handling unicode with older Pythons.
Diffstat (limited to 'redis')
-rw-r--r--redis/_compat.py12
-rwxr-xr-xredis/client.py14
-rwxr-xr-xredis/connection.py48
-rw-r--r--redis/exceptions.py11
-rw-r--r--redis/lock.py3
5 files changed, 25 insertions, 63 deletions
diff --git a/redis/_compat.py b/redis/_compat.py
index 310611b..1ad0acc 100644
--- a/redis/_compat.py
+++ b/redis/_compat.py
@@ -109,12 +109,6 @@ if sys.version_info[0] < 3:
def nativestr(x):
return x if isinstance(x, str) else x.encode('utf-8', 'replace')
- def u(x):
- return x.decode()
-
- def b(x):
- return x
-
def next(x):
return x.next()
@@ -147,12 +141,6 @@ else:
def nativestr(x):
return x if isinstance(x, str) else x.decode('utf-8', 'replace')
- def u(x):
- return x
-
- def b(x):
- return x.encode('latin-1') if not isinstance(x, bytes) else x
-
next = next
unichr = chr
imap = map
diff --git a/redis/client.py b/redis/client.py
index 6e66a31..97af09a 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals
from itertools import chain
import datetime
import sys
@@ -6,9 +7,8 @@ import time
import threading
import time as mod_time
import hashlib
-from redis._compat import (b, basestring, bytes, imap, iteritems, iterkeys,
- itervalues, izip, long, nativestr, unicode,
- safe_unicode)
+from redis._compat import (basestring, bytes, imap, iteritems, iterkeys,
+ itervalues, izip, long, nativestr, safe_unicode)
from redis.connection import (ConnectionPool, UnixDomainSocketConnection,
SSLConnection, Token)
from redis.lock import Lock, LuaLock
@@ -24,7 +24,7 @@ from redis.exceptions import (
WatchError,
)
-SYM_EMPTY = b('')
+SYM_EMPTY = b''
def list_or_args(keys, args):
@@ -334,7 +334,7 @@ def parse_slowlog_get(response, **options):
'id': item[0],
'start_time': int(item[1]),
'duration': int(item[2]),
- 'command': b(' ').join(item[3])
+ 'command': b' '.join(item[3])
} for item in response]
@@ -3292,8 +3292,8 @@ class BasePipeline(object):
raise r
def annotate_exception(self, exception, number, command):
- cmd = safe_unicode(' ').join(imap(safe_unicode, command))
- msg = unicode('Command # %d (%s) of pipeline caused error: %s') % (
+ cmd = ' '.join(imap(safe_unicode, command))
+ msg = 'Command # %d (%s) of pipeline caused error: %s' % (
number, cmd, safe_unicode(exception.args[0]))
exception.args = (msg,) + exception.args[1:]
diff --git a/redis/connection.py b/redis/connection.py
index 1e8b86c..d473753 100755
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals
from distutils.version import StrictVersion
from itertools import chain
import io
@@ -13,7 +14,7 @@ try:
except ImportError:
ssl_available = False
-from redis._compat import (b, xrange, imap, byte_to_chr, unicode, bytes, long,
+from redis._compat import (xrange, imap, byte_to_chr, unicode, bytes, long,
nativestr, basestring, iteritems,
LifoQueue, Empty, Full, urlparse, parse_qs,
recv, recv_into, select, unquote)
@@ -45,16 +46,14 @@ if HIREDIS_AVAILABLE:
warnings.warn(msg)
HIREDIS_USE_BYTE_BUFFER = True
- # only use byte buffer if hiredis supports it and the Python version
- # is >= 2.7
- if not HIREDIS_SUPPORTS_BYTE_BUFFER or (
- sys.version_info[0] == 2 and sys.version_info[1] < 7):
+ # only use byte buffer if hiredis supports it
+ if not HIREDIS_SUPPORTS_BYTE_BUFFER:
HIREDIS_USE_BYTE_BUFFER = False
-SYM_STAR = b('*')
-SYM_DOLLAR = b('$')
-SYM_CRLF = b('\r\n')
-SYM_EMPTY = b('')
+SYM_STAR = b'*'
+SYM_DOLLAR = b'$'
+SYM_CRLF = b'\r\n'
+SYM_EMPTY = b''
SERVER_CLOSED_CONNECTION_ERROR = "Connection closed by server."
@@ -85,7 +84,7 @@ class Token(object):
if isinstance(value, Token):
value = value.value
self.value = value
- self.encoded_value = b(value)
+ self.encoded_value = value.encode()
def __repr__(self):
return self.value
@@ -109,9 +108,9 @@ class Encoder(object):
elif isinstance(value, bytes):
return value
elif isinstance(value, (int, long)):
- value = b(str(value))
+ value = str(value).encode()
elif isinstance(value, float):
- value = b(repr(value))
+ 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)
@@ -645,21 +644,21 @@ class Connection(object):
else:
args = (Token.get_token(command),) + args[1:]
- buff = SYM_EMPTY.join(
- (SYM_STAR, b(str(len(args))), SYM_CRLF))
+ buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
for arg in imap(self.encoder.encode, args):
# to avoid large string mallocs, chunk the command into the
# output list if we're sending large values
if len(buff) > 6000 or len(arg) > 6000:
buff = SYM_EMPTY.join(
- (buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF))
+ (buff, SYM_DOLLAR, str(len(arg)).encode(), SYM_CRLF))
output.append(buff)
output.append(arg)
buff = SYM_CRLF
else:
- buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))),
- SYM_CRLF, arg, SYM_CRLF))
+ buff = SYM_EMPTY.join(
+ (buff, SYM_DOLLAR, str(len(arg)).encode(),
+ SYM_CRLF, arg, SYM_CRLF))
output.append(buff)
return output
@@ -832,23 +831,10 @@ class ConnectionPool(object):
querystring arguments always win.
"""
- url_string = url
url = urlparse(url)
- qs = ''
-
- # in python2.6, custom URL schemes don't recognize querystring values
- # they're left as part of the url.path.
- if '?' in url.path and not url.query:
- # chop the querystring including the ? off the end of the url
- # and reparse it.
- qs = url.path.split('?', 1)[1]
- url = urlparse(url_string[:-(len(qs) + 1)])
- else:
- qs = url.query
-
url_options = {}
- for name, value in iteritems(parse_qs(qs)):
+ for name, value in iteritems(parse_qs(url.query)):
if value and len(value) > 0:
parser = URL_QUERY_ARGUMENT_PARSERS.get(name)
if parser:
diff --git a/redis/exceptions.py b/redis/exceptions.py
index a8518c7..44ab6f7 100644
--- a/redis/exceptions.py
+++ b/redis/exceptions.py
@@ -1,21 +1,10 @@
"Core exceptions raised by the Redis client"
-from redis._compat import unicode
class RedisError(Exception):
pass
-# python 2.5 doesn't implement Exception.__unicode__. Add it here to all
-# our exception types
-if not hasattr(RedisError, '__unicode__'):
- def __unicode__(self):
- if isinstance(self.args[0], unicode):
- return self.args[0]
- return unicode(self.args[0])
- RedisError.__unicode__ = __unicode__
-
-
class AuthenticationError(RedisError):
pass
diff --git a/redis/lock.py b/redis/lock.py
index bc7e850..920e0bf 100644
--- a/redis/lock.py
+++ b/redis/lock.py
@@ -3,7 +3,6 @@ import time as mod_time
import uuid
from redis.exceptions import LockError, WatchError
from redis.utils import dummy
-from redis._compat import b
class Lock(object):
@@ -99,7 +98,7 @@ class Lock(object):
wait trying to acquire the lock.
"""
sleep = self.sleep
- token = b(uuid.uuid1().hex)
+ token = uuid.uuid1().hex.encode()
if blocking is None:
blocking = self.blocking
if blocking_timeout is None: