Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python 2.7 Unicode compatible. #100

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Client(threading.local):
_FLAG_INTEGER = 1 << 1
_FLAG_LONG = 1 << 2
_FLAG_COMPRESSED = 1 << 3
_FLAG_TEXT = 1 << 4

_SERVER_RETRIES = 10 # how many times to try finding a free server.

Expand Down Expand Up @@ -968,18 +969,23 @@ def _val_to_store_info(self, val, min_compress_len):
the new value itself.
"""
flags = 0
if isinstance(val, six.binary_type):
# Check against the exact type, rather than using isinstance, so that
# subclasses of native types (such as markup-safe strings) are pickled
# and restored as instances of the correct class.
type_ = type(val)
if type_ == six.binary_type:
pass
elif isinstance(val, six.text_type):
elif type_ == six.text_type:
flags |= Client._FLAG_TEXT
val = val.encode('utf-8')
elif isinstance(val, int):
elif type_ == int:
flags |= Client._FLAG_INTEGER
val = '%d' % val
if six.PY3:
val = val.encode('ascii')
# force no attempt to compress this silly string.
min_compress_len = 0
elif six.PY2 and isinstance(val, long):
elif six.PY2 and type_ == long:
flags |= Client._FLAG_LONG
val = str(val)
if six.PY3:
Expand Down Expand Up @@ -1266,11 +1272,10 @@ def _recv_value(self, server, flags, rlen):
flags &= ~Client._FLAG_COMPRESSED

if flags == 0:
# Bare string
if six.PY3:
val = buf.decode('utf8')
else:
# Bare bytes
val = buf
elif flags & Client._FLAG_TEXT:
val = buf.decode('utf8')
elif flags & Client._FLAG_INTEGER:
val = int(buf)
elif flags & Client._FLAG_LONG:
Expand Down