Browse Source

extmod/modlwip: Use mp_raise_OSError helper function.

Reduces esp8266 code size by about 230 bytes.
pull/2494/head
Damien George 8 years ago
parent
commit
06d0083468
  1. 48
      extmod/modlwip.c

48
extmod/modlwip.c

@ -598,11 +598,11 @@ STATIC mp_obj_t lwip_socket_make_new(const mp_obj_type_t *type, mp_uint_t n_args
case MOD_NETWORK_SOCK_STREAM: socket->pcb.tcp = tcp_new(); break; case MOD_NETWORK_SOCK_STREAM: socket->pcb.tcp = tcp_new(); break;
case MOD_NETWORK_SOCK_DGRAM: socket->pcb.udp = udp_new(); break; case MOD_NETWORK_SOCK_DGRAM: socket->pcb.udp = udp_new(); break;
//case MOD_NETWORK_SOCK_RAW: socket->pcb.raw = raw_new(); break; //case MOD_NETWORK_SOCK_RAW: socket->pcb.raw = raw_new(); break;
default: nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EINVAL))); default: mp_raise_OSError(MP_EINVAL);
} }
if (socket->pcb.tcp == NULL) { if (socket->pcb.tcp == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOMEM))); mp_raise_OSError(MP_ENOMEM);
} }
switch (socket->type) { switch (socket->type) {
@ -686,7 +686,7 @@ STATIC mp_obj_t lwip_socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
} }
if (err != ERR_OK) { if (err != ERR_OK) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_lookup_table[-err]))); mp_raise_OSError(error_lookup_table[-err]);
} }
return mp_const_none; return mp_const_none;
@ -698,15 +698,15 @@ STATIC mp_obj_t lwip_socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
mp_int_t backlog = mp_obj_get_int(backlog_in); mp_int_t backlog = mp_obj_get_int(backlog_in);
if (socket->pcb.tcp == NULL) { if (socket->pcb.tcp == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EBADF))); mp_raise_OSError(MP_EBADF);
} }
if (socket->type != MOD_NETWORK_SOCK_STREAM) { if (socket->type != MOD_NETWORK_SOCK_STREAM) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EOPNOTSUPP))); mp_raise_OSError(MP_EOPNOTSUPP);
} }
struct tcp_pcb *new_pcb = tcp_listen_with_backlog(socket->pcb.tcp, (u8_t)backlog); struct tcp_pcb *new_pcb = tcp_listen_with_backlog(socket->pcb.tcp, (u8_t)backlog);
if (new_pcb == NULL) { if (new_pcb == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOMEM))); mp_raise_OSError(MP_ENOMEM);
} }
socket->pcb.tcp = new_pcb; socket->pcb.tcp = new_pcb;
tcp_accept(new_pcb, _lwip_tcp_accept); tcp_accept(new_pcb, _lwip_tcp_accept);
@ -719,15 +719,15 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
lwip_socket_obj_t *socket = self_in; lwip_socket_obj_t *socket = self_in;
if (socket->pcb.tcp == NULL) { if (socket->pcb.tcp == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EBADF))); mp_raise_OSError(MP_EBADF);
} }
if (socket->type != MOD_NETWORK_SOCK_STREAM) { if (socket->type != MOD_NETWORK_SOCK_STREAM) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EOPNOTSUPP))); mp_raise_OSError(MP_EOPNOTSUPP);
} }
// I need to do this because "tcp_accepted", later, is a macro. // I need to do this because "tcp_accepted", later, is a macro.
struct tcp_pcb *listener = socket->pcb.tcp; struct tcp_pcb *listener = socket->pcb.tcp;
if (listener->state != LISTEN) { if (listener->state != LISTEN) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EINVAL))); mp_raise_OSError(MP_EINVAL);
} }
// accept incoming connection // accept incoming connection
@ -738,7 +738,7 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
if (socket->incoming.connection != NULL) break; if (socket->incoming.connection != NULL) break;
} }
if (socket->incoming.connection == NULL) { if (socket->incoming.connection == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT))); mp_raise_OSError(MP_ETIMEDOUT);
} }
} else { } else {
while (socket->incoming.connection == NULL) { while (socket->incoming.connection == NULL) {
@ -785,7 +785,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
lwip_socket_obj_t *socket = self_in; lwip_socket_obj_t *socket = self_in;
if (socket->pcb.tcp == NULL) { if (socket->pcb.tcp == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EBADF))); mp_raise_OSError(MP_EBADF);
} }
// get address // get address
@ -800,9 +800,9 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
case MOD_NETWORK_SOCK_STREAM: { case MOD_NETWORK_SOCK_STREAM: {
if (socket->state != STATE_NEW) { if (socket->state != STATE_NEW) {
if (socket->state == STATE_CONNECTED) { if (socket->state == STATE_CONNECTED) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EALREADY))); mp_raise_OSError(MP_EALREADY);
} else { } else {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EINPROGRESS))); mp_raise_OSError(MP_EINPROGRESS);
} }
} }
// Register our recieve callback. // Register our recieve callback.
@ -811,7 +811,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected); err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected);
if (err != ERR_OK) { if (err != ERR_OK) {
socket->state = STATE_NEW; socket->state = STATE_NEW;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_lookup_table[-err]))); mp_raise_OSError(error_lookup_table[-err]);
} }
socket->peer_port = (mp_uint_t)port; socket->peer_port = (mp_uint_t)port;
memcpy(socket->peer, &dest, sizeof(socket->peer)); memcpy(socket->peer, &dest, sizeof(socket->peer));
@ -822,7 +822,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
if (socket->state != STATE_CONNECTING) break; if (socket->state != STATE_CONNECTING) break;
} }
if (socket->state == STATE_CONNECTING) { if (socket->state == STATE_CONNECTING) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT))); mp_raise_OSError(MP_ETIMEDOUT);
} }
} else { } else {
while (socket->state == STATE_CONNECTING) { while (socket->state == STATE_CONNECTING) {
@ -843,7 +843,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
} }
if (err != ERR_OK) { if (err != ERR_OK) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_lookup_table[-err]))); mp_raise_OSError(error_lookup_table[-err]);
} }
return mp_const_none; return mp_const_none;
@ -855,7 +855,7 @@ STATIC void lwip_socket_check_connected(lwip_socket_obj_t *socket) {
// not connected // not connected
int _errno = error_lookup_table[-socket->state]; int _errno = error_lookup_table[-socket->state];
socket->state = _ERR_BADF; socket->state = _ERR_BADF;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); mp_raise_OSError(_errno);
} }
} }
@ -880,7 +880,7 @@ STATIC mp_obj_t lwip_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
} }
} }
if (ret == -1) { if (ret == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); mp_raise_OSError(_errno);
} }
return mp_obj_new_int_from_uint(ret); return mp_obj_new_int_from_uint(ret);
@ -909,7 +909,7 @@ STATIC mp_obj_t lwip_socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
} }
} }
if (ret == -1) { if (ret == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); mp_raise_OSError(_errno);
} }
if (ret == 0) { if (ret == 0) {
@ -944,7 +944,7 @@ STATIC mp_obj_t lwip_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t
} }
} }
if (ret == -1) { if (ret == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); mp_raise_OSError(_errno);
} }
return mp_obj_new_int_from_uint(ret); return mp_obj_new_int_from_uint(ret);
@ -977,7 +977,7 @@ STATIC mp_obj_t lwip_socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
} }
} }
if (ret == -1) { if (ret == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); mp_raise_OSError(_errno);
} }
mp_obj_t tuple[2]; mp_obj_t tuple[2];
@ -1010,7 +1010,7 @@ STATIC mp_obj_t lwip_socket_sendall(mp_obj_t self_in, mp_obj_t buf_in) {
// most useful behavior is: check whether we will be able to send all of input // most useful behavior is: check whether we will be able to send all of input
// data without EAGAIN, and if won't be, raise it without sending any. // data without EAGAIN, and if won't be, raise it without sending any.
if (bufinfo.len > tcp_sndbuf(socket->pcb.tcp)) { if (bufinfo.len > tcp_sndbuf(socket->pcb.tcp)) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EAGAIN))); mp_raise_OSError(MP_EAGAIN);
} }
} }
// TODO: In CPython3.5, socket timeout should apply to the // TODO: In CPython3.5, socket timeout should apply to the
@ -1018,7 +1018,7 @@ STATIC mp_obj_t lwip_socket_sendall(mp_obj_t self_in, mp_obj_t buf_in) {
while (bufinfo.len != 0) { while (bufinfo.len != 0) {
ret = lwip_tcp_send(socket, bufinfo.buf, bufinfo.len, &_errno); ret = lwip_tcp_send(socket, bufinfo.buf, bufinfo.len, &_errno);
if (ret == -1) { if (ret == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); mp_raise_OSError(_errno);
} }
bufinfo.len -= ret; bufinfo.len -= ret;
bufinfo.buf = (char*)bufinfo.buf + ret; bufinfo.buf = (char*)bufinfo.buf + ret;
@ -1259,7 +1259,7 @@ STATIC mp_obj_t lwip_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
if (state.status < 0) { if (state.status < 0) {
// TODO: CPython raises gaierror, we raise with native lwIP negative error // TODO: CPython raises gaierror, we raise with native lwIP negative error
// values, to differentiate from normal errno's at least in such way. // values, to differentiate from normal errno's at least in such way.
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(state.status))); mp_raise_OSError(state.status);
} }
mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL);

Loading…
Cancel
Save