Skip to content

Commit e8398a5

Browse files
committed
extmod: Update to use new mp_get_stream helper.
With this patch objects are only checked that they have the stream protocol at the start of their use as a stream, and afterwards the efficient mp_get_stream() helper is used to extract the stream protocol C methods.
1 parent 6abede2 commit e8398a5

6 files changed

Lines changed: 17 additions & 14 deletions

File tree

extmod/modujson.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
#if MICROPY_PY_UJSON
3636

3737
STATIC mp_obj_t mod_ujson_dump(mp_obj_t obj, mp_obj_t stream) {
38-
if (!MP_OBJ_IS_OBJ(stream)) {
39-
mp_raise_TypeError(NULL);
40-
}
38+
mp_get_stream_raise(stream, MP_STREAM_OP_WRITE);
4139
mp_print_t print = {MP_OBJ_TO_PTR(stream), mp_stream_write_adaptor};
4240
mp_obj_print_helper(&print, obj, PRINT_JSON);
4341
return mp_const_none;

extmod/modussl_mbedtls.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, cons
7676
STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
7777
mp_obj_t sock = *(mp_obj_t*)ctx;
7878

79-
const mp_stream_p_t *sock_stream = mp_get_stream_raise(sock, MP_STREAM_OP_WRITE);
79+
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
8080
int err;
8181

8282
mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err);
@@ -93,7 +93,7 @@ STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
9393
STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
9494
mp_obj_t sock = *(mp_obj_t*)ctx;
9595

96-
const mp_stream_p_t *sock_stream = mp_get_stream_raise(sock, MP_STREAM_OP_READ);
96+
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
9797
int err;
9898

9999
mp_uint_t out_sz = sock_stream->read(sock, buf, len, &err);
@@ -109,6 +109,9 @@ STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
109109

110110

