diff options
author | Nicolas NoƩ <nicolas@niconoe.eu> | 2017-11-14 15:01:07 -0500 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2017-11-14 15:14:37 -0500 |
commit | e1f72daabc011b83a8debb86e9e2b7ff6307a41c (patch) | |
tree | 081e3a828aaa7fea71d9ef84343e9264aead6de7 /memcache.py | |
parent | cc94e72f629a7145158def6a363ff51e56dee022 (diff) | |
download | python-memcached-e1f72daabc011b83a8debb86e9e2b7ff6307a41c.tar.gz |
Fix #79, #80 -- Fix storing non-ASCII values on Python 2 and binary values on Python 3
Diffstat (limited to 'memcache.py')
-rw-r--r-- | memcache.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/memcache.py b/memcache.py index cf2a89a..efb8bff 100644 --- a/memcache.py +++ b/memcache.py @@ -134,6 +134,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. @@ -955,11 +956,16 @@ class Client(threading.local): 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. + val_type = type(val) + if val_type == six.binary_type: pass - elif isinstance(val, six.text_type): + elif val_type == six.text_type: + flags |= Client._FLAG_TEXT val = val.encode('utf-8') - elif isinstance(val, int): + elif val_type == int: flags |= Client._FLAG_INTEGER val = '%d' % val if six.PY3: @@ -1250,13 +1256,11 @@ class Client(threading.local): if flags & Client._FLAG_COMPRESSED: buf = self.decompressor(buf) flags &= ~Client._FLAG_COMPRESSED - if flags == 0: - # Bare string - if six.PY3: - val = buf.decode('utf8') - else: - val = buf + # Bare bytes + val = buf + elif flags & Client._FLAG_TEXT: + val = buf.decode('utf-8') elif flags & Client._FLAG_INTEGER: val = int(buf) elif flags & Client._FLAG_LONG: |