Skip to content

Commit 134c10e

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 47e1b85 + f898a95 commit 134c10e

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

py/objarray.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
9191
// This is top-level factory function, not virtual method
9292
// TODO: "bytearray" really should be type, not function
9393
STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
94+
if (MP_OBJ_IS_SMALL_INT(arg)) {
95+
uint len = MP_OBJ_SMALL_INT_VALUE(arg);
96+
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
97+
memset(o->items, 0, len);
98+
return o;
99+
}
100+
94101
return array_construct(BYTEARRAY_TYPECODE, arg);
95102
}
96103
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);

py/objstr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,7 @@ const mp_obj_type_t mp_type_bytes = {
13521352
.make_new = bytes_make_new,
13531353
.binary_op = str_binary_op,
13541354
.getiter = mp_obj_new_bytes_iterator,
1355+
.buffer_p = { .get_buffer = str_get_buffer },
13551356
.locals_dict = (mp_obj_t)&str_locals_dict,
13561357
};
13571358

tests/basics/bytearray1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
print(bytearray(4))
12
a = bytearray([1, 2, 200])
23
print(a[0], a[2])
34
print(a[-1])

unix/modffi.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,32 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *
253253
values[i] = 0;
254254
} else if (MP_OBJ_IS_INT(a)) {
255255
values[i] = mp_obj_int_get(a);
256-
} else if (MP_OBJ_IS_STR(a) || MP_OBJ_IS_TYPE(a, &mp_type_bytes)) {
256+
} else if (MP_OBJ_IS_STR(a)) {
257257
const char *s = mp_obj_str_get_str(a);
258258
values[i] = (ffi_arg)s;
259+
} else if (((mp_obj_base_t*)a)->type->buffer_p.get_buffer != NULL) {
260+
mp_obj_base_t *o = (mp_obj_base_t*)a;
261+
buffer_info_t bufinfo;
262+
o->type->buffer_p.get_buffer(o, &bufinfo, BUFFER_READ); // TODO: BUFFER_READ?
263+
if (bufinfo.buf == NULL) {
264+
goto error;
265+
}
266+
values[i] = (ffi_arg)bufinfo.buf;
259267
} else if (MP_OBJ_IS_TYPE(a, &fficallback_type)) {
260268
mp_obj_fficallback_t *p = a;
261269
values[i] = (ffi_arg)p->func;
262270
} else {
263-
assert(0);
271+
goto error;
264272
}
265273
valueptrs[i] = &values[i];
266274
}
267275

268276
ffi_arg retval;
269277
ffi_call(&self->cif, self->func, &retval, valueptrs);
270278
return return_ffi_value(retval, self->rettype);
279+
280+
error:
281+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Don't know how to pass object to native function"));
271282
}
272283

273284
STATIC const mp_obj_type_t ffifunc_type = {

0 commit comments

Comments
 (0)