Skip to content

Commit d310ca2

Browse files
committed
esp32/modsocket: Make socket.recv take exactly 2 args.
Per CPython specs. Also optimise allocation of the recv buffer so it doesn't need to use the stack.
1 parent b6c833d commit d310ca2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

esp32/modsocket.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,20 @@ STATIC mp_obj_t socket_setblocking(const mp_obj_t arg0, const mp_obj_t arg1) {
202202
}
203203
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
204204

205-
STATIC mp_obj_t socket_recv(mp_uint_t n_args, const mp_obj_t *args) {
206-
byte buf[1024];
207-
socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
208-
size_t len = (n_args > 1) ? MIN(mp_obj_get_int(args[1]), sizeof(buf)) : sizeof(buf);
209-
int x = lwip_recvfrom_r(self->fd, buf, len, 0, NULL, NULL);
210-
if (x >= 0) return mp_obj_new_bytes(buf, x);
211-
if (errno == EWOULDBLOCK) return mp_obj_new_bytes(buf, 0);
205+
STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
206+
socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
207+
size_t len = mp_obj_get_int(len_in);
208+
vstr_t vstr;
209+
vstr_init_len(&vstr, len);
210+
int x = lwip_recvfrom_r(self->fd, vstr.buf, len, 0, NULL, NULL);
211+
if (x >= 0) {
212+
vstr.len = x;
213+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
214+
}
215+
if (errno == EWOULDBLOCK) return mp_const_empty_bytes;
212216
exception_from_errno(errno);
213217
}
214-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_recv_obj, 1, 2, socket_recv);
218+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);
215219

216220
STATIC mp_obj_t socket_send(const mp_obj_t arg0, const mp_obj_t arg1) {
217221
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy