Add SocketPool.gaierror

gaierror(-2) is raised in the failure case of getaddrinfo. This is
compatible with cpython's socket module.

Typical session:
```
>>> import socketpool
>>> import wifi
>>> socket = socketpool.SocketPool(wifi.radio)
>>> try: socket.getaddrinfo("boo", 0)
... except socket.gaierror as e: ee = e
...
>>> type(ee)
<class 'gaierror'>
>>> ee.errno == socket.EAI_NONAME
True
>>> ee.strerror
'Name or service not known'
>>> raise ee
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <module>
gaierror: (-2, 'Name or service not known')
```

Closes: #6941
This commit is contained in:
Jeff Epler 2022-11-28 16:56:28 -06:00
parent ad2d190507
commit aef55b7e41
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
1 changed files with 18 additions and 1 deletions

View File

@ -36,6 +36,8 @@
#include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/socketpool/SocketPool.h"
#define SOCKETPOOL_EAI_NONAME (-2)
//| class SocketPool:
//| """A pool of socket resources available for the given radio. Only one
//| SocketPool can be created for each radio.
@ -63,12 +65,20 @@ STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t
return MP_OBJ_FROM_PTR(s);
}
//| class gaierror(OSError):
//| """Errors raised by getaddrinfo"""
//|
MP_DEFINE_EXCEPTION(gaierror, OSError)
//|
//| AF_INET: int
//| AF_INET6: int
//|
//| SOCK_STREAM: int
//| SOCK_DGRAM: int
//| SOCK_RAW: int
//| EAI_NONAME: int
//|
//| TCP_NODELAY: int
//|
@ -145,7 +155,11 @@ STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t
}
if (ip_str == mp_const_none) {
mp_raise_OSError(-2); // socket.EAI_NONAME from CPython
mp_obj_t exc_args[2] = {
MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_EAI_NONAME),
MP_OBJ_NEW_QSTR(MP_QSTR_Name_space_or_space_service_space_not_space_known),
};
nlr_raise(mp_obj_new_exception_args(&mp_type_gaierror, 2, exc_args));
}
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
@ -164,6 +178,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_getaddrinfo_obj, 1, sock
STATIC const mp_rom_map_elem_t socketpool_socketpool_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&socketpool_socketpool_socket_obj) },
{ MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&socketpool_socketpool_getaddrinfo_obj) },
{ MP_ROM_QSTR(MP_QSTR_gaierror), MP_ROM_PTR(&mp_type_gaierror) },
// class constants
{ MP_ROM_QSTR(MP_QSTR_AF_INET), MP_ROM_INT(SOCKETPOOL_AF_INET) },
@ -176,6 +191,8 @@ STATIC const mp_rom_map_elem_t socketpool_socketpool_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_TCP_NODELAY), MP_ROM_INT(SOCKETPOOL_TCP_NODELAY) },
{ MP_ROM_QSTR(MP_QSTR_IPPROTO_TCP), MP_ROM_INT(SOCKETPOOL_IPPROTO_TCP) },
{ MP_ROM_QSTR(MP_QSTR_EAI_NONAME), MP_ROM_INT(SOCKETPOOL_EAI_NONAME) },
};
STATIC MP_DEFINE_CONST_DICT(socketpool_socketpool_locals_dict, socketpool_socketpool_locals_dict_table);