8000 stmhal: Use mp_raise_OSError helper function. · boneskull/circuitpython@a2bfcbe · GitHub
[go: up one dir, main page]

Skip to content

Commit a2bfcbe

Browse files
committed
stmhal: Use mp_raise_OSError helper function.
1 parent e3d2999 commit a2bfcbe

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

stmhal/moduos.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string.h>
2929

3030
#include "py/mpstate.h"
31+
#include "py/runtime.h"
3132
#include "py/objtuple.h"
3233
#include "py/objstr.h"
3334
#include "genhdr/mpversion.h"
@@ -107,7 +108,7 @@ STATIC mp_obj_t os_getcwd(void) {
107108
FRESULT res = f_getcwd(buf, sizeof buf);
108109

109110
if (res != FR_OK) {
110-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
111+
mp_raise_OSError(fresult_to_errno_table[res]);
111112
}
112113

113114
return mp_obj_new_str(buf, strlen(buf), false);
@@ -258,8 +259,7 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
258259
res = f_stat(path, &fno);
259260
}
260261
if (res != FR_OK) {
261-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
262-
MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
262+
mp_raise_OSError(fresult_to_errno_table[res]);
263263
}
264264
}
265265

@@ -319,7 +319,7 @@ STATIC mp_obj_t os_statvfs(mp_obj_t path_in) {
319319
return t;
320320

321321
error:
322-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
322+
mp_raise_OSError(fresult_to_errno_table[res]);
323323
}
324324
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs);
325325

stmhal/moduselect.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <stdio.h>
2828

29-
#include "py/nlr.h"
29+
#include "py/runtime.h"
3030
#include "py/obj.h"
3131
#include "py/objlist.h"
3232
#include "py/mperrno.h"
@@ -89,7 +89,7 @@ STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map 10000 , mp_uint_t *rwx_num) {
8989

9090
if (ret == -1) {
9191
// error doing ioctl
92-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errcode)));
92+
mp_raise_OSError(errcode);
9393
}
9494

9595
if (ret != 0) {
@@ -213,7 +213,7 @@ STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmas
213213
mp_obj_poll_t *self = self_in;
214214
mp_map_elem_t *elem = mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP);
215215
if (elem == NULL) {
216-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOENT)));
216+
mp_raise_OSError(MP_ENOENT);
217217
}
218218
((poll_obj_t*)elem->value)->flags = mp_obj_get_int(eventmask_in);
219219
return mp_const_none;

stmhal/modusocket.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ STATIC void socket_select_nic(mod_network_socket_obj_t *self, const byte *ip) {
7676
// call the NIC to open the socket
7777
int _errno;
7878
if (self->nic_type->socket(self, &_errno) != 0) {
79-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
79+
mp_raise_OSError(_errno);
8080
}
8181
}
8282
}
@@ -105,7 +105,7 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
105105
// call the NIC to bind the socket
106106
int _errno;
107107
if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
108-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
108+
mp_raise_OSError(_errno);
109109
}
110110

111111
return mp_const_none;
@@ -119,12 +119,12 @@ STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
119119
if (self->nic == MP_OBJ_NULL) {
120120
// not connected
121121
// TODO I th 10000 ink we can listen even if not bound...
122-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOTCONN)));
122+
mp_raise_OSError(MP_ENOTCONN);
123123
}
124124

125125
int _errno;
126126
if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
127-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
127+
mp_raise_OSError(_errno);
128128
}
129129

130130
return mp_const_none;
@@ -147,7 +147,7 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
147147
mp_uint_t port;
148148
int _errno;
149149
if (self->nic_type->accept(self, socket2, ip, &port, &_errno) != 0) {
150-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
150+
mp_raise_OSError(_errno);
151151
}
152152

153153
// new socket has valid state, so set the NIC to the same as parent
@@ -177,7 +177,7 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
177177
// call the NIC to connect the socket
178178
int _errno;
179179
if (self->nic_type->connect(self, ip, port, &_errno) != 0) {
180-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
180+
mp_raise_OSError(_errno);
181181
}
182182

