From 1cc01656f7457b095c6984e1ffff3046fd6a6d9f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 19 Apr 2014 23:41:35 +0300 Subject: [PATCH] select: Actually implement extended arg to epoll.register(). Allow to pass arbitrary Python objects as "callback data" to epoll, which will be then returned when activity on fd detected. --- select/select.py | 13 +++++++++---- select/setup.py | 2 +- select/test_epoll.py | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/select/select.py b/select/select.py index 1a25fc0b..cf3f1a18 100644 --- a/select/select.py +++ b/select/select.py @@ -23,14 +23,19 @@ EPOLL_CTL_MOD = 3 class Epoll: + # Second value is actually of uint64_t size, so struct + # will be smaller on 32bit, but seem to not segfault. + epoll_event = "IO" + def __init__(self, epfd): self.epfd = epfd - self.evbuf = struct.pack("IQ", 0, 0) + self.evbuf = struct.pack(self.epoll_event, 0, 0) def register(self, fd, eventmask=EPOLLIN|EPOLLPRI|EPOLLOUT, retval=None): + "retval is extension to stdlib, value to use in results from .poll()." if retval is None: retval = fd - s = struct.pack("IQ", eventmask, retval) + s = struct.pack(self.epoll_event, eventmask, retval) r = epoll_ctl(self.epfd, EPOLL_CTL_ADD, fd, s) os.check_error(r) @@ -40,8 +45,8 @@ class Epoll: os.check_error(n) res = [] if n > 0: - ev, h = struct.unpack("IQ", s) - res.append((h, ev)) + vals = struct.unpack(self.epoll_event, s) + res.append((vals[1], vals[0])) return res diff --git a/select/setup.py b/select/setup.py index 8ae9fabc..24c6a0c2 100644 --- a/select/setup.py +++ b/select/setup.py @@ -5,7 +5,7 @@ sys.path.pop(0) from setuptools import setup setup(name='micropython-select', - version='0.0.1', + version='0.0.2', description='select module to MicroPython', url='https://github.com/micropython/micropython/issues/405', author='Paul Sokolovsky', diff --git a/select/test_epoll.py b/select/test_epoll.py index c1626268..346fc2af 100644 --- a/select/test_epoll.py +++ b/select/test_epoll.py @@ -3,7 +3,7 @@ import os ep = select.epoll() -ep.register(0, select.EPOLLIN) +ep.register(0, select.EPOLLIN, (lambda x:x, (0,))) res = ep.poll(2000) print(res) for ev, fd in res: