diff options
-rw-r--r-- | pymemcache/serde.py | 15 | ||||
-rw-r--r-- | pymemcache/test/test_serde.py | 24 | ||||
-rw-r--r-- | tox.ini | 9 |
3 files changed, 39 insertions, 9 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'}) @@ -1,5 +1,5 @@ [tox] -envlist = py26, py27, pypy, pypy3, py33, py34, docs, flake8 +envlist = py26, py27, pypy, pypy3, py33, py34, docs, py27-flake8, py34-flake8 [testenv] commands = @@ -7,7 +7,12 @@ commands = pip install -e . py.test {posargs:pymemcache/test/} -[testenv:flake8] +[testenv:py27-flake8] +commands = + pip install flake8 + flake8 pymemcache/ + +[testenv:py34-flake8] commands = pip install flake8 flake8 pymemcache/ |