Skip to content

Commit 3aa8ee7

Browse files
committed
py: Add mp_get_buffer(), mp_get_buffer_raise() convenience functions to API.
1 parent 2b00919 commit 3aa8ee7

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

py/obj.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,21 @@ mp_obj_t mp_identity(mp_obj_t self) {
330330
return self;
331331
}
332332
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
333+
334+
bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) {
335+
mp_obj_base_t *o = (mp_obj_base_t *)obj;
336+
if (o->type->buffer_p.get_buffer == NULL) {
337+
return false;
338+
}
339+
o->type->buffer_p.get_buffer(o, bufinfo, BUFFER_READ);
340+
if (bufinfo->buf == NULL) {
341+
return false;
342+
}
343+
return true;
344+
}
345+
346+
void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo) {
347+
if (!mp_get_buffer(obj, bufinfo)) {
348+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Object with buffer protocol required"));
349+
}
350+
}

py/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ typedef struct _buffer_info_t {
196196
typedef struct _mp_buffer_p_t {
197197
machine_int_t (*get_buffer)(mp_obj_t obj, buffer_info_t *bufinfo, int flags);
198198
} mp_buffer_p_t;
199+
bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo);
200+
void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo);
199201

200202
// Stream protocol
201203
typedef struct _mp_stream_p_t {

unix/modsocket.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ STATIC const mp_obj_type_t microsocket_type;
3434
{ if (err_flag == -1) \
3535
{ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error_val)); } }
3636

37-
STATIC void get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) {
38-
mp_obj_base_t *o = (mp_obj_base_t *)obj;
39-
if (o->type->buffer_p.get_buffer == NULL) {
40-
goto error;
41-
}
42-
o->type->buffer_p.get_buffer(o, bufinfo, BUFFER_READ);
43-
if (bufinfo->buf == NULL) {
44-
goto error;
45-
}
46-
return;
47-
48-
error:
49-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Operation not supported"));
50-
}
51-
5237
STATIC mp_obj_socket_t *socket_new(int fd) {
5338
mp_obj_socket_t *o = m_new_obj(mp_obj_socket_t);
5439
o->base.type = &microsocket_type;
@@ -96,7 +81,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_fileno_obj, socket_fileno);
9681
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
9782
mp_obj_socket_t *self = self_in;
9883
buffer_info_t bufinfo;
99-
get_buffer(addr_in, &bufinfo);
84+
mp_get_buffer_raise(addr_in, &bufinfo);
10085
int r = connect(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
10186
RAISE_ERRNO(r, errno);
10287
return mp_const_none;
@@ -106,7 +91,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
10691
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
10792
mp_obj_socket_t *self = self_in;
10893
buffer_info_t bufinfo;
109-
get_buffer(addr_in, &bufinfo);
94+
mp_get_buffer_raise(addr_in, &bufinfo);
11095
int r = bind(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
11196
RAISE_ERRNO(r, errno);
11297
return mp_const_none;
@@ -184,7 +169,7 @@ STATIC mp_obj_t socket_setsockopt(uint n_args, const mp_obj_t *args) {
184169
optlen = sizeof(val);
185170
} else {
186171
buffer_info_t bufinfo;
187-
get_buffer(args[3], &bufinfo);
172+
mp_get_buffer_raise(args[3], &bufinfo);
188173
optval = bufinfo.buf;
189174
optlen = bufinfo.len;
190175
}

0 commit comments

Comments
 (0)