8000 py: Fix mp_get_buffer, and use it in more places. · lurch/micropython@8a1cab9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a1cab9

Browse files
committed
py: Fix mp_get_buffer, and use it in more places.
Must use mp_obj_get_type to get the type of an object. Can't assume mp_obj_t is castable to mp_obj_base_t.
1 parent 4b01de4 commit 8a1cab9

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

py/obj.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ mp_obj_t mp_identity(mp_obj_t self) {
333333
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
334334

335335
bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) {
336-
mp_obj_base_t *o = (mp_obj_base_t *)obj;
337-
if (o->type->buffer_p.get_buffer == NULL) {
336+
mp_obj_type_t *type = mp_obj_get_type(obj);
337+
if (type->buffer_p.get_buffer == NULL) {
338338
return false;
339339
}
340-
o->type->buffer_p.get_buffer(o, bufinfo, BUFFER_READ);
340+
type->buffer_p.get_buffer(obj, bufinfo, BUFFER_READ);
341341
if (bufinfo->buf == NULL) {
342342
return false;
343343
}

py/objfun.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,15 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
442442
mp_obj_t *items;
443443
mp_obj_list_get(obj, &len, &items);
444444
return (machine_uint_t)items;
445-
} else if (type->buffer_p.get_buffer != NULL) {
446-
// supports the buffer protocol, get a pointer to the data
447-
buffer_info_t bufinfo;
448-
type->buffer_p.get_buffer(obj, &bufinfo, BUFFER_READ);
449-
return (machine_uint_t)bufinfo.buf;
450445
} else {
451-
// just pass along a pointer to the object
452-
return (machine_uint_t)obj;
446+
buffer_info_t bufinfo;
447+
if (mp_get_buffer(obj, &bufinfo)) {
448+
// supports the buffer protocol, return a pointer to the data
449+
return (machine_uint_t)bufinfo.buf;
450+
} else {
451+
// just pass along a pointer to the object
452+
return (machine_uint_t)obj;
453+
}
453454
}
454455
}
455456
}

stmhal/dac.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,8 @@ mp_obj_t pyb_dac_dma(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
180180
// set TIM6 to trigger the DAC at the given frequency
181181
TIM6_Config(mp_obj_get_int(args[2]));
182182

183-
mp_obj_type_t *type = mp_obj_get_type(args[1]);
184-
if (type->buffer_p.get_buffer == NULL) {
185-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "buffer argument must support buffer protocol"));
186-
}
187183
buffer_info_t bufinfo;
188-
type->buffer_p.get_buffer(args[1], &bufinfo, BUFFER_READ);
184+
mp_get_buffer_raise(args[1], &bufinfo);
189185

190186
__DMA1_CLK_ENABLE();
191187

stmhal/i2c.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,13 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) {
143143
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
144144
machine_uint_t mem_addr = mp_obj_get_int(args[2]);
145145
HAL_StatusTypeDef status;
146-
mp_obj_type_t *type = mp_obj_get_type(args[3]);
147-
if (type->buffer_p.get_buffer != NULL) {
148-
buffer_info_t bufinfo;
149-
type->buffer_p.get_buffer(args[3], &bufinfo, BUFFER_READ);
150-
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
151-
} else if (MP_OBJ_IS_INT(args[3])) {
146+
if (MP_OBJ_IS_INT(args[3])) {
152147
uint8_t data[1] = {mp_obj_get_int(args[3])};
153148
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
154149
} else {
155-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "data argument must be an integer or support the buffer protocol"));
150+
buffer_info_t bufinfo;
151+
mp_get_buffer_raise(args[3], &bufinfo);
152+
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
156153
}
157154

158155
//printf("Write got %d\n", status);

0 commit comments

Comments
 (0)
0