summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pymemcache/client/base.py8
-rw-r--r--pymemcache/serde.py15
-rw-r--r--pymemcache/test/test_serde.py24
-rw-r--r--setup.cfg6
-rw-r--r--tox.ini9
5 files changed, 47 insertions, 15 deletions
diff --git a/pymemcache/client/base.py b/pymemcache/client/base.py
index 841ac46..fc23ccb 100644
--- a/pymemcache/client/base.py
+++ b/pymemcache/client/base.py
@@ -86,12 +86,14 @@ def _check_key(key, key_prefix=b''):
try:
key = key.encode('ascii')
except UnicodeEncodeError:
- raise MemcacheIllegalInputError("No ascii key: %r" % (key,))
+ raise MemcacheIllegalInputError("Non-ASCII key: '%r'" % (key,))
key = key_prefix + key
if b' ' in key or b'\n' in key:
- raise MemcacheIllegalInputError("Key contains spaces: %r" % (key,))
+ raise MemcacheIllegalInputError(
+ "Key contains space and/or newline: '%r'" % (key,)
+ )
if len(key) > 250:
- raise MemcacheIllegalInputError("Key is too long: %r" % (key,))
+ raise MemcacheIllegalInputError("Key is too long: '%r'" % (key,))
return key
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'})
diff --git a/setup.cfg b/setup.cfg
index 1ff4e7b..8c2b13c 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -6,9 +6,9 @@ tag = True
[bdist_wheel]
universal = true
-[pytest]
+[tool:pytest]
norecursedirs = build docs/_build *.egg .tox *.venv
-addopts =
+addopts =
--verbose
--tb=short
--capture=no
@@ -17,7 +17,7 @@ addopts =
-rfEsxX
--cov=pymemcache --cov-report=xml --cov-report=term-missing
-m unit
-markers =
+markers =
unit
integration
benchmark
diff --git a/tox.ini b/tox.ini
index 4ee7b65..5b067dc 100644
--- a/tox.ini
+++ b/tox.ini
@@ -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/