Skip to content

Commit 1cc0165

Browse files
committed
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.
1 parent b149831 commit 1cc0165

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

select/select.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323

2424
class Epoll:
2525

26+
# Second value is actually of uint64_t size, so struct
27+
# will be smaller on 32bit, but seem to not segfault.
28+
epoll_event = "IO"
29+
2630
def __init__(self, epfd):
2731
self.epfd = epfd
28-
self.evbuf = struct.pack("IQ", 0, 0)
32+
self.evbuf = struct.pack(self.epoll_event, 0, 0)
2933

3034
def register(self, fd, eventmask=EPOLLIN|EPOLLPRI|EPOLLOUT, retval=None):
35+
"retval is extension to stdlib, value to use in results from .poll()."
3136
if retval is None:
3237
retval = fd
33-
s = struct.pack("IQ", eventmask, retval)
38+
s = struct.pack(self.epoll_event, eventmask, retval)
3439
r = epoll_ctl(self.epfd, EPOLL_CTL_ADD, fd, s)
3540
os.check_error(r)
3641

@@ -40,8 +45,8 @@ def poll(self, timeout=-1):
4045
os.check_error(n)
4146
res = []
4247
if n > 0:
43-
ev, h = struct.unpack("IQ", s)
44-
res.append((h, ev))
48+
vals = struct.unpack(self.epoll_event, s)
49+
res.append((vals[1], vals[0]))
4550
return res
4651

4752

select/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from setuptools import setup
66

77
setup(name='micropython-select',
8-
version='0.0.1',
8+
version='0.0.2',
99
description='select module to MicroPython',
1010
url='https://github.com/micropython/micropython/issues/405',
1111
author='Paul Sokolovsky',

select/test_epoll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
ep = select.epoll()
6-
ep.register(0, select.EPOLLIN)
6+
ep.register(0, select.EPOLLIN, (lambda x:x, (0,)))
77
res = ep.poll(2000)
88
print(res)
99
for ev, fd in res:

0 commit comments

Comments
 (0)
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