Skip to content

Commit df41913

Browse files
committed
examples/natmod/btree: Make btree.open use mp_arg_parse_all for kwargs.
Python code is no longer needed to implement keyword arguments in `btree.open()`, it can now be done in C. Signed-off-by: Damien George <damien@micropython.org>
1 parent c624a5c commit df41913

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

examples/natmod/btree/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MPY_DIR = ../../..
55
MOD = btree_$(ARCH)
66

77
# Source files (.c or .py)
8-
SRC = btree_c.c btree_py.py
8+
SRC = btree_c.c
99

1010
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
1111
ARCH = x64

examples/natmod/btree/btree_c.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,39 @@ mp_getiter_iternext_custom_t btree_getiter_iternext;
106106
mp_map_elem_t btree_locals_dict_table[8];
107107
static MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table);
108108

109-
static mp_obj_t btree_open(size_t n_args, const mp_obj_t *args) {
109+
static mp_obj_t btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
110+
// The allowed_args array must have its qstr's populated at runtime.
111+
enum { ARG_flags, ARG_cachesize, ARG_pagesize, ARG_minkeypage };
112+
mp_arg_t allowed_args[] = {
113+
{ MP_QSTR_, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
114+
{ MP_QSTR_, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
115+
{ MP_QSTR_, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
116+
{ MP_QSTR_, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
117+
};
118+
allowed_args[0].qst = MP_QSTR_flags;
119+
allowed_args[1].qst = MP_QSTR_cachesize;
120+
allowed_args[2].qst = MP_QSTR_pagesize;
121+
allowed_args[3].qst = MP_QSTR_minkeypage;
122+
110123
// Make sure we got a stream object
111-
mp_get_stream_raise(args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
124+
mp_get_stream_raise(pos_args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
125+
126+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
127+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
112128

113129
BTREEINFO openinfo = {0};
114-
openinfo.flags = mp_obj_get_int(args[1]);
115-
openinfo.cachesize = mp_obj_get_int(args[2]);
116-
openinfo.psize = mp_obj_get_int(args[3]);
117-
openinfo.minkeypage = mp_obj_get_int(args[4]);
118-
DB *db = __bt_open(MP_OBJ_TO_PTR(args[0]), &btree_stream_fvtable, &openinfo, 0);
130+
openinfo.flags = args[ARG_flags].u_int;
131+
openinfo.cachesize = args[ARG_cachesize].u_int;
132+
openinfo.psize = args[ARG_pagesize].u_int;
133+
openinfo.minkeypage = args[ARG_minkeypage].u_int;
134+
DB *db = __bt_open(MP_OBJ_TO_PTR(pos_args[0]), &btree_stream_fvtable, &openinfo, 0);
119135
if (db == NULL) {
120136
mp_raise_OSError(native_errno);
121137
}
122138

123-
return MP_OBJ_FROM_PTR(btree_new(db, args[0]));
139+
return MP_OBJ_FROM_PTR(btree_new(db, pos_args[0]));
124140
}
125-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_open_obj, 5, 5, btree_open);
141+
static MP_DEFINE_CONST_FUN_OBJ_KW(btree_open_obj, 1, btree_open);
126142

127143
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
128144
MP_DYNRUNTIME_INIT_ENTRY
@@ -147,7 +163,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
147163
btree_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_items), MP_OBJ_FROM_PTR(&btree_items_obj) };
148164
MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 4);
149165

150-
mp_store_global(MP_QSTR__open, MP_OBJ_FROM_PTR(&btree_open_obj));
166+
mp_store_global(MP_QSTR_open, MP_OBJ_FROM_PTR(&btree_open_obj));
151167
mp_store_global(MP_QSTR_INCL, MP_OBJ_NEW_SMALL_INT(FLAG_END_KEY_INCL));
152168
mp_store_global(MP_QSTR_DESC, MP_OBJ_NEW_SMALL_INT(FLAG_DESC));
153169

examples/natmod/btree/btree_py.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)