Skip to content

Commit b5b6b6d

Browse files
committed
add ExtType, update doc, add a test
1 parent 608c985 commit b5b6b6d

File tree

9 files changed

+447
-102
lines changed

9 files changed

+447
-102
lines changed

locale/circuitpython.pot

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-12-01 18:39-0800\n"
11+
"POT-Creation-Date: 2020-12-07 15:35-0800\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -270,6 +270,10 @@ msgstr ""
270270
msgid "3-arg pow() not supported"
271271
msgstr ""
272272

273+
#: shared-module/msgpack/__init__.c
274+
msgid "64 bit types"
275+
msgstr ""
276+
273277
#: ports/atmel-samd/common-hal/countio/Counter.c
274278
#: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c
275279
msgid "A hardware interrupt channel is already in use"
@@ -2371,6 +2375,10 @@ msgstr ""
23712375
msgid "circle can only be registered in one parent"
23722376
msgstr ""
23732377

2378+
#: shared-bindings/msgpack/ExtType.c
2379+
msgid "code outside range 0~127"
2380+
msgstr ""
2381+
23742382
#: shared-bindings/displayio/Palette.c
23752383
msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)"
23762384
msgstr ""
@@ -2451,6 +2459,10 @@ msgstr ""
24512459
msgid "default 'except' must be last"
24522460
msgstr ""
24532461

2462+
#: shared-bindings/msgpack/__init__.c
2463+
msgid "default is not a function"
2464+
msgstr ""
2465+
24542466
#: shared-bindings/audiobusio/PDMIn.c
24552467
msgid ""
24562468
"destination buffer must be a bytearray or array of type 'B' for bit_depth = 8"
@@ -2550,6 +2562,10 @@ msgstr ""
25502562
msgid "expecting key:value for dict"
25512563
msgstr ""
25522564

2565+
#: shared-bindings/msgpack/__init__.c
2566+
msgid "ext_hook is not a function"
2567+
msgstr ""
2568+
25532569
#: py/argcheck.c
25542570
msgid "extra keyword arguments given"
25552571
msgstr ""
@@ -3078,12 +3094,12 @@ msgstr ""
30783094
msgid "no binding for nonlocal found"
30793095
msgstr ""
30803096

3081-
#: py/builtinimport.c
3082-
msgid "no module named '%q'"
3097+
#: shared-module/msgpack/__init__.c
3098+
msgid "no default packer"
30833099
msgstr ""
30843100

3085-
#: shared-module/msgpack/__init__.c
3086-
msgid "no packer"
3101+
#: py/builtinimport.c
3102+
msgid "no module named '%q'"
30873103
msgstr ""
30883104

30893105
#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c
@@ -3099,10 +3115,6 @@ msgstr ""
30993115
msgid "no such attribute"
31003116
msgstr ""
31013117

3102-
#: shared-module/msgpack/__init__.c
3103-
msgid "no unpacker found"
3104-
msgstr ""
3105-
31063118
#: ports/nrf/common-hal/_bleio/Connection.c
31073119
msgid "non-UUID found in service_uuids_whitelist"
31083120
msgstr ""
@@ -3560,10 +3572,6 @@ msgstr ""
35603572
msgid "tobytes can be invoked for dense arrays only"
35613573
msgstr ""
35623574

3563-
#: shared-module/msgpack/__init__.c
3564-
msgid "too big"
3565-
msgstr ""
3566-
35673575
#: shared-module/struct/__init__.c
35683576
msgid "too many arguments provided with the given format"
35693577
msgstr ""

shared-bindings/msgpack/ExtType.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Bernhard Boser
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "py/smallint.h"
29+
#include "py/objproperty.h"
30+
#include "shared-bindings/msgpack/ExtType.h"
31+
32+
//| class ExtType:
33+
//| """ExtType represents ext type in msgpack."""
34+
//| def __init__(self, code: int, data: bytes) -> None:
35+
//| """Constructor"""
36+
//|
37+
//| :param int code: type code in range 0~127.
38+
//| :param bytes data: representation.
39+
40+
STATIC mp_obj_t mod_msgpack_exttype_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
41+
mod_msgpack_extype_obj_t *self = m_new_obj(mod_msgpack_extype_obj_t);
42+
self->base.type = &mod_msgpack_exttype_type;
43+
enum { ARG_code, ARG_data };
44+
static const mp_arg_t allowed_args[] = {
45+
{ MP_QSTR_code, MP_ARG_INT | MP_ARG_REQUIRED },
46+
{ MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED },
47+
};
48+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
49+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
50+
51+
int code = args[ARG_code].u_int;
52+
if (code < 0 || code > 127) {
53+
mp_raise_AttributeError(translate("code outside range 0~127"));
54+
}
55+
self->code = code;
56+
57+
mp_obj_t data = args[ARG_data].u_obj;
58+
self->data = data;
59+
return MP_OBJ_FROM_PTR(self);
60+
}
61+
62+
63+
//| code: int
64+
//| """The type code, in range 0~127."""
65+
//|
66+
67+
STATIC mp_obj_t mod_msgpack_exttype_get_code(mp_obj_t self_in) {
68+
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
69+
return MP_OBJ_NEW_SMALL_INT(self->code);
70+
}
71+
MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_code_obj, mod_msgpack_exttype_get_code);
72+
73+
STATIC mp_obj_t mod_msgpack_exttype_set_code(mp_obj_t self_in, mp_obj_t code_in) {
74+
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
75+
int code = mp_obj_get_int(code_in);
76+
if (code < 0 || code > 127) {
77+
mp_raise_AttributeError(translate("code outside range 0~127"));
78+
}
79+
self->code = code;
80+
return mp_const_none;
81+
}
82+
MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_code_obj, mod_msgpack_exttype_set_code);
83+
84+
const mp_obj_property_t mod_msgpack_exttype_code_obj = {
85+
.base.type = &mp_type_property,
86+
.proxy = {(mp_obj_t)&mod_msgpack_exttype_get_code_obj,
87+
(mp_obj_t)&mod_msgpack_exttype_set_code_obj,
88+
(mp_obj_t)&mp_const_none_obj},
89+
};
90+
91+
//| data: bytes
92+
//| """Data."""
93+
//|
94+
95+
STATIC mp_obj_t mod_msgpack_exttype_get_data(mp_obj_t self_in) {
96+
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
97+
return self->data;
98+
}
99+
MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_data_obj, mod_msgpack_exttype_get_data);
100+
101+
STATIC mp_obj_t mod_msgpack_exttype_set_data(mp_obj_t self_in, mp_obj_t data_in) {
102+
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
103+
self->data = data_in;
104+
return mp_const_none;
105+
}
106+
MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_data_obj, mod_msgpack_exttype_set_data);
107+
108+
const mp_obj_property_t mod_msgpack_exttype_data_obj = {
109+
.base.type = &mp_type_property,
110+
.proxy = {(mp_obj_t)&mod_msgpack_exttype_get_data_obj,
111+
(mp_obj_t)&mod_msgpack_exttype_set_data_obj,
112+
(mp_obj_t)&mp_const_none_obj},
113+
};
114+
115+
STATIC mp_rom_map_elem_t mod_msgpack_exttype_locals_dict_table[] = {
116+
// Properties
117+
{ MP_ROM_QSTR(MP_QSTR_code), MP_ROM_PTR(&mod_msgpack_exttype_code_obj) },
118+
{ MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&mod_msgpack_exttype_data_obj) },
119+
};
120+
STATIC MP_DEFINE_CONST_DICT(mod_msgpack_exttype_locals_dict, mod_msgpack_exttype_locals_dict_table);
121+
122+
const mp_obj_type_t mod_msgpack_exttype_type = {
123+
{ &mp_type_type },
124+
.name = MP_QSTR_ExtType,
125+
.make_new = mod_msgpack_exttype_make_new,
126+
.locals_dict = (mp_obj_dict_t*)&mod_msgpack_exttype_locals_dict,
127+
};

shared-bindings/msgpack/ExtType.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Bernhard Boser
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK_EXTTYPE___INIT___H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK_EXTTYPE___INIT___H
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
int32_t code;
35+
mp_obj_t data;
36+
} mod_msgpack_extype_obj_t;
37+
38+
extern const mp_obj_type_t mod_msgpack_exttype_type;
39+
40+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK_EXTTYPE___INIT___H

0 commit comments

Comments
 (0)