|
31 | 31 |
|
32 | 32 | #include "py/nlr.h"
|
33 | 33 | #include "py/runtime.h"
|
| 34 | +#include "py/stream.h" |
34 | 35 |
|
35 | 36 | #if MICROPY_PY_BTREE
|
36 | 37 |
|
@@ -314,23 +315,28 @@ STATIC const mp_obj_type_t btree_type = {
|
314 | 315 | .locals_dict = (void*)&btree_locals_dict,
|
315 | 316 | };
|
316 | 317 |
|
| 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 | + |
317 | 325 | STATIC mp_obj_t mod_btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
318 | 326 | static const mp_arg_t allowed_args[] = {
|
319 | 327 | { MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
320 | 328 | };
|
321 | 329 |
|
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); |
326 | 332 |
|
327 | 333 | struct {
|
328 | 334 | mp_arg_val_t server_side;
|
329 | 335 | } args;
|
330 | 336 | mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
331 | 337 | MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
|
332 | 338 |
|
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); |
334 | 340 | if (db == NULL) {
|
335 | 341 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
|
336 | 342 | }
|
|
0 commit comments