Skip to content

Commit b11b85a

Browse files
committed
py: Allow to pass buffer protocol flags to get_buffer helper funcs.
1 parent a8f5d15 commit b11b85a

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

py/modstruct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
5656
uint size = calcsize_items(fmt);
5757
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
5858
mp_buffer_info_t bufinfo;
59-
mp_get_buffer_raise(data_in, &bufinfo);
59+
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
6060
byte *p = bufinfo.buf;
6161

6262
for (uint i = 0; i < size; i++) {

py/obj.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,20 @@ mp_obj_t mp_identity(mp_obj_t self) {
357357
}
358358
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
359359

360-
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo) {
360+
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags) {
361361
mp_obj_type_t *type = mp_obj_get_type(obj);
362362
if (type->buffer_p.get_buffer == NULL) {
363363
return false;
364364
}
365-
int ret = type->buffer_p.get_buffer(obj, bufinfo, MP_BUFFER_READ);
365+
int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
366366
if (ret != 0 || bufinfo->buf == NULL) {
367367
return false;
368368
}
369369
return true;
370370
}
371371

372-
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo) {
373-
if (!mp_get_buffer(obj, bufinfo)) {
372+
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags) {
373+
if (!mp_get_buffer(obj, bufinfo, flags)) {
374374
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
375375
}
376376
}

py/obj.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ typedef struct _mp_buffer_info_t {
209209
typedef struct _mp_buffer_p_t {
210210
machine_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
211211
} mp_buffer_p_t;
212-
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo);
213-
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo);
212+
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
213+
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
214214

215215
// Stream protocol
216216
typedef struct _mp_stream_p_t {

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
427427
return (machine_uint_t)items;
428428
} else {
429429
mp_buffer_info_t bufinfo;
430-
if (mp_get_buffer(obj, &bufinfo)) {
430+
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
431431
// supports the buffer protocol, return a pointer to the data
432432
return (machine_uint_t)bufinfo.buf;
433433
} else {

py/objint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ STATIC mp_obj_t int_from_bytes(uint n_args, const mp_obj_t *args) {
273273

274274
// get the buffer info
275275
mp_buffer_info_t bufinfo;
276-
mp_get_buffer_raise(args[1], &bufinfo);
276+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
277277

278278
// convert the bytes to an integer
279279
machine_uint_t value = 0;

unix/modsocket.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_fileno_obj, socket_fileno);
8181
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
8282
mp_obj_socket_t *self = self_in;
8383
mp_buffer_info_t bufinfo;
84-
mp_get_buffer_raise(addr_in, &bufinfo);
84+
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
8585
int r = connect(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
8686
RAISE_ERRNO(r, errno);
8787
return mp_const_none;
@@ -91,7 +91,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
9191
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
9292
mp_obj_socket_t *self = self_in;
9393
mp_buffer_info_t bufinfo;
94-
mp_get_buffer_raise(addr_in, &bufinfo);
94+
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
9595
int r = bind(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
9696
RAISE_ERRNO(r, errno);
9797
return mp_const_none;
@@ -169,7 +169,7 @@ STATIC mp_obj_t socket_setsockopt(uint n_args, const mp_obj_t *args) {
169169
optlen = sizeof(val);
170170
} else {
171171
mp_buffer_info_t bufinfo;
172-
mp_get_buffer_raise(args[3], &bufinfo);
172+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
173173
optval = bufinfo.buf;
174174
optlen = bufinfo.len;
175175
}

0 commit comments

Comments
 (0)