Skip to content

Commit 99061d1

Browse files
committed
extmod/modbtree: Switch to accepting stream object instead of filename.
Requires "embedded" BerkeleyDB BTree implementation.
1 parent 0dfe849 commit 99061d1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

extmod/modbtree.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "py/nlr.h"
3333
#include "py/runtime.h"
34+
#include "py/stream.h"
3435

3536
#if MICROPY_PY_BTREE
3637

@@ -314,23 +315,28 @@ STATIC const mp_obj_type_t btree_type = {
314315
.locals_dict = (void*)&btree_locals_dict,
315316
};
316317

318+
STATIC FILEVTABLE btree_stream_fvtable = {
319+
mp_stream_posix_read,
320+
mp_stream_posix_write,
321+
mp_stream_posix_lseek,
322+
mp_stream_posix_fsync
323+
};
324+
317325
STATIC mp_obj_t mod_btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
318326
static const mp_arg_t allowed_args[] = {
319327
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
320328
};
321329

322-
const char *fname = NULL;
323-
if (pos_args[0] != mp_const_none) {
324-
fname = mp_obj_str_get_str(pos_args[0]);
325-
}
330+
// Make sure we got a stream object
331+
mp_get_stream_raise(pos_args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
326332

327333
struct {
328334
mp_arg_val_t server_side;
329335
} args;
330336
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
331337
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
332338

333-
DB *db = __bt_open(fname, /*flags*/O_CREAT | O_RDWR, /*mode*/0770, /*openinfo*/NULL, /*dflags*/0);
339+
DB *db = __bt_open(pos_args[0], &btree_stream_fvtable, /*openinfo*/NULL, /*dflags*/0);
334340
if (db == NULL) {
335341
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
336342
}

0 commit comments

Comments
 (0)