Renamed serialization.encode/decode -> .dumps, .loads

This commit is contained in:
Ask Solem 2013-09-24 14:31:28 +01:00
parent 74313e93fe
commit aee4345020
3 changed files with 84 additions and 92 deletions

View File

@ -14,7 +14,7 @@ from .connection import maybe_channel, is_connection
from .entity import Exchange, Queue, DELIVERY_MODES
from .exceptions import ContentDisallowed
from .five import int_types, text_t, values
from .serialization import encode, prepare_accept_content
from .serialization import dumps, prepare_accept_content
from .utils import ChannelPromise, maybe_list
__all__ = ['Exchange', 'Queue', 'Producer', 'Consumer']
@ -235,7 +235,7 @@ class Producer(object):
if not content_type:
serializer = serializer or self.serializer
(content_type, content_encoding,
body) = encode(body, serializer=serializer)
body) = dumps(body, serializer=serializer)
else:
# If the programmer doesn't want us to serialize,
# make sure content_encoding is set.

View File

@ -24,7 +24,7 @@ from .five import BytesIO, text_t
from .utils import entrypoints
from .utils.encoding import str_to_bytes, bytes_t
__all__ = ['pickle', 'encode', 'decode', 'register', 'unregister']
__all__ = ['pickle', 'loads', 'dumps', 'register', 'unregister']
SKIP_DECODE = frozenset(['binary', 'ascii-8bit'])
if sys.platform.startswith('java'): # pragma: no cover
@ -116,7 +116,7 @@ class SerializerRegistry(object):
raise SerializerNotInstalled(
'No encoder installed for {0}'.format(name))
def encode(self, data, serializer=None):
def dumps(self, data, serializer=None):
if serializer == 'raw':
return raw_encode(data)
if serializer and not self._encoders.get(serializer):
@ -146,9 +146,10 @@ class SerializerRegistry(object):
payload = encoder(data)
return content_type, content_encoding, payload
encode = dumps # XXX compat
def decode(self, data, content_type, content_encoding,
accept=None, force=False):
def loads(self, data, content_type, content_encoding,
accept=None, force=False):
if accept is not None:
if content_type not in accept:
raise self._for_untrusted_content(content_type, 'untrusted')
@ -166,10 +167,11 @@ class SerializerRegistry(object):
not isinstance(data, text_t):
return _decode(data, content_encoding)
return data
decode = loads # XXX compat
def _for_untrusted_content(self, ctype, why):
return ContentDisallowed(
'Refusing to decode {0} content of type {1}'.format(
'Refusing to deserialize {0} content of type {1}'.format(
why,
parenthesize_alias(self.type_to_name.get(ctype, ctype), ctype),
),
@ -181,7 +183,7 @@ registry = SerializerRegistry()
"""
.. function:: encode(data, serializer=default_serializer)
.. function:: dumps(data, serializer=default_serializer)
Serialize a data structure into a string suitable for sending
as an AMQP message body.
@ -210,12 +212,12 @@ registry = SerializerRegistry()
:raises SerializerNotInstalled: If the serialization method
requested is not available.
"""
encode = registry.encode
dumps = encode = registry.encode # XXX encode is a compat alias
"""
.. function:: decode(data, content_type, content_encoding):
.. function:: loads(data, content_type, content_encoding):
Deserialize a data stream as serialized using `encode`
Deserialize a data stream as serialized using `dumps`
based on `content_type`.
:param data: The message data to deserialize.
@ -229,7 +231,7 @@ encode = registry.encode
:returns: The unserialized data.
"""
decode = registry.decode
loads = decode = registry.decode # XXX decode is a compat alias
"""
@ -284,14 +286,14 @@ def raw_encode(data):
def register_json():
"""Register a encoder/decoder for JSON serialization."""
from anyjson import loads, dumps
from anyjson import loads as json_loads, dumps as json_dumps
def _loads(obj):
if isinstance(obj, bytes_t):
obj = obj.decode()
return loads(obj)
return json_loads(obj)
registry.register('json', dumps, _loads,
registry.register('json', json_dumps, _loads,
content_type='application/json',
content_encoding='utf-8')
@ -329,10 +331,10 @@ def register_pickle():
"""The fastest serialization method, but restricts
you to python clients."""
def dumps(obj, dumper=pickle.dumps):
def pickle_dumps(obj, dumper=pickle.dumps):
return dumper(obj, protocol=pickle_protocol)
registry.register('pickle', dumps, unpickle,
registry.register('pickle', pickle_dumps, unpickle,
content_type='application/x-python-serialize',
content_encoding='binary')
@ -341,13 +343,13 @@ def register_msgpack():
"""See http://msgpack.sourceforge.net/"""
try:
try:
from msgpack import packb as dumps, unpackb
loads = lambda s: unpackb(s, encoding='utf-8')
from msgpack import packb as pack, unpackb
unpack = lambda s: unpackb(s, encoding='utf-8')
except ImportError:
# msgpack < 0.2.0 and Python 2.5
from msgpack import packs as dumps, unpacks as loads # noqa
from msgpack import packs as pack, unpacks as unpack # noqa
registry.register(
'msgpack', dumps, loads,
'msgpack', pack, unpack,
content_type='application/x-msgpack',
content_encoding='binary')
except (ImportError, ValueError):

View File

@ -12,7 +12,7 @@ from kombu.five import text_t, bytes_t
from kombu.serialization import (
registry, register, SerializerNotInstalled,
raw_encode, register_yaml, register_msgpack,
decode, pickle, pickle_protocol,
dumps, loads, pickle, pickle_protocol,
unregister, register_pickle, enable_insecure_serializers,
disable_insecure_serializers,
)
@ -99,74 +99,66 @@ class test_Serialization(Case):
registry.enable('application/json')
self.assertNotIn('application/json', registry._disabled_content_types)
def test_decode_when_disabled(self):
def test_loads_when_disabled(self):
disabled = registry._disabled_content_types
try:
registry.disable('testS')
with self.assertRaises(SerializerNotInstalled):
registry.decode(
'xxd', 'application/testS', 'utf-8', force=False,
)
loads('xxd', 'application/testS', 'utf-8', force=False)
ret = registry.decode(
'xxd', 'application/testS', 'utf-8', force=True,
)
ret = loads('xxd', 'application/testS', 'utf-8', force=True)
self.assertEqual(ret, 'decoded')
finally:
disabled.clear()
def test_decode_when_data_is_None(self):
registry.decode(None, 'application/testS', 'utf-8')
def test_loads_when_data_is_None(self):
loads(None, 'application/testS', 'utf-8')
def test_content_type_decoding(self):
self.assertEqual(
unicode_string,
registry.decode(unicode_string_as_utf8,
content_type='plain/text',
content_encoding='utf-8'),
loads(unicode_string_as_utf8,
content_type='plain/text', content_encoding='utf-8'),
)
self.assertEqual(
latin_string,
registry.decode(latin_string_as_latin1,
content_type='application/data',
content_encoding='latin-1'),
loads(latin_string_as_latin1,
content_type='application/data', content_encoding='latin-1'),
)
def test_content_type_binary(self):
self.assertIsInstance(
registry.decode(unicode_string_as_utf8,
content_type='application/data',
content_encoding='binary'),
loads(unicode_string_as_utf8,
content_type='application/data', content_encoding='binary'),
bytes_t,
)
self.assertEqual(
unicode_string_as_utf8,
registry.decode(unicode_string_as_utf8,
content_type='application/data',
content_encoding='binary'),
loads(unicode_string_as_utf8,
content_type='application/data', content_encoding='binary'),
)
def test_content_type_encoding(self):
# Using the 'raw' serializer
self.assertEqual(
unicode_string_as_utf8,
registry.encode(unicode_string, serializer='raw')[-1],
dumps(unicode_string, serializer='raw')[-1],
)
self.assertEqual(
latin_string_as_utf8,
registry.encode(latin_string, serializer='raw')[-1],
dumps(latin_string, serializer='raw')[-1],
)
# And again w/o a specific serializer to check the
# code where we force unicode objects into a string.
self.assertEqual(
unicode_string_as_utf8,
registry.encode(unicode_string)[-1],
dumps(unicode_string)[-1],
)
self.assertEqual(
latin_string_as_utf8,
registry.encode(latin_string)[-1],
dumps(latin_string)[-1],
)
def test_enable_insecure_serializers(self):
@ -194,22 +186,21 @@ class test_Serialization(Case):
call('pickle'), call('yaml'), call('doomsday')
])
def test_json_decode(self):
def test_json_loads(self):
self.assertEqual(
py_data,
registry.decode(json_data,
content_type='application/json',
content_encoding='utf-8'),
loads(json_data,
content_type='application/json', content_encoding='utf-8'),
)
def test_json_encode(self):
def test_json_dumps(self):
self.assertEqual(
registry.decode(
registry.encode(py_data, serializer='json')[-1],
loads(
dumps(py_data, serializer='json')[-1],
content_type='application/json',
content_encoding='utf-8',
),
registry.decode(
loads(
json_data,
content_type='application/json',
content_encoding='utf-8',
@ -217,11 +208,11 @@ class test_Serialization(Case):
)
@skip_if_not_module('msgpack', (ImportError, ValueError))
def test_msgpack_decode(self):
def test_msgpack_loads(self):
register_msgpack()
res = registry.decode(msgpack_data,
content_type='application/x-msgpack',
content_encoding='binary')
res = loads(msgpack_data,
content_type='application/x-msgpack',
content_encoding='binary')
if sys.version_info[0] < 3:
for k, v in res.items():
if isinstance(v, text_t):
@ -234,15 +225,15 @@ class test_Serialization(Case):
)
@skip_if_not_module('msgpack', (ImportError, ValueError))
def test_msgpack_encode(self):
def test_msgpack_dumps(self):
register_msgpack()
self.assertEqual(
registry.decode(
registry.encode(msgpack_py_data, serializer='msgpack')[-1],
loads(
dumps(msgpack_py_data, serializer='msgpack')[-1],
content_type='application/x-msgpack',
content_encoding='binary',
),
registry.decode(
loads(
msgpack_data,
content_type='application/x-msgpack',
content_encoding='binary',
@ -250,43 +241,43 @@ class test_Serialization(Case):
)
@skip_if_not_module('yaml')
def test_yaml_decode(self):
def test_yaml_loads(self):
register_yaml()
self.assertEqual(
py_data,
registry.decode(yaml_data,
content_type='application/x-yaml',
content_encoding='utf-8'),
loads(yaml_data,
content_type='application/x-yaml',
content_encoding='utf-8'),
)
@skip_if_not_module('yaml')
def test_yaml_encode(self):
def test_yaml_dumps(self):
register_yaml()
self.assertEqual(
registry.decode(
registry.encode(py_data, serializer='yaml')[-1],
loads(
dumps(py_data, serializer='yaml')[-1],
content_type='application/x-yaml',
content_encoding='utf-8',
),
registry.decode(
loads(
yaml_data,
content_type='application/x-yaml',
content_encoding='utf-8',
),
)
def test_pickle_decode(self):
def test_pickle_loads(self):
self.assertEqual(
py_data,
registry.decode(pickle_data,
content_type='application/x-python-serialize',
content_encoding='binary'),
loads(pickle_data,
content_type='application/x-python-serialize',
content_encoding='binary'),
)
def test_pickle_encode(self):
def test_pickle_dumps(self):
self.assertEqual(
pickle.loads(pickle_data),
pickle.loads(registry.encode(py_data, serializer='pickle')[-1]),
pickle.loads(dumps(py_data, serializer='pickle')[-1]),
)
def test_register(self):
@ -295,35 +286,34 @@ class test_Serialization(Case):
def test_unregister(self):
with self.assertRaises(SerializerNotInstalled):
unregister('nonexisting')
registry.encode('foo', serializer='pickle')
dumps('foo', serializer='pickle')
unregister('pickle')
with self.assertRaises(SerializerNotInstalled):
registry.encode('foo', serializer='pickle')
dumps('foo', serializer='pickle')
register_pickle()
def test_set_default_serializer_missing(self):
with self.assertRaises(SerializerNotInstalled):
registry._set_default_serializer('nonexisting')
def test_encode_missing(self):
def test_dumps_missing(self):
with self.assertRaises(SerializerNotInstalled):
registry.encode('foo', serializer='nonexisting')
dumps('foo', serializer='nonexisting')
def test_encode__no_serializer(self):
ctyp, cenc, data = registry.encode(str_to_bytes('foo'))
def test_dumps__no_serializer(self):
ctyp, cenc, data = dumps(str_to_bytes('foo'))
self.assertEqual(ctyp, 'application/data')
self.assertEqual(cenc, 'binary')
def test_decode__not_accepted(self):
def test_loads__not_accepted(self):
with self.assertRaises(ContentDisallowed):
registry.decode('tainted', 'application/x-evil',
'binary', accept=[])
loads('tainted', 'application/x-evil', 'binary', accept=[])
with self.assertRaises(ContentDisallowed):
registry.decode('tainted', 'application/x-evil', 'binary',
accept=['application/x-json'])
loads('tainted', 'application/x-evil', 'binary',
accept=['application/x-json'])
self.assertTrue(
registry.decode('tainted', 'application/x-doomsday', 'binary',
accept=['application/x-doomsday'])
loads('tainted', 'application/x-doomsday', 'binary',
accept=['application/x-doomsday'])
)
def test_raw_encode(self):
@ -336,10 +326,10 @@ class test_Serialization(Case):
def test_register_yaml__no_yaml(self):
register_yaml()
with self.assertRaises(SerializerNotInstalled):
decode('foo', 'application/x-yaml', 'utf-8')
loads('foo', 'application/x-yaml', 'utf-8')
@mask_modules('msgpack')
def test_register_msgpack__no_msgpack(self):
register_msgpack()
with self.assertRaises(SerializerNotInstalled):
decode('foo', 'application/x-msgpack', 'utf-8')
loads('foo', 'application/x-msgpack', 'utf-8')