183183
return mp_const_none;
@@ -189,14 +189,14 @@ STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
189189
mod_network_socket_obj_t *self = self_in;
190190
if (self->nic == MP_OBJ_NULL) {
191191
// not connected
192-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_EPIPE)));
192+
mp_raise_OSError(MP_EPIPE);
193193
}
194194
mp_buffer_info_t bufinfo;
195195
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
196196
int _errno;
197197
mp_uint_t ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno);
198198
if (ret == -1) {
199-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
199+
mp_raise_OSError(_errno);
200200
}
201201
return mp_obj_new_int_from_uint(ret);
202202
}
@@ -207,15 +207,15 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
207207
mod_network_socket_obj_t *self = self_in;
208208
if (self->nic == MP_OBJ_NULL) {
209209
// not connected
210-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOTCONN)));
210+
mp_raise_OSError(MP_ENOTCONN);
211211
}
212212
mp_int_t len = mp_obj_get_int(len_in);
213213
vstr_t vstr;
214214
vstr_init_len(&vstr, len);
215215
int _errno;
216216
mp_uint_t ret = self->nic_type->recv(self, (byte*)vstr.buf, len, &_errno);
217217
if (ret == -1) {
218-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
218+
mp_raise_OSError(_errno);
219219
}
220220
if (ret == 0) {
221221
return mp_const_empty_bytes;
@@ -244,7 +244,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_
244244
int _errno;
245245
mp_int_t ret = self->nic_type->sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
246246
if (ret == -1) {
247-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
247+
mp_raise_OSError(_errno);
248248
}
249249

250250
return mp_obj_new_int(ret);
@@ -256,7 +256,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
256256
mod_network_socket_obj_t *self = self_in;
257257
if (self->nic == MP_OBJ_NULL) {
258258
// not connected
259-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOTCONN)));
259+
mp_raise_OSError(MP_ENOTCONN);
260260
}
261261
vstr_t vstr;
262262
vstr_init_len(&vstr, mp_obj_get_int(len_in));
@@ -265,7 +265,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
265265
int _errno;
266266
mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
267267
if (ret == -1) {
268-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
268+
mp_raise_OSError(_errno);
269269
}
270270
mp_obj_t tuple[2];
271271
if (ret == 0) {
@@ -302,7 +302,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) {
302302

303303
int _errno;
304304
if (self->nic_type->setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
305-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
305+
mp_raise_OSError(_errno);
306306
}
307307

308308
return mp_const_none;
@@ -317,7 +317,7 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
317317
mod_network_socket_obj_t *self = self_in;
318318
if (self->nic == MP_OBJ_NULL) {
319319
// not connected
320-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ENOTCONN)));
320+
mp_raise_OSError(MP_ENOTCONN);
321321
}
322322
mp_uint_t timeout;
323323
if (timeout_in == mp_const_none) {
@@ -331,7 +331,7 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
331331
}
332332
int _errno;
333333
if (self->nic_type->settimeout(self, timeout, &_errno) != 0) {
334-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
334+
mp_raise_OSError(_errno);
335335
}
336336
return mp_const_none;
337337
}
@@ -401,7 +401,7 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
401401
int ret = nic_type->gethostbyname(nic, host, hlen, out_ip);
402402
if (ret != 0) {
403403
// TODO CPython raises: socket.gaierror: [Errno -2] Name or service not known
404-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ret)));
404+
mp_raise_OSError(ret);
405405
}
406406
mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL);
407407
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_AF_INET);

stmhal/mphalport.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <string.h>
22

33
#include "py/mpstate.h"
4+
#include "py/runtime.h"
45
#include "py/mperrno.h"
56
#include "py/mphal.h"
67
#include "usb.h"
@@ -15,7 +16,7 @@ const byte mp_hal_status_to_errno_table[4] = {
1516
};
1617

1718
NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
18-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(mp_hal_status_to_errno_table[status])));
19+
mp_raise_OSError(mp_hal_status_to_errno_table[status]);
1920
}
2021

2122
void mp_hal_set_interrupt_char(int c) {

0 commit comments

Comments
 (0)
0