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)
def encoded(typ, data, for_json): if for_json: return {CANARY: typ, 'v': data} return msgpack.ExtType(typ, msgpack_dumps(data))
def ext_hook(self, code, data): service.log("ext_hook", code, data) return msgpack.ExtType(code, data)
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))
def ext_hook(code, data): if code == 42: p = PasteFile.from_dict(data) return p return msgpack.ExtType(code, data)
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, ))
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)
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)
def write(self, data): global _stream msgpack.pack(msgpack.ExtType(3, data), _stream)
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))
def _unserializer(registry, code, data): handler = registry.get(code) if handler is None: return msgpack.ExtType(code, data) else: return handler.deserialize(data)
def default(obj): if isinstance(obj, EcPt): data = obj.export(POINT_CONVERSION_UNCOMPRESSED) return msgpack.ExtType(2, data) raise TypeError("Unknown type: %r" % (obj, ))
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)
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)
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')
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)
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)
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, ))
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)
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)
def ext_hook(code, data): for num, _, _, decoder in custom_encdec_list: if num == code: return decoder(data) return msgpack.ExtType(code, data)
def raw_embed(data): return msgpack.ExtType(EMBEDDED_MSGPACK_TYPE, data)
def default(obj): if isinstance(obj, PasteFile): return msgpack.ExtType(42, obj.to_dict()) raise TypeError('Unknown type: %r' % (obj, ))
def ext_hook(code, data): if code == EMBEDDED_MSGPACK_TYPE: return msgpack_serializer.loads(data) return msgpack.ExtType(code, data)
def default(obj): return msgpack.ExtType(78, obj)
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)))
def default(ob): return msgpack.ExtType(127, b"")
def unpacking_ext_hook(self, code, data): if code == self.MSGPACK_EXT_CODE: return self(msgpack_bytes=data) return msgpack.ExtType(code, data)
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
def objecthook(code, data): if code == 42: return Sargecmd(data) return msgpack.ExtType(code, data)