/usr/lib/python3/dist-packages/plainbox/vendor/pyglibc/select.py is in python3-plainbox 0.25-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | # Copyright (c) 2014 Canonical Ltd.
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
:mod:`pyglibc.select` -- pyglibc-based pure-python select.py
============================================================
This module contains a re-implementation of the :mod:`select` module from
Python's standard library as of Python 3.4. It is compatible with Python 2.7+
(including Python 3) and supports all of the features (including setting
close-on-exec flags).
This module is considered stable public API. It will maintain backwards
compatibility for the foreseeable future. Any changes will be made to conform
more strictly with the original specification and the reference implementation
present in the python standard library.
Only features in scope for Linux are implemented. Obsolete select and poll
interfaces are not implemented. The API is deliberately kept identical to the
version from stdlib so that code can be ported from one to the other by mere
import swap.
"""
from __future__ import absolute_import
from ctypes import POINTER
from ctypes import byref
from ctypes import cast
from errno import EBADF
from threading import Lock
from plainbox.vendor.glibc import EPOLLERR
from plainbox.vendor.glibc import EPOLLET
from plainbox.vendor.glibc import EPOLLHUP
from plainbox.vendor.glibc import EPOLLIN
from plainbox.vendor.glibc import EPOLLMSG
from plainbox.vendor.glibc import EPOLLONESHOT
from plainbox.vendor.glibc import EPOLLOUT
from plainbox.vendor.glibc import EPOLLPRI
from plainbox.vendor.glibc import EPOLLRDBAND
from plainbox.vendor.glibc import EPOLLRDHUP
from plainbox.vendor.glibc import EPOLLRDNORM
from plainbox.vendor.glibc import EPOLLWRBAND
from plainbox.vendor.glibc import EPOLLWRNORM
from plainbox.vendor.glibc import EPOLL_CLOEXEC
from plainbox.vendor.glibc import EPOLL_CTL_ADD
from plainbox.vendor.glibc import EPOLL_CTL_DEL
from plainbox.vendor.glibc import EPOLL_CTL_MOD
from plainbox.vendor.glibc import FD_SETSIZE
from plainbox.vendor.glibc import close
from plainbox.vendor.glibc import epoll_create1
from plainbox.vendor.glibc import epoll_ctl
from plainbox.vendor.glibc import epoll_event
from plainbox.vendor.glibc import epoll_wait
__author__ = 'Zygmunt Krynicki <zygmunt.krynicki@canonical.com>'
__version__ = '1.0' # Let's claim this is complete and fix issues, if any
__all__ = ['epoll', 'EPOLL_CLOEXEC', 'EPOLLIN', 'EPOLLOUT', 'EPOLLPRI',
'EPOLLERR', 'EPOLLHUP', 'EPOLLET', 'EPOLLONESHOT', 'EPOLLRDNORM',
'EPOLLRDBAND', 'EPOLLWRNORM', 'EPOLLWRBAND', 'EPOLLMSG']
# NOTE: Extra features not present in Python 3.4
__all__ += ['EPOLLRDHUP']
def _err_closed():
raise ValueError("I/O operation on closed epoll object")
class epoll(object):
"""
Pure-python reimplementation of :class:`select.epoll` from Python 3.4
compatible with Python 2.7+.
"""
# Somewhat inefficient lock acquired on each call to epoll.close() to
# ensure that we match semantics from python stdlib where close can be
# called concurrently.
_close_lock = Lock()
def __init__(self, sizehint=-1, flags=0):
"""
:param sizehint:
Dummy argument for compatibility with select.epoll, ignored.
:param flags:
Flags passed to ``epoll_create1()``. Note that internally flags are
always OR-ed with EPOLL_CLOEXEC, matching what Python 3.4 does, so
passing 0 is perfectly fine.
"""
self._epfd = -1
self._epfd = epoll_create1(flags | EPOLL_CLOEXEC)
def __enter__(self):
"""
Enter a context manager
:returns:
self
:raises ValueError:
If :meth:`closed()` is True
"""
if self._epfd < 0:
_err_closed()
return self
def __exit__(self, *args):
"""
Exit a context manager
This method calls :meth:`close()`.
"""
self.close()
def close(self):
"""
Close the internal epoll file descriptor if it isn't closed
:raises OSError:
If the underlying ``close(2)`` fails. The error message matches
those found in the manual page.
"""
with self._close_lock:
epfd = self._epfd
if epfd >= 0:
self._epfd = -1
close(epfd)
@property
def closed(self):
"""
property indicating if the internal epoll descriptor was closed
"""
return self._epfd < 0
def fileno(self):
"""
Get the descriptor number obtained from ``epoll_create1()(2)``
:returns:
The descriptor number
:raises ValueError:
If :meth:`closed()` is True
"""
if self._epfd < 0:
_err_closed()
return self._epfd
@classmethod
def fromfd(cls, fd):
"""
Create a new epoll object from a given file descriptor
:param fd:
A pre-made file descriptor obtained from ``epoll_create(2)`` or
``epoll_create1(2)``
:raises ValueError:
If fd is not a valid file descriptor
:returns:
A new epoll object
.. note::
If the passed descriptor is incorrect then various methods will
fail and raise OSError with an appropriate message.
"""
if fd < 0:
_err_closed()
self = cls.__new__()
object.__init__(self)
self._epfd = fd
return self
def register(self, fd, eventmask=None):
"""
Register a new descriptor
:param fd:
The descriptor to register.
:param eventmask:
Bit-mask of events that will be monitored. By default EPOLLIN,
EPOLLOUT and EPOLLPRI are used. Note that EPOLLHUP is implicit and
doesn't need to be provided.
:raises ValueError:
If :meth:`closed()` is True
:raises OSError:
If the underlying ``epoll_ctl(2)`` fails. The error message matches
those found in the manual page.
"""
if self._epfd < 0:
_err_closed()
if eventmask is None:
eventmask = EPOLLIN | EPOLLOUT | EPOLLPRI
ev = epoll_event()
ev.events = eventmask
ev.data.fd = fd
epoll_ctl(self._epfd, EPOLL_CTL_ADD, fd, byref(ev))
def unregister(self, fd):
"""
Unregister a previously registered descriptor
:param fd:
The descriptor to unregister
:raises ValueError:
If :meth:`closed()` is True
:raises OSError:
If the underlying ``epoll_ctl(2)`` fails. The error message matches
those found in the manual page.
.. note::
For feature parity with Python 3.4, unlike what ``epoll_ctl(2)``
would do, we are silently ignoring ``EBADF`` which is raised if
"""
if self._epfd < 0:
_err_closed()
ev = epoll_event()
try:
epoll_ctl(self._epfd, EPOLL_CTL_DEL, fd, byref(ev))
except OSError as exc:
# Allow fd to be closed, matching Python 3.4
if exc.errno != EBADF:
raise
def modify(self, fd, eventmask):
"""
Change the bit-mask of events associated with a previously-registered
descriptor.
:param fd:
The descriptor to modify.
:param eventmask:
New bit-mask of events that will be monitored.
:raises ValueError:
If :meth:`closed()` is True
:raises OSError:
If the underlying ``epoll_ctl(2)`` fails. The error message matches
those found in the manual page.
"""
if self._epfd < 0:
_err_closed()
ev = epoll_event()
ev.events = eventmask
ev.data.fd = fd
epoll_ctl(self._epfd, EPOLL_CTL_MOD, fd, byref(ev))
def poll(self, timeout=-1, maxevents=-1):
"""
Poll for events
:param timeout:
The amount of seconds to wait for events before giving up. The
default value, -1, represents infinity. Note that unlike the
underlying ``epoll_wait()`` timeout is a fractional number
representing **seconds**.
:param maxevents:
The maximum number of events to report. The default is a
reasonably-sized maximum, identical to the one selected by
Python 3.4.
:returns:
A list of (fd, events) that were reported or an empty list if the
timeout elapsed.
:raises ValueError:
If :meth:`closed()` is True
:raises OSError:
If the underlying ``epoll_wait(2)`` fails. The error message
matches those found in the manual page.
"""
if self._epfd < 0:
_err_closed()
if timeout != -1:
# 1000 because epoll_wait(2) uses milliseconds
timeout = int(timeout * 1000)
if maxevents == -1:
maxevents = FD_SETSIZE - 1
events = (epoll_event * maxevents)()
num_events = epoll_wait(
self._epfd, cast(byref(events), POINTER(epoll_event)),
maxevents, timeout)
return [(events[i].data.fd, events[i].events)
for i in range(num_events)]
|