Commit a4aaf824 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

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.
parent fdb411a8
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2016 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#ifndef __MICROPY_INCLUDED_UNIX_FILE_H__
#define __MICROPY_INCLUDED_UNIX_FILE_H__
typedef struct _mp_obj_fdfile_t {
mp_obj_base_t base;
int fd;
} mp_obj_fdfile_t;
extern const mp_obj_type_t mp_type_fileio;
extern const mp_obj_type_t mp_type_textio;
#endif // __MICROPY_INCLUDED_UNIX_FILE_H__
......@@ -36,6 +36,7 @@
#include "py/stream.h"
#include "py/builtin.h"
#include "py/mphal.h"
#include "fdfile.h"
#if MICROPY_PY_IO
......@@ -43,11 +44,6 @@
#define fsync _commit
#endif
typedef struct _mp_obj_fdfile_t {
mp_obj_base_t base;
int fd;
} mp_obj_fdfile_t;
#ifdef MICROPY_CPYTHON_COMPAT
STATIC void check_fd_is_open(const mp_obj_fdfile_t *o) {
if (o->fd < 0) {
......
......@@ -62,12 +62,14 @@
#define MICROPY_SOCKET_EXTRA (0)
// This type must "inherit" from mp_obj_fdfile_t, i.e. matching subset of
// fields should have the same layout.
typedef struct _mp_obj_socket_t {
mp_obj_base_t base;
int fd;
} mp_obj_socket_t;
STATIC const mp_obj_type_t usocket_type;
const mp_obj_type_t mp_type_socket;
// Helper functions
#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
STATIC mp_obj_socket_t *socket_new(int fd) {
mp_obj_socket_t *o = m_new_obj(mp_obj_socket_t);
o->base.type = &usocket_type;
o->base.type = &mp_type_socket;
o->fd = fd;
return o;
}
......@@ -374,7 +376,7 @@ STATIC const mp_stream_p_t usocket_stream_p = {
.write = socket_write,
};
STATIC const mp_obj_type_t usocket_type = {
const mp_obj_type_t mp_type_socket = {
{ &mp_type_type },
.name = MP_QSTR_socket,
.print = socket_print,
......@@ -550,7 +552,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_socket_sockaddr_obj, mod_socket_sockaddr);
STATIC const mp_rom_map_elem_t mp_module_socket_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usocket) },
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&usocket_type) },
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_type_socket) },
{ MP_ROM_QSTR(MP_QSTR_getaddrinfo), MP_ROM_PTR(&mod_socket_getaddrinfo_obj) },
{ MP_ROM_QSTR(MP_QSTR_inet_pton), MP_ROM_PTR(&mod_socket_inet_pton_obj) },
{ MP_ROM_QSTR(MP_QSTR_inet_ntop), MP_ROM_PTR(&mod_socket_inet_ntop_obj) },
......
......@@ -38,6 +38,9 @@
#include "py/objlist.h"
#include "py/objtuple.h"
#include "py/mphal.h"
#include "fdfile.h"
extern const mp_obj_type_t mp_type_socket;
// Flags for poll()
#define FLAG_ONESHOT (1)
......@@ -51,10 +54,23 @@ typedef struct _mp_obj_poll_t {
struct pollfd *entries;
} mp_obj_poll_t;
STATIC int get_fd(mp_obj_t fdlike) {
int fd;
// Shortcut for fdfile compatible types
if (MP_OBJ_IS_TYPE(fdlike, &mp_type_fileio) || MP_OBJ_IS_TYPE(fdlike, &mp_type_socket)) {
mp_obj_fdfile_t *fdfile = MP_OBJ_TO_PTR(fdlike);
fd = fdfile->fd;
} else {
fd = mp_obj_get_int(fdlike);
}
return fd;
}
/// \method register(obj[, eventmask])
STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) {
mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
int fd = mp_obj_get_int(args[1]);
int fd = get_fd(args[1]);
mp_uint_t flags;
if (n_args == 3) {
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);
STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
struct pollfd *entries = self->entries;
int fd = mp_obj_get_int(obj_in);
int fd = get_fd(obj_in);
for (int i = self->len - 1; i >= 0; i--) {
if (entries->fd == fd) {
entries->fd = -1;
......@@ -113,7 +129,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
struct pollfd *entries = self->entries;
int fd = mp_obj_get_int(obj_in);
int fd = get_fd(obj_in);
for (int i = self->len - 1; i >= 0; i--) {
if (entries->fd == fd) {
entries->events = mp_obj_get_int(eventmask_in);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment