예제 #1
0
파일: dataset1.py 프로젝트: rcoup/sno
 def _msgpack_unpack_ext(self, code, data):
     if code == self.MSGPACK_EXT_GEOM:
         return Geometry.of(data)  # bytes
     else:
         self.L.warn("Unexpected msgpack extension: %d", code)
         return msgpack.ExtType(code, data)
예제 #2
0
def encoded(typ, data, for_json):
    if for_json:
        return {CANARY: typ, 'v': data}
    return msgpack.ExtType(typ, msgpack_dumps(data))
예제 #3
0
def ext_hook(self, code, data):
    service.log("ext_hook", code, data)
    return msgpack.ExtType(code, data)
예제 #4
0
 def default(obj):
     for num, c_type, encoder, _ in custom_encdec_list:
         if isinstance(obj, c_type):
             return msgpack.ExtType(num, encoder(obj))
     raise TypeError("Unknown type: {}".format(obj))
예제 #5
0
def ext_hook(code, data):
    if code == 42:
        p = PasteFile.from_dict(data)
        return p
    return msgpack.ExtType(code, data)
예제 #6
0
def _msgpack_default(obj):
    if isinstance(obj, datetime.datetime):
        return msgpack.ExtType(
            TYPE_DATETIME_CODE,
            obj.strftime(DATETIME_FORMAT).encode(CONTENT_ENCODING))
    raise TypeError("Unknown type: %r" % (obj, ))
예제 #7
0
class TestMsgpackFormatter(unittest.TestCase):
    formatter = MsgpackFormatter()

    @data(
        [1, 2, 3, 4],
        [0, [25, 636905376000000000, 636906075333708700, 55.0, None]],
        [
            3925794820, 0,
            msgpack.Timestamp(seconds=1587539993, nanoseconds=518021200), False
        ],
        [1, msgpack.ExtType(code=1, data=b'text')],
        [1, msgpack.ExtType(code=1, data=b'\x94\x01\x02\x03\x04')],

        # Example from #4781
        b'\xdc\x00\x1f\xcd9\x07\x10\xcf\x08\xd8\x03\x9e\x8f\xf53t\xcd\x01\xae'
        b'\xce\x00\x08\x82:\xa47626\xcc\xca&\xcfH\xd8\x03\xa1\xc4C^\xba\xcfH'
        b'\xd8\x03\x9e\x90\x81\xbf\x01G\xc2\xcd\x05C\xce\x00\x02|Z\xc2\xc2\xcc'
        b'\xe5\xcb@kD\xc97r\x82+\x0b\xcb@S\xc0\x00\x00\x00\x00\x00\xa11'
        b'\xb4hhjjk c \xd0\xbf\xd0\xbe\xd1\x87\xd1\x82\xd0\xbe\xd0\xb9\x01\x85'
        b'\x04\xce\x00\x01\x98\xa9\x03\xce\x00\x02\xdfm\x02\xce\x00\x02\xbc]'
        b'\x01\xce\x00\x02\xa9Q\x00\xce\x00\x0b\x1b\xc5\xcd\x16n\xc0\xc0\xc2'
        b'\xc0\x92\x00\x91\x0b\xc0')
    def test_decode(self, val):
        if type(val) == bytes:
            msgpacked_val = val
            val = msgpack.loads(val, raw=False, strict_map_key=False)
        else:
            msgpacked_val = msgpack.dumps(val)

        expected_output = json.dumps(val,
                                     default=self.formatter.default,
                                     ensure_ascii=False)

        formatter_response_dict = self.formatter.decode(msgpacked_val)

        self.assertIn('output', formatter_response_dict)

        actual_output = formatter_response_dict['output']
        self.assertEqual(actual_output, expected_output)

    @data(
        {
            'valid': ['valid', 'bytes'],
            'extra': ['extra', 'bytes']
        },
        {
            'valid': [],
            'extra': ['extra', 'bytes']
        },
        {
            'valid': msgpack.Timestamp(1, 1),
            'extra': ['extra', 'bytes']
        },
    )
    def test_decode_stream(self, stream):
        expected_output = json.dumps(stream['valid'],
                                     default=self.formatter.default,
                                     ensure_ascii=False)
        buf = io.BytesIO()

        buf.write(msgpack.dumps(stream['valid'], use_bin_type=True))
        buf.write(msgpack.dumps(stream['extra'], use_bin_type=True))

        broken_val = buf.getvalue()[:-5]
        formatter_response_dict = self.formatter.decode(broken_val)

        self.assertIn('output', formatter_response_dict)

        actual_output = formatter_response_dict['output']
        self.assertEqual(actual_output, expected_output)

    def test_encode(self):
        val = json.dumps('test')
        expected_output = msgpack.dumps('test')

        output = self.formatter.encode(val)
        self.assertEqual(output, expected_output)
예제 #8
0
import typing

import msgpack

from terraform import schemas

UNKNOWN = msgpack.ExtType(code=0, data=b"\x00")


def set_unknowns(
        value: typing.Optional[typing.Dict[str,
                                           typing.Any]], schema: schemas.Block
) -> typing.Optional[typing.Dict[str, typing.Any]]:
    result = {}

    if value is None:
        all_none = True
        for name, attribute in schema.attributes.items():
            if attribute.computed:
                result[name] = UNKNOWN
                all_none = False
            else:
                result[name] = None
        if all_none:
            return value
        return result

    value = typing.cast(typing.Dict[str, typing.Any], value)

    for name, attribute in schema.attributes.items():
        this_value = value.get(name)
예제 #9
0
파일: async_server.py 프로젝트: iot49/iot49
 def write(self, data):
     global _stream
     msgpack.pack(msgpack.ExtType(3, data), _stream)
