Skip to content

Commit

Permalink
pybricks.common.System.storage: use buffer protocol
Browse files Browse the repository at this point in the history
This updates the storage method to allow any object with the buffer
protocol instead of just bytes.

Also change the missing arg error to a TypeError while we are touching
this.

Issue: #121
  • Loading branch information
dlech committed Oct 21, 2022
1 parent 246ebea commit ccb6251
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pybricks/common/pb_type_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@ STATIC mp_obj_t pb_type_System_storage(size_t n_args, const mp_obj_t *pos_args,
mp_int_t offset = mp_obj_get_int(offset_in);

// Handle read.
if (write_in == mp_const_none) {
if (read_in != mp_const_none) {

This comment has been minimized.

Copy link
@laurensvalk

laurensvalk Oct 21, 2022

Member

This is not entirely the same. The reason for doing this check the other way originally, is to require that at least one argument remains none. So that passing both read and write would raise an error. It seems like this change would make it pass.

This comment has been minimized.

Copy link
@dlech

dlech Oct 21, 2022

Author Member

oops

This comment has been minimized.

Copy link
@laurensvalk

laurensvalk Oct 21, 2022

Member

Heh, don’t let it stop the release though. There’s always next time :shipit:

byte *data;
mp_uint_t size = mp_obj_get_int(read_in);
pb_assert(pbsys_program_load_get_user_data(offset, &data, size));
return mp_obj_new_bytes(data, size);
}

// Handle write.
if (read_in == mp_const_none && !mp_obj_is_str(write_in) && mp_obj_is_str_or_bytes(write_in)) {
mp_obj_str_t *obj = ((mp_obj_str_t *)MP_OBJ_TO_PTR(write_in));
pbsys_program_load_set_user_data(offset, obj->data, obj->len);
if (write_in != mp_const_none) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(write_in, &bufinfo, MP_BUFFER_READ);

pb_assert(pbsys_program_load_set_user_data(offset, bufinfo.buf, bufinfo.len));

return mp_const_none;
}

mp_raise_ValueError(MP_ERROR_TEXT("Must set either read (int) or write (bytes)."));
mp_raise_TypeError(MP_ERROR_TEXT("Must set either read (int) or write (bytes)."));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_System_storage_obj, 0, pb_type_System_storage);

Expand Down

0 comments on commit ccb6251

Please sign in to comment.