diff options
author | Nicholas Charriere <nicholascharriere@gmail.com> | 2016-08-22 10:15:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-22 10:15:18 +0200 |
commit | 013b8af3786df65952427564f1d7e103d929ac65 (patch) | |
tree | cebcf4ddfa86309f1fc7b5f24d922e0951982035 /pymemcache | |
parent | 6f6526e71259c6573d7c20325d8342abc09a06d4 (diff) | |
parent | 4bed13f9d3da7b913e4ed5470399674fafa8ad11 (diff) | |
download | pymemcache-013b8af3786df65952427564f1d7e103d929ac65.tar.gz |
Merge pull request #112 from adamchainz/lint_python3
Lint on Python 3 too
Diffstat (limited to 'pymemcache')
-rw-r--r-- | pymemcache/serde.py | 15 | ||||
-rw-r--r-- | pymemcache/test/test_serde.py | 24 |
2 files changed, 32 insertions, 7 deletions
diff --git a/pymemcache/serde.py b/pymemcache/serde.py index c7a00ee..2226349 100644 --- a/pymemcache/serde.py +++ b/pymemcache/serde.py @@ -14,11 +14,12 @@ import logging import pickle +from io import BytesIO try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO + long_type = long # noqa +except NameError: + long_type = None FLAG_PICKLE = 1 << 0 @@ -34,12 +35,12 @@ def python_memcache_serializer(key, value): elif isinstance(value, int): flags |= FLAG_INTEGER value = "%d" % value - elif isinstance(value, long): + elif long_type is not None and isinstance(value, long_type): flags |= FLAG_LONG value = "%d" % value else: flags |= FLAG_PICKLE - output = StringIO() + output = BytesIO() pickler = pickle.Pickler(output, 0) pickler.dump(value) value = output.getvalue() @@ -55,11 +56,11 @@ def python_memcache_deserializer(key, value, flags): return int(value) if flags & FLAG_LONG: - return long(value) + return long_type(value) if flags & FLAG_PICKLE: try: - buf = StringIO(value) + buf = BytesIO(value) unpickler = pickle.Unpickler(buf) return unpickler.load() except Exception: diff --git a/pymemcache/test/test_serde.py b/pymemcache/test/test_serde.py new file mode 100644 index 0000000..9849cdc --- /dev/null +++ b/pymemcache/test/test_serde.py @@ -0,0 +1,24 @@ +from unittest import TestCase + +from pymemcache.serde import (python_memcache_serializer, + python_memcache_deserializer) + + +class TestSerde(TestCase): + + def check(self, value): + serialized, flags = python_memcache_serializer(b'key', value) + deserialized = python_memcache_deserializer(b'key', serialized, flags) + assert deserialized == value + + def test_str(self): + self.check('value') + + def test_int(self): + self.check(1) + + def test_long(self): + self.check(123123123123123123123) + + def test_pickleable(self): + self.check({'a': 'dict'}) |