diff options
author | Giampaolo Rodola' <g.rodola@gmail.com> | 2014-03-20 20:43:41 (GMT) |
---|---|---|
committer | Giampaolo Rodola' <g.rodola@gmail.com> | 2014-03-20 20:43:41 (GMT) |
commit | f97e82937f8a4e14df939007abacfa252ad09fc8 (patch) | |
tree | d771b2bbbcbaffcebbdc8988e572bdb7e2fdbcd5 /Lib/selectors.py | |
parent | 5782e25dfe401dddfde091417c977b44e4442cb6 (diff) | |
download | cpython-f97e82937f8a4e14df939007abacfa252ad09fc8.zip cpython-f97e82937f8a4e14df939007abacfa252ad09fc8.tar.gz cpython-f97e82937f8a4e14df939007abacfa252ad09fc8.tar.bz2 |
Fix issue 18931: selectors module now supports /dev/poll on Solaris.
Diffstat (limited to 'Lib/selectors.py')
-rw-r--r-- | Lib/selectors.py | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/Lib/selectors.py b/Lib/selectors.py index 9be9225..4e9ae6e 100644 --- a/Lib/selectors.py +++ b/Lib/selectors.py @@ -441,6 +441,64 @@ if hasattr(select, 'epoll'): super().close() +if hasattr(select, 'devpoll'): + + class DevpollSelector(_BaseSelectorImpl): + """Solaris /dev/poll selector.""" + + def __init__(self): + super().__init__() + self._devpoll = select.devpoll() + + def fileno(self): + return self._devpoll.fileno() + + def register(self, fileobj, events, data=None): + key = super().register(fileobj, events, data) + poll_events = 0 + if events & EVENT_READ: + poll_events |= select.POLLIN + if events & EVENT_WRITE: + poll_events |= select.POLLOUT + self._devpoll.register(key.fd, poll_events) + return key + + def unregister(self, fileobj): + key = super().unregister(fileobj) + self._devpoll.unregister(key.fd) + return key + + def select(self, timeout=None): + if timeout is None: + timeout = None + elif timeout <= 0: + timeout = 0 + else: + # devpoll() has a resolution of 1 millisecond, round away from + # zero to wait *at least* timeout seconds. + timeout = math.ceil(timeout * 1e3) + ready = [] + try: + fd_event_list = self._devpoll.poll(timeout) + except InterruptedError: + return ready + for fd, event in fd_event_list: + events = 0 + if event & ~select.POLLIN: + events |= EVENT_WRITE + if event & ~select.POLLOUT: + events |= EVENT_READ + + key = self._key_from_fd(fd) + if key: + ready.append((key, events & key.events)) + return ready + + def close(self): + self._devpoll.close() + super().close() + + if hasattr(select, 'kqueue'): class KqueueSelector(_BaseSelectorImpl): @@ -513,12 +571,14 @@ if hasattr(select, 'kqueue'): super().close() -# Choose the best implementation: roughly, epoll|kqueue > poll > select. +# Choose the best implementation: roughly, epoll|kqueue|devpoll > poll > select. # select() also can't accept a FD > FD_SETSIZE (usually around 1024) if 'KqueueSelector' in globals(): DefaultSelector = KqueueSelector elif 'EpollSelector' in globals(): DefaultSelector = EpollSelector +elif 'DevpollSelector' in globals(): + DefaultSelector = DevpollSelector elif 'PollSelector' in globals(): DefaultSelector = PollSelector else: |