diff options
| author | INADA Naoki <inada-n@klab.com> | 2013-10-21 00:59:22 +0900 |
|---|---|---|
| committer | INADA Naoki <inada-n@klab.com> | 2013-10-21 00:59:22 +0900 |
| commit | e3fee4db5fbf1ead4a98fff6c8843574480c3c2a (patch) | |
| tree | 1cff58d64577660b7d632806a67b798716c37854 /msgpack/__init__.py | |
| parent | 37c2ad63af8a6e5cb6944f80d931fedbc6b49e7d (diff) | |
| download | msgpack-python-e3fee4db5fbf1ead4a98fff6c8843574480c3c2a.tar.gz | |
fallback: support packing ExtType
Diffstat (limited to 'msgpack/__init__.py')
| -rw-r--r-- | msgpack/__init__.py | 26 |
1 files changed, 24 insertions, 2 deletions
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'): |
