From 41e0cdefe79455a151a1a3cb0588c278a8361a97 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 4 Oct 2018 09:50:27 +0300 Subject: [PATCH 1/3] unix/moduselect: poll.unregister: Add optional throw=True param. If 2nd, positional only argument of False is passed to poll.unregister() method, then it won't throw exception if passed a stream object not registered with the poller, which was the previous behavior. Then default behavior is made compatible with CPython's, which is to throw exception in this case. --- ports/unix/moduselect.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ports/unix/moduselect.c b/ports/unix/moduselect.c index a95663e31fa03..8b84a96696083 100644 --- a/ports/unix/moduselect.c +++ b/ports/unix/moduselect.c @@ -130,7 +130,10 @@ STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register); /// \method unregister(obj) -STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { +STATIC mp_obj_t poll_unregister(size_t n_args, const mp_obj_t *args) { + mp_obj_t self_in = args[0]; + mp_obj_t obj_in = args[1]; + mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); struct pollfd *entries = self->entries; int fd = get_fd(obj_in); @@ -140,15 +143,19 @@ STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { if (self->obj_map) { self->obj_map[entries - self->entries] = MP_OBJ_NULL; } - break; + return mp_const_true; } entries++; } - // TODO raise KeyError if obj didn't exist in map - return mp_const_none; + // If "throw" arg if False, don't raise exception. + if (n_args > 2 && args[2] == mp_const_false) { + return mp_const_false; + } + + mp_raise_msg(&mp_type_KeyError, NULL); } -MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_unregister_obj, 2, 3, poll_unregister); /// \method modify(obj, eventmask) STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) { From fcec4c27300ffc80a0054375732ecb5371ecdf20 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 4 Oct 2018 12:43:55 +0300 Subject: [PATCH 2/3] tests/extmod/uselect_poll_basic: Test unregister() on stream not in poller. After previous change, poll.unregister() is now by default compliant with CPython behavior. --- tests/extmod/uselect_poll_basic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/extmod/uselect_poll_basic.py b/tests/extmod/uselect_poll_basic.py index 828fda1bbe359..830f4ec51c6a5 100644 --- a/tests/extmod/uselect_poll_basic.py +++ b/tests/extmod/uselect_poll_basic.py @@ -32,3 +32,8 @@ poller.modify(s, select.POLLIN) except OSError as e: assert e.args[0] == errno.ENOENT + +try: + poller.unregister(s) +except KeyError: + print("KeyError") From d98a9927e8ad52d040e539757085839f17105d78 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 6 Oct 2018 21:15:26 +0300 Subject: [PATCH 3/3] tests/uselect_poll_unreg: Test for throw=False arg for poll.unregister(). --- tests/extmod/uselect_poll_unreg.py | 19 +++++++++++++++++++ tests/extmod/uselect_poll_unreg.py.exp | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 tests/extmod/uselect_poll_unreg.py create mode 100644 tests/extmod/uselect_poll_unreg.py.exp diff --git a/tests/extmod/uselect_poll_unreg.py b/tests/extmod/uselect_poll_unreg.py new file mode 100644 index 0000000000000..7ab014f6d1938 --- /dev/null +++ b/tests/extmod/uselect_poll_unreg.py @@ -0,0 +1,19 @@ +# Test MicroPython's idempotent extension to poll.unregister(). +try: + import usocket as socket, uselect as select, uerrno as errno +except ImportError: + print("SKIP") + raise SystemExit + + +poller = select.poll() + +s = socket.socket() + +poller.register(s) + +print(poller.unregister(s, False)) + +# Can unregister multiple times +print(poller.unregister(s, False)) +print(poller.unregister(s, False)) diff --git a/tests/extmod/uselect_poll_unreg.py.exp b/tests/extmod/uselect_poll_unreg.py.exp new file mode 100644 index 0000000000000..06eb4d10eecd8 --- /dev/null +++ b/tests/extmod/uselect_poll_unreg.py.exp @@ -0,0 +1,3 @@ +True +False +False pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy