From 96bcd76f49afd00f5b7def1ff7cfd002a7fa477d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 20 Oct 2013 20:28:32 +0900 Subject: Packing ExtType and some cleanup --- msgpack/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'msgpack/__init__.py') diff --git a/msgpack/__init__.py b/msgpack/__init__.py index 79107b6..a7b47b1 100644 --- a/msgpack/__init__.py +++ b/msgpack/__init__.py @@ -26,6 +26,7 @@ def pack(o, stream, **kwargs): packer = Packer(**kwargs) stream.write(packer.pack(o)) + def packb(o, **kwargs): """ Pack object `o` and return packed bytes @@ -40,4 +41,3 @@ loads = unpackb dump = pack dumps = packb - -- cgit v1.2.1 From e3fee4db5fbf1ead4a98fff6c8843574480c3c2a Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 21 Oct 2013 00:59:22 +0900 Subject: fallback: support packing ExtType --- msgpack/__init__.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'msgpack/__init__.py') diff --git a/msgpack/__init__.py b/msgpack/__init__.py index a7b47b1..56a0b36 100644 --- a/msgpack/__init__.py +++ b/msgpack/__init__.py @@ -2,9 +2,31 @@ from msgpack._version import version from msgpack.exceptions import * -from collections import namedtuple -ExtType = namedtuple('ExtType', 'code data') +class ExtType(object): + __slots__ = ('code', 'data') + + def __init__(self, code, data): + if not isinstance(code, int): + raise TypeError("code must be int") + if not isinstance(data, bytes): + raise TypeError("data must be bytes") + if not 0 <= code <= 127: + raise ValueError("code must be 0~127") + self.code = code + self.data = data + + def __eq__(self, other): + if not isinstance(other, ExtType): + return NotImplemented + return self.code == other.code and self.data == other.data + + def __hash__(self): + return self.code ^ hash(self.data) + + def __repr__(self): + return "msgpack.ExtType(%r, %r)" % (self.code, self.data) + import os if os.environ.get('MSGPACK_PUREPYTHON'): -- cgit v1.2.1 From d84a403bc0bbbb36c4a5833e00269eef6c4a91ae Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 21 Oct 2013 01:12:57 +0900 Subject: fix bugs. --- msgpack/__init__.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'msgpack/__init__.py') diff --git a/msgpack/__init__.py b/msgpack/__init__.py index 56a0b36..a958025 100644 --- a/msgpack/__init__.py +++ b/msgpack/__init__.py @@ -2,30 +2,18 @@ from msgpack._version import version from msgpack.exceptions import * +from collections import namedtuple -class ExtType(object): - __slots__ = ('code', 'data') - def __init__(self, code, data): +class ExtType(namedtuple('ExtType', 'code data')): + def __new__(cls, code, data): if not isinstance(code, int): raise TypeError("code must be int") if not isinstance(data, bytes): raise TypeError("data must be bytes") if not 0 <= code <= 127: raise ValueError("code must be 0~127") - self.code = code - self.data = data - - def __eq__(self, other): - if not isinstance(other, ExtType): - return NotImplemented - return self.code == other.code and self.data == other.data - - def __hash__(self): - return self.code ^ hash(self.data) - - def __repr__(self): - return "msgpack.ExtType(%r, %r)" % (self.code, self.data) + return super(ExtType, cls).__new__(cls, code, data) import os -- cgit v1.2.1