Skip to content

Commit a4aaf82

Browse files
committed
unix/moduselect: Allow poll.register(), etc. accept fd-like objects.
This includes file and socket objects, backed by Unix file descriptor. This improves compatibility with stmhal's uselect (and convenience of use), though not completely: return value from poll.poll() is still raw file descriptor.
1 parent fdb411a commit a4aaf82

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

unix/fdfile.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2016 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/obj.h"
29+
30+
#ifndef __MICROPY_INCLUDED_UNIX_FILE_H__
31+
#define __MICROPY_INCLUDED_UNIX_FILE_H__
32+
33+
typedef struct _mp_obj_fdfile_t {
34+
mp_obj_base_t base;
35+
int fd;
36+
} mp_obj_fdfile_t;
37+
38+
extern const mp_obj_type_t mp_type_fileio;
39+
extern const mp_obj_type_t mp_type_textio;
40+
41+
#endif // __MICROPY_INCLUDED_UNIX_FILE_H__

unix/file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,14 @@
3636
#include "py/stream.h"
3737
#include "py/builtin.h"
3838
#include "py/mphal.h"
39+
#include "fdfile.h"
3940

4041
#if MICROPY_PY_IO
4142

4243
#ifdef _WIN32
4344
#define fsync _commit
4445
#endif
4546

46-
typedef struct _mp_obj_fdfile_t {
47-
mp_obj_base_t base;
48-
int fd;
49-
} mp_obj_fdfile_t;
50-
5147
#ifdef MICROPY_CPYTHON_COMPAT
5248
STATIC void check_fd_is_open(const mp_obj_fdfile_t *o) {
5349
if (o->fd < 0) {

unix/modsocket.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@
6262

6363
#define MICROPY_SOCKET_EXTRA (0)
6464

65+
// This type must "inherit" from mp_obj_fdfile_t, i.e. matching subset of
66+
// fields should have the same layout.
6567
typedef struct _mp_obj_socket_t {
6668
mp_obj_base_t base;
6769
int fd;
6870
} mp_obj_socket_t;
6971

70-
STATIC const mp_obj_type_t usocket_type;
72+
const mp_obj_type_t mp_type_socket;
7173

7274
// Helper functions
7375
#define RAISE_ERRNO(err_flag, error_val) \
@@ -80,7 +82,7 @@ static inline mp_obj_t mp_obj_from_sockaddr(const struct sockaddr *addr, socklen
8082

8183
STATIC mp_obj_socket_t *socket_new(int fd) {
8284
mp_obj_socket_t *o = m_new_obj(mp_obj_socket_t);
83-
o->base.type = &usocket_type;
85+
o->base.type = &mp_type_socket;
8486
o->fd = fd;
8587
return o;
8688
}
@@ -374,7 +376,7 @@ STATIC const mp_stream_p_t usocket_stream_p = {
374376
.write = socket_write,
375377
};
376378

377-
STATIC const mp_obj_type_t usocket_type = {
379+
const mp_obj_type_t mp_type_socket = {
378380
{ &mp_type_type },
379381
.name = MP_QSTR_socket,
380382
.print = socket_print,
@@ -550,7 +552,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_sockaddr_obj, mod_socket_sockaddr);
550552

551553
STATIC const mp_rom_map_elem_t mp_module_socket_globals_table[] = {
552554
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usocket) },
553-
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&usocket_type) },
555+
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_type_socket) },
554556
{ MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&mod_socket_getaddrinfo_obj) },
555557
{ MP_ROM_QSTR(MP_QSTR_inet_pton), MP_ROM_PTR(&mod_socket_inet_pton_obj) },
556558
{ MP_ROM_QSTR(MP_QSTR_inet_ntop), MP_ROM_PTR(&mod_socket_inet_ntop_obj) },

unix/moduselect.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include "py/objlist.h"
3939
#include "py/objtuple.h"
4040
#include "py/mphal.h"
41+
#include "fdfile.h"
42+
43+
extern const mp_obj_type_t mp_type_socket;
4144

4245
// Flags for poll()
4346
#define FLAG_ONESHOT (1)
@@ -51,10 +54,23 @@ typedef struct _mp_obj_poll_t {
5154
struct pollfd *entries;
5255
} mp_obj_poll_t;
5356

57+
STATIC int get_fd(mp_obj_t fdlike) {
58+
int fd;
59+
// Shortcut for fdfile compatible types
60+
if (MP_OBJ_IS_TYPE(fdlike, &mp_type_fileio) || MP_OBJ_IS_TYPE(fdlike, &mp_type_socket)) {
61+
mp_obj_fdfile_t *fdfile = MP_OBJ_TO_PTR(fdlike);
62+
fd = fdfile->fd;
63+
} else {
64+
fd = mp_obj_get_int(fdlike);
65+
}
66+
return fd;
67+
}
68+
5469
/// \method register(obj[, eventmask])
5570
STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) {
5671
mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
57-
int fd = mp_obj_get_int(args[1]);
72+
int fd = get_fd(args[1]);
73+
5874
mp_uint_t flags;
5975
if (n_args == 3) {
6076
flags = mp_obj_get_int(args[2]);
@@ -95,7 +111,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
95111
STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
96112
mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
97113
struct pollfd *entries = self->entries;
98-
int fd = mp_obj_get_int(obj_in);
114+
int fd = get_fd(obj_in);
99115
for (int i = self->len - 1; i >= 0; i--) {
100116
if (entries->fd == fd) {
101117
entries->fd = -1;
@@ -113,7 +129,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
113129
STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
114130
mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
115131
struct pollfd *entries = self->entries;
116-
int fd = mp_obj_get_int(obj_in);
132+
int fd = get_fd(obj_in);
117133
for (int i = self->len - 1; i >= 0; i--) {
118134
if (entries->fd == fd) {
119135
entries->events = mp_obj_get_int(eventmask_in);

0 commit comments

Comments
 (0)