예제 #10
0
def _serializer(registry, obj):
    handler = registry.match(obj)
    if handler is None:
        raise TypeError("No serialization handler registered"
                        " for type '%s'" % (type(obj).__name__))
    return msgpack.ExtType(handler.identity, handler.serialize(obj))
예제 #11
0
def _unserializer(registry, code, data):
    handler = registry.get(code)
    if handler is None:
        return msgpack.ExtType(code, data)
    else:
        return handler.deserialize(data)
예제 #12
0
파일: pack.py 프로젝트: momerm/sphinx
def default(obj):
    if isinstance(obj, EcPt):
        data = obj.export(POINT_CONVERSION_UNCOMPRESSED)
        return msgpack.ExtType(2, data)

    raise TypeError("Unknown type: %r" % (obj, ))
예제 #13
0
	def _msgpack_ext_hook(cls, code, obj_value):
		default = msgpack.ExtType(code, obj_value)
		if its.py_v3 and isinstance(obj_value, bytes):
			obj_value = obj_value.decode('utf-8')
		obj_type = cls._ext_types.get(code)
		return _serialize_ext_load(obj_type, obj_value, default)
예제 #14
0
	def _msgpack_default(cls, obj):
		obj_type, obj_value = _serialize_ext_dump(obj)
		obj_type = next(i[0] for i in cls._ext_types.items() if i[1] == obj_type)
		if its.py_v3 and isinstance(obj_value, str):
			obj_value = obj_value.encode('utf-8')
		return msgpack.ExtType(obj_type, obj_value)
예제 #15
0

with open('tests/python-ref-header.scm') as f:
    header = f.readlines()

with open('tests/python-ref-tests.scm', 'w') as f:
    f.writelines(header)

    def append_assert(val, chicken_expr=None, name=None):
        f.write(asChickenAssertion(val, chicken_expr=chicken_expr, name=name))

    append_assert(-1)
    append_assert(-100)
    append_assert(100)
    append_assert(-16384)
    append_assert(16384)
    append_assert(56213)
    append_assert(-56213)
    append_assert(100102831903)
    append_assert(-100102831903)
    append_assert(1.3313)
    append_assert(-7.8125653266e-200)
    append_assert(-7.8125653266e-231)
    append_assert([], '\'#()')
    append_assert([10, True, ['hi']],
                  chicken_expr='\'#(10 #t #("hi"))',
                  name='little nested list')
    append_assert(msgpack.ExtType(42, 'a'.encode('utf8')),
                  chicken_expr='(make-extension 42 (string->blob "a"))',
                  name='extension')
예제 #16
0
 def unpacking_ext_hook(self, code, data):
     if code == self.MSGPACK_EXT_CODE:
         return type(self)(msgpack_bytes=data)._deep_copy_dict()
     return msgpack.ExtType(code, data)
예제 #17
0
 def encode_ext(self, obj):
     convert = ExtConvertMeta.from_obj(obj)
     packed = msgpack.packb(convert.encode(obj), use_bin_type=True)
     return msgpack.ExtType(convert.code, packed)
예제 #18
0
def default(obj: Any) -> msgpack.ExtType:
    for cls, (t, encode) in __pack_handlers.items():
        if isinstance(obj, cls):
            return msgpack.ExtType(t, encode(obj))
    else:
        raise TypeError("Unknown type: %r" % (obj, ))
예제 #19
0
def _msgpack_ext_hook(code, data):
    if code == TYPE_DATETIME_CODE:
        return datetime.datetime.strptime(data.decode(CONTENT_ENCODING),
                                          DATETIME_FORMAT)
    return msgpack.ExtType(code, data)
예제 #20
0
def ext_hook(code: int, data: bytes) -> msgpack.ExtType:
    for c, decode in __unpack_handlers.items():
        if code == c:
            return decode(data)
    else:
        return msgpack.ExtType(code, data)
예제 #21
0
 def ext_hook(code, data):
     for num, _, _, decoder in custom_encdec_list:
         if num == code:
             return decoder(data)
     return msgpack.ExtType(code, data)
예제 #22
0
파일: base.py 프로젝트: lnenov/lymph
def raw_embed(data):
    return msgpack.ExtType(EMBEDDED_MSGPACK_TYPE, data)
예제 #23
0
def default(obj):
    if isinstance(obj, PasteFile):
        return msgpack.ExtType(42, obj.to_dict())
    raise TypeError('Unknown type: %r' % (obj, ))
예제 #24
0
파일: base.py 프로젝트: lnenov/lymph
def ext_hook(code, data):
    if code == EMBEDDED_MSGPACK_TYPE:
        return msgpack_serializer.loads(data)
    return msgpack.ExtType(code, data)
예제 #25
0
 def default(obj):
     return msgpack.ExtType(78, obj)
예제 #26
0
 def packing_hook(self, obj):
     if isinstance(obj, self):
         return msgpack.ExtType(self.MSGPACK_EXT_CODE, obj.serialized)
     raise TypeError("can't serialize {}({})".format(type(obj), repr(obj)))
예제 #27
0
 def default(ob):
     return msgpack.ExtType(127, b"")
예제 #28
0
 def unpacking_ext_hook(self, code, data):
     if code == self.MSGPACK_EXT_CODE:
         return self(msgpack_bytes=data)
     return msgpack.ExtType(code, data)
예제 #29
0
def encode(obj):
    """ Encode an object for proper decoding by Java or ObjC
    """
    if hasattr(obj, '__id__'):
        return msgpack.ExtType(ExtType.REF, msgpack.packb(obj.__id__))
    return obj
예제 #30
0
 def objecthook(code, data):
     if code == 42:
         return Sargecmd(data)
     return msgpack.ExtType(code, data)