111111
STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
112+
// Verify the socket object has the full stream protocol
113+
mp_get_stream_raise(sock, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
114+
112115
#if MICROPY_PY_USSL_FINALISER
113116
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
114117
#else

extmod/moduzlib.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ STATIC unsigned char read_src_stream(TINF_DATA *data) {
5353
p -= offsetof(mp_obj_decompio_t, decomp);
5454
mp_obj_decompio_t *self = (mp_obj_decompio_t*)p;
5555

56-
const mp_stream_p_t *stream = mp_get_stream_raise(self->src_stream, MP_STREAM_OP_READ);
56+
const mp_stream_p_t *stream = mp_get_stream(self->src_stream);
5757
int err;
5858
byte c;
5959
mp_uint_t out_sz = stream->read(self->src_stream, &c, 1, &err);
@@ -68,6 +68,7 @@ STATIC unsigned char read_src_stream(TINF_DATA *data) {
6868

6969
STATIC mp_obj_t decompio_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
7070
mp_arg_check_num(n_args, n_kw, 1, 2, false);
71+
mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
7172
mp_obj_decompio_t *o = m_new_obj(mp_obj_decompio_t);
7273
o->base.type = type;
7374
memset(&o->decomp, 0, sizeof(o->decomp));

extmod/modwebrepl.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ STATIC char denied_prompt[] = "\r\nAccess denied\r\n";
7676
STATIC char webrepl_passwd[10];
7777

7878
STATIC void write_webrepl(mp_obj_t websock, const void *buf, size_t len) {
79-
const mp_stream_p_t *sock_stream = mp_get_stream_raise(websock, MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
79+
const mp_stream_p_t *sock_stream = mp_get_stream(websock);
8080
int err;
8181
int old_opts = sock_stream->ioctl(websock, MP_STREAM_SET_DATA_OPTS, FRAME_BIN, &err);
8282
sock_stream->write(websock, buf, len, &err);
@@ -86,7 +86,7 @@ STATIC void write_webrepl(mp_obj_t websock, const void *buf, size_t len) {
8686
#define SSTR(s) s, sizeof(s) - 1
8787
STATIC void write_webrepl_str(mp_obj_t websock, const char *str, int sz) {
8888
int err;
89-
const mp_stream_p_t *sock_stream = mp_get_stream_raise(websock, MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
89+
const mp_stream_p_t *sock_stream = mp_get_stream(websock);
9090
sock_stream->write(websock, str, sz, &err);
9191
}
9292

@@ -110,8 +110,7 @@ STATIC mp_obj_t webrepl_make_new(const mp_obj_type_t *type, size_t n_args, size_
110110
}
111111

112112
STATIC int write_file_chunk(mp_obj_webrepl_t *self) {
113-
const mp_stream_p_t *file_stream =
114-
mp_get_stream_raise(self->cur_file, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
113+
const mp_stream_p_t *file_stream = mp_get_stream(self->cur_file);
115114
byte readbuf[2 + 256];
116115
int err;
117116
mp_uint_t out_sz = file_stream->read(self->cur_file, readbuf + 2, sizeof(readbuf) - 2, &err);
@@ -181,7 +180,7 @@ STATIC mp_uint_t _webrepl_read(mp_obj_t self_in, void *buf, mp_uint_t size, int
181180
// We know that os.dupterm always calls with size = 1
182181
assert(size == 1);
183182
mp_obj_webrepl_t *self = self_in;
184-
const mp_stream_p_t *sock_stream = mp_get_stream_raise(self->sock, MP_STREAM_OP_READ);
183+
const mp_stream_p_t *sock_stream = mp_get_stream(self->sock);
185184
mp_uint_t out_sz = sock_stream->read(self->sock, buf, size, errcode);
186185
//DEBUG_printf("webrepl: Read %d initial bytes from websocket\n", out_sz);
187186
if (out_sz == 0 || out_sz == MP_STREAM_ERROR) {
@@ -293,7 +292,7 @@ STATIC mp_uint_t webrepl_write(mp_obj_t self_in, const void *buf, mp_uint_t size
293292
// Don't forward output until passwd is entered
294293
return size;
295294
}
296-
const mp_stream_p_t *stream_p = mp_get_stream_raise(self->sock, MP_STREAM_OP_WRITE);
295+
const mp_stream_p_t *stream_p = mp_get_stream(self->sock);
297296
return stream_p->write(self->sock, buf, size, errcode);
298297
}
299298

extmod/modwebsocket.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ STATIC mp_uint_t websocket_write(mp_obj_t self_in, const void *buf, mp_uint_t si
5959

6060
STATIC mp_obj_t websocket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
6161
mp_arg_check_num(n_args, n_kw, 1, 2, false);
62+
mp_get_stream_raise(args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
6263
mp_obj_websocket_t *o = m_new_obj(mp_obj_websocket_t);
6364
o->base.type = type;
6465
o->sock = args[0];
@@ -75,7 +76,7 @@ STATIC mp_obj_t websocket_make_new(const mp_obj_type_t *type, size_t n_args, siz
7576

7677
STATIC mp_uint_t websocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
7778
mp_obj_websocket_t *self = MP_OBJ_TO_PTR(self_in);
78-
const mp_stream_p_t *stream_p = mp_get_stream_raise(self->sock, MP_STREAM_OP_READ);
79+
const mp_stream_p_t *stream_p = mp_get_stream(self->sock);
7980
while (1) {
8081
if (self->to_recv != 0) {
8182
mp_uint_t out_sz = stream_p->read(self->sock, self->buf + self->buf_pos, self->to_recv, errcode);

extmod/uos_dupterm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int mp_uos_dupterm_rx_chr(void) {
6262
if (nlr_push(&nlr) == 0) {
6363
byte buf[1];
6464
int errcode;
65-
const mp_stream_p_t *stream_p = mp_get_stream_raise(MP_STATE_VM(dupterm_objs[idx]), MP_STREAM_OP_READ);
65+
const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_VM(dupterm_objs[idx]));
6666
mp_uint_t out_sz = stream_p->read(MP_STATE_VM(dupterm_objs[idx]), buf, 1, &errcode);
6767
if (out_sz == 0) {
6868
nlr_pop();
@@ -125,6 +125,7 @@ STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) {
125125
if (args[0] == mp_const_none) {
126126
MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL;
127127
} else {
128+
mp_get_stream_raise(args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
128129
MP_STATE_VM(dupterm_objs[idx]) = args[0];
129130
}
130131

0 commit comments

Comments
 (0)