mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-13 19:04:16 +00:00
fallback: support packing ExtType
This commit is contained in:
parent
37c2ad63af
commit
e3fee4db5f
3 changed files with 131 additions and 81 deletions
|
|
@ -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'):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue