2020-11-08 21:39:20 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Bernhard Boser
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-12-07 18:16:16 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
2020-11-09 21:09:56 -05:00
|
|
|
#include "shared-bindings/msgpack/__init__.h"
|
|
|
|
#include "shared-module/msgpack/__init__.h"
|
2020-12-07 18:16:16 -05:00
|
|
|
#include "shared-bindings/msgpack/ExtType.h"
|
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
#define MP_OBJ_IS_METH(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_bound_method))
|
2020-11-09 21:09:56 -05:00
|
|
|
|
|
|
|
//| """Pack object in msgpack format
|
|
|
|
//|
|
|
|
|
//| The msgpack format is similar to json, except that the encoded data is binary.
|
2020-12-07 18:16:16 -05:00
|
|
|
//| See https://msgpack.org for details. The module implements a subset of the cpython
|
|
|
|
//| module msgpack-python.
|
|
|
|
//|
|
|
|
|
//| Not implemented: 64-bit int, uint, float.
|
2020-11-09 21:09:56 -05:00
|
|
|
//|
|
2021-01-06 12:03:58 -05:00
|
|
|
//| Example 1::
|
|
|
|
//|
|
2020-11-09 21:09:56 -05:00
|
|
|
//| import msgpack
|
2020-11-12 15:28:40 -05:00
|
|
|
//| from io import BytesIO
|
2020-11-09 21:12:55 -05:00
|
|
|
//|
|
2020-11-12 15:28:40 -05:00
|
|
|
//| b = BytesIO()
|
2020-12-09 15:01:09 -05:00
|
|
|
//| msgpack.pack({'list': [True, False, None, 1, 3.14], 'str': 'blah'}, b)
|
2020-11-12 15:28:40 -05:00
|
|
|
//| b.seek(0)
|
2020-12-07 19:32:38 -05:00
|
|
|
//| print(msgpack.unpack(b))
|
2020-11-09 21:12:55 -05:00
|
|
|
//|
|
2021-01-06 12:03:58 -05:00
|
|
|
//| Example 2: handling objects::
|
2020-12-07 18:16:16 -05:00
|
|
|
//|
|
|
|
|
//| from msgpack import pack, unpack, ExtType
|
|
|
|
//| from io import BytesIO
|
|
|
|
//|
|
|
|
|
//| class MyClass:
|
|
|
|
//| def __init__(self, val):
|
|
|
|
//| self.value = val
|
|
|
|
//| def __str__(self):
|
|
|
|
//| return str(self.value)
|
|
|
|
//|
|
|
|
|
//| data = MyClass(b'my_value')
|
|
|
|
//|
|
|
|
|
//| def encoder(obj):
|
|
|
|
//| if isinstance(obj, MyClass):
|
|
|
|
//| return ExtType(1, obj.value)
|
|
|
|
//| return f"no encoder for {obj}"
|
2020-11-09 21:09:56 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
//| def decoder(code, data):
|
|
|
|
//| if code == 1:
|
|
|
|
//| return MyClass(data)
|
|
|
|
//| return f"no decoder for type {code}"
|
|
|
|
//|
|
|
|
|
//| buffer = BytesIO()
|
|
|
|
//| pack(data, buffer, default=encoder)
|
|
|
|
//| buffer.seek(0)
|
|
|
|
//| decoded = unpack(buffer, ext_hook=decoder)
|
|
|
|
//| print(f"{data} -> {buffer.getvalue()} -> {decoded}")
|
2021-01-06 12:03:58 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
//| """
|
2020-12-10 13:59:26 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
|
2020-12-08 18:48:56 -05:00
|
|
|
//| def pack(obj: object, buffer: WriteableBuffer, *, default: Union[Callable[[object], None], None] = None) -> None:
|
2020-12-07 18:16:16 -05:00
|
|
|
//| """Ouput object to buffer in msgpack format.
|
2021-01-06 12:03:58 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
//| :param object obj: Object to convert to msgpack format.
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.WriteableBuffer buffer: buffer to write into
|
|
|
|
//| :param Optional[~circuitpython_typing.Callable[[object], None]] default:
|
2020-12-07 18:16:16 -05:00
|
|
|
//| function called for python objects that do not have
|
|
|
|
//| a representation in msgpack format.
|
|
|
|
//| """
|
2021-01-05 14:17:09 -05:00
|
|
|
//| ...
|
2020-12-10 13:59:26 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
STATIC mp_obj_t mod_msgpack_pack(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_obj, ARG_buffer, ARG_default };
|
|
|
|
STATIC const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_obj, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_default, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-11-08 21:39:20 -05:00
|
|
|
|
2020-12-07 18:16:16 -05:00
|
|
|
mp_obj_t handler = args[ARG_default].u_obj;
|
2021-04-22 20:55:39 -04:00
|
|
|
if (handler != mp_const_none && !mp_obj_is_fun(handler) && !MP_OBJ_IS_METH(handler)) {
|
2020-12-07 18:16:16 -05:00
|
|
|
mp_raise_ValueError(translate("default is not a function"));
|
|
|
|
}
|
|
|
|
|
|
|
|
common_hal_msgpack_pack(args[ARG_obj].u_obj, args[ARG_buffer].u_obj, handler);
|
2020-11-08 21:39:20 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-10-07 16:36:06 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mod_msgpack_pack_obj, 0, mod_msgpack_pack);
|
2020-11-08 21:39:20 -05:00
|
|
|
|
2020-12-08 18:48:56 -05:00
|
|
|
|
|
|
|
//| def unpack(buffer: ReadableBuffer, *, ext_hook: Union[Callable[[int, bytes], object], None] = None, use_list: bool=True) -> object:
|
2020-12-07 18:16:16 -05:00
|
|
|
//| """Unpack and return one object from buffer.
|
2021-01-06 12:03:58 -05:00
|
|
|
//|
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer buffer: buffer to read from
|
|
|
|
//| :param Optional[~circuitpython_typing.Callable[[int, bytes], object]] ext_hook: function called for objects in
|
2020-12-07 18:16:16 -05:00
|
|
|
//| msgpack ext format.
|
|
|
|
//| :param Optional[bool] use_list: return array as list or tuple (use_list=False).
|
2021-01-06 12:03:58 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
//| :return object: object read from buffer.
|
|
|
|
//| """
|
2021-01-05 14:17:09 -05:00
|
|
|
//| ...
|
2020-12-10 13:59:26 -05:00
|
|
|
//|
|
2020-12-07 18:16:16 -05:00
|
|
|
STATIC mp_obj_t mod_msgpack_unpack(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_buffer, ARG_ext_hook, ARG_use_list };
|
|
|
|
STATIC const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
|
|
|
{ MP_QSTR_ext_hook, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
|
|
|
{ MP_QSTR_use_list, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = true } },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
mp_obj_t hook = args[ARG_ext_hook].u_obj;
|
2021-04-22 20:55:39 -04:00
|
|
|
if (hook != mp_const_none && !mp_obj_is_fun(hook) && !MP_OBJ_IS_METH(hook)) {
|
2020-12-07 18:16:16 -05:00
|
|
|
mp_raise_ValueError(translate("ext_hook is not a function"));
|
|
|
|
}
|
2020-11-08 21:39:20 -05:00
|
|
|
|
2020-12-07 18:16:16 -05:00
|
|
|
return common_hal_msgpack_unpack(args[ARG_buffer].u_obj, hook, args[ARG_use_list].u_bool);
|
2020-11-08 21:39:20 -05:00
|
|
|
}
|
2021-10-07 16:36:06 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mod_msgpack_unpack_obj, 0, mod_msgpack_unpack);
|
2020-11-08 21:39:20 -05:00
|
|
|
|
2020-11-09 21:09:56 -05:00
|
|
|
|
2020-11-08 21:39:20 -05:00
|
|
|
STATIC const mp_rom_map_elem_t msgpack_module_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_msgpack) },
|
2020-12-07 18:16:16 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ExtType), MP_ROM_PTR(&mod_msgpack_exttype_type) },
|
2020-11-08 21:39:20 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pack), MP_ROM_PTR(&mod_msgpack_pack_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unpack), MP_ROM_PTR(&mod_msgpack_unpack_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(msgpack_module_globals, msgpack_module_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t msgpack_module = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&msgpack_module_globals,
|
2020-11-08 21:39:20 -05:00
|
|
|
};
|
Convert more modules to use MP_REGISTER_MODULE
Convert bitbangio, bitmaptools, _bleio, board, busio, countio, digitalio, framebufferio, frequencyio, gamepadshift, getpass, keypad, math, microcontroller, and msgpack modules to use MP_REGISTER_MODULE.
Related to #5183.
2021-08-26 21:32:44 -04:00
|
|
|
|
|
|
|
MP_REGISTER_MODULE(MP_QSTR_msgpack, msgpack_module, CIRCUITPY_MSGPACK);
|