| 1 |
import errno |
| 2 |
|
| 3 |
from rpython.rlib import _rsocket_rffi as _c, rpoll |
| 4 |
from rpython.rlib.rarithmetic import USHRT_MAX |
| 5 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 6 |
from rpython.rlib import objectmodel |
| 7 |
|
| 8 |
from pypy.interpreter.baseobjspace import W_Root |
| 9 |
from pypy.interpreter.error import OperationError, oefmt, wrap_oserror |
| 10 |
from pypy.interpreter.gateway import ( |
| 11 |
Unwrapper, WrappedDefault, interp2app, unwrap_spec) |
| 12 |
from pypy.interpreter.typedef import TypeDef |
| 13 |
from pypy.module.time import timeutils |
| 14 |
|
| 15 |
defaultevents = rpoll.POLLIN | rpoll.POLLOUT | rpoll.POLLPRI |
| 16 |
|
| 17 |
|
| 18 |
def poll(space): |
| 19 |
"""Returns a polling object, which supports registering and |
| 20 |
unregistering file descriptors, and then polling them for I/O |
| 21 |
events. |
| 22 |
""" |
| 23 |
return Poll() |
| 24 |
|
| 25 |
|
| 26 |
class Poll(W_Root): |
| 27 |
def __init__(self): |
| 28 |
self.fddict = {} |
| 29 |
self.running = False |
| 30 |
|
| 31 |
@unwrap_spec(events="c_ushort") |
| 32 |
def register(self, space, w_fd, events=defaultevents): |
| 33 |
""" |
| 34 |
Register a file descriptor with the polling object. |
| 35 |
fd -- either an integer, or an object with a fileno() method returning an |
| 36 |
int. |
| 37 |
events -- an optional bitmask describing the type of events to check for |
| 38 |
""" |
| 39 |
fd = space.c_filedescriptor_w(w_fd) |
| 40 |
self.fddict[fd] = events |
| 41 |
|
| 42 |
@unwrap_spec(events="c_ushort") |
| 43 |
def modify(self, space, w_fd, events): |
| 44 |
""" |
| 45 |
Modify an already registered file descriptor. |
| 46 |
fd -- either an integer, or an object with a fileno() method returning an |
| 47 |
int. |
| 48 |
events -- an optional bitmask describing the type of events to check for |
| 49 |
""" |
| 50 |
fd = space.c_filedescriptor_w(w_fd) |
| 51 |
if fd not in self.fddict: |
| 52 |
raise wrap_oserror(space, OSError(errno.ENOENT, "poll.modify"), |
| 53 |
w_exception_class=space.w_IOError) |
| 54 |
self.fddict[fd] = events |
| 55 |
|
| 56 |
def unregister(self, space, w_fd): |
| 57 |
""" |
| 58 |
Remove a file descriptor being tracked by the polling object. |
| 59 |
""" |
| 60 |
fd = space.c_filedescriptor_w(w_fd) |
| 61 |
try: |
| 62 |
del self.fddict[fd] |
| 63 |
except KeyError: |
| 64 |
raise OperationError(space.w_KeyError, space.newint(fd)) |
| 65 |
|
| 66 |
@unwrap_spec(w_timeout=WrappedDefault(None)) |
| 67 |
def poll(self, space, w_timeout): |
| 68 |
""" |
| 69 |
Polls the set of registered file descriptors, returning a list containing |
| 70 |
any descriptors that have events or errors to report. |
| 71 |
|
| 72 |
the timeout parameter is in milliseconds""" |
| 73 |
if space.is_w(w_timeout, space.w_None): |
| 74 |
timeout = -1 |
| 75 |
end_time = 0 |
| 76 |
elif space.isinstance_w(w_timeout, space.w_float) or space.isinstance_w(w_timeout, space.w_int): |
| 77 |
if space.is_true(space.lt(w_timeout, space.newint(0))): |
| 78 |
timeout = -1 |
| 79 |
end_time = 0 |
| 80 |
else: |
| 81 |
timeout = space.c_int_w(space.int(w_timeout)) |
| 82 |
end_time = timeutils.monotonic(space) + timeout * 0.001 |
| 83 |
else: |
| 84 |
raise oefmt(space.w_TypeError, |
| 85 |
"timeout must be an integer or None") |
| 86 |
|
| 87 |
if self.running: |
| 88 |
raise oefmt(space.w_RuntimeError, "concurrent poll() invocation") |
| 89 |
while True: |
| 90 |
self.running = True |
| 91 |
try: |
| 92 |
retval = rpoll.poll(self.fddict, timeout) |
| 93 |
except rpoll.PollError as e: |
| 94 |
if e.errno == errno.EINTR: |
| 95 |
space.getexecutioncontext().checksignals() |
| 96 |
timeout = int((end_time - timeutils.monotonic(space)) |
| 97 |
* 1000.0 + 0.999) |
| 98 |
if timeout < 0: |
| 99 |
timeout = 0 |
| 100 |
continue |
| 101 |
message, lgt = e.get_msg_utf8() |
| 102 |
raise OperationError(space.w_OSError, |
| 103 |
space.newtuple2(space.newint(e.errno), |
| 104 |
space.newtext(message, lgt))) |
| 105 |
finally: |
| 106 |
self.running = False |
| 107 |
break |
| 108 |
|
| 109 |
retval_w = [] |
| 110 |
for fd, revents in retval: |
| 111 |
retval_w.append(space.newtuple2(space.newint(fd), |
| 112 |
space.newint(revents))) |
| 113 |
return space.newlist(retval_w) |
| 114 |
|
| 115 |
def descr_new(space, w_subtype, __args__): |
| 116 |
raise oefmt(space.w_TypeError, "cannot create 'select.poll' instances") |
| 117 |
|
| 118 |
pollmethods = {} |
| 119 |
for methodname in 'register modify unregister poll'.split(): |
| 120 |
pollmethods[methodname] = interp2app(getattr(Poll, methodname)) |
| 121 |
pollmethods['__new__'] = interp2app(descr_new) |
| 122 |
Poll.typedef = TypeDef('select.poll', **pollmethods) |
| 123 |
Poll.typedef.acceptable_as_base_class = False |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@objectmodel.always_inline |
| 128 |
def _build_fd_set(space, list_w, ll_list, nfds): |
| 129 |
_c.FD_ZERO(ll_list) |
| 130 |
fdlist = [] |
| 131 |
for w_f in list_w: |
| 132 |
fd = space.c_filedescriptor_w(w_f) |
| 133 |
if fd > nfds: |
| 134 |
if _c.MAX_FD_SIZE is not None and fd >= _c.MAX_FD_SIZE: |
| 135 |
raise oefmt(space.w_ValueError, |
| 136 |
"file descriptor out of range in select()") |
| 137 |
nfds = fd |
| 138 |
_c.FD_SET(fd, ll_list) |
| 139 |
fdlist.append(fd) |
| 140 |
return fdlist, nfds |
| 141 |
|
| 142 |
|
| 143 |
def _unbuild_fd_set(space, list_w, fdlist, ll_list, reslist_w): |
| 144 |
for i in range(len(fdlist)): |
| 145 |
fd = fdlist[i] |
| 146 |
if _c.FD_ISSET(fd, ll_list): |
| 147 |
reslist_w.append(list_w[i]) |
| 148 |
|
| 149 |
|
| 150 |
def _call_select(space, iwtd_w, owtd_w, ewtd_w, |
| 151 |
ll_inl, ll_outl, ll_errl, ll_timeval, timeout): |
| 152 |
fdlistin = fdlistout = fdlisterr = None |
| 153 |
nfds = -1 |
| 154 |
if ll_inl: |
| 155 |
fdlistin, nfds = _build_fd_set(space, iwtd_w, ll_inl, nfds) |
| 156 |
if ll_outl: |
| 157 |
fdlistout, nfds = _build_fd_set(space, owtd_w, ll_outl, nfds) |
| 158 |
if ll_errl: |
| 159 |
fdlisterr, nfds = _build_fd_set(space, ewtd_w, ll_errl, nfds) |
| 160 |
|
| 161 |
if ll_timeval: |
| 162 |
end_time = timeutils.monotonic(space) + timeout |
| 163 |
else: |
| 164 |
end_time = 0.0 |
| 165 |
|
| 166 |
while True: |
| 167 |
if ll_timeval: |
| 168 |
i = int(timeout) |
| 169 |
rffi.setintfield(ll_timeval, 'c_tv_sec', i) |
| 170 |
rffi.setintfield(ll_timeval, 'c_tv_usec', int((timeout-i)*1000000)) |
| 171 |
|
| 172 |
res = _c.select(nfds + 1, ll_inl, ll_outl, ll_errl, ll_timeval) |
| 173 |
|
| 174 |
if res >= 0: |
| 175 |
break |
| 176 |
err = _c.geterrno() |
| 177 |
if err != errno.EINTR: |
| 178 |
msg, length = _c.socket_strerror_utf8(err) |
| 179 |
raise OperationError(space.w_OSError, space.newtuple2( |
| 180 |
space.newint(err), space.newtext(msg, length))) |
| 181 |
|
| 182 |
space.getexecutioncontext().checksignals() |
| 183 |
if timeout > 0.0: |
| 184 |
timeout = end_time - timeutils.monotonic(space) |
| 185 |
if timeout < 0.0: |
| 186 |
timeout = 0.0 |
| 187 |
|
| 188 |
resin_w = [] |
| 189 |
resout_w = [] |
| 190 |
reserr_w = [] |
| 191 |
if res > 0: |
| 192 |
if fdlistin is not None: |
| 193 |
_unbuild_fd_set(space, iwtd_w, fdlistin, ll_inl, resin_w) |
| 194 |
if fdlistout is not None: |
| 195 |
_unbuild_fd_set(space, owtd_w, fdlistout, ll_outl, resout_w) |
| 196 |
if fdlisterr is not None: |
| 197 |
_unbuild_fd_set(space, ewtd_w, fdlisterr, ll_errl, reserr_w) |
| 198 |
return space.newtuple([space.newlist(resin_w), |
| 199 |
space.newlist(resout_w), |
| 200 |
space.newlist(reserr_w)]) |
| 201 |
|
| 202 |
|
| 203 |
@unwrap_spec(w_timeout=WrappedDefault(None)) |
| 204 |
def select(space, w_iwtd, w_owtd, w_ewtd, w_timeout): |
| 205 |
"""Wait until one or more file descriptors are ready for some kind of I/O. |
| 206 |
The first three arguments are sequences of file descriptors to be waited for: |
| 207 |
rlist -- wait until ready for reading |
| 208 |
wlist -- wait until ready for writing |
| 209 |
xlist -- wait for an ``exceptional condition'' |
| 210 |
If only one kind of condition is required, pass [] for the other lists. |
| 211 |
A file descriptor is either a socket or file object, or a small integer |
| 212 |
gotten from a fileno() method call on one of those. |
| 213 |
|
| 214 |
The optional 4th argument specifies a timeout in seconds; it may be |
| 215 |
a floating point number to specify fractions of seconds. If it is absent |
| 216 |
or None, the call will never time out. |
| 217 |
|
| 218 |
The return value is a tuple of three lists corresponding to the first three |
| 219 |
arguments; each contains the subset of the corresponding file descriptors |
| 220 |
that are ready. |
| 221 |
|
| 222 |
*** IMPORTANT NOTICE *** |
| 223 |
On Windows, only sockets are supported; on Unix, all file descriptors. |
| 224 |
""" |
| 225 |
|
| 226 |
iwtd_w = space.unpackiterable(w_iwtd) |
| 227 |
owtd_w = space.unpackiterable(w_owtd) |
| 228 |
ewtd_w = space.unpackiterable(w_ewtd) |
| 229 |
|
| 230 |
if space.is_w(w_timeout, space.w_None): |
| 231 |
timeout = -1.0 |
| 232 |
else: |
| 233 |
timeout = space.float_w(w_timeout) |
| 234 |
if timeout < 0.0: |
| 235 |
raise oefmt(space.w_ValueError, "timeout must be non-negative") |
| 236 |
|
| 237 |
ll_inl = lltype.nullptr(_c.fd_set.TO) |
| 238 |
ll_outl = lltype.nullptr(_c.fd_set.TO) |
| 239 |
ll_errl = lltype.nullptr(_c.fd_set.TO) |
| 240 |
ll_timeval = lltype.nullptr(_c.timeval) |
| 241 |
|
| 242 |
try: |
| 243 |
if len(iwtd_w) > 0: |
| 244 |
ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw') |
| 245 |
if len(owtd_w) > 0: |
| 246 |
ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw') |
| 247 |
if len(ewtd_w) > 0: |
| 248 |
ll_errl = lltype.malloc(_c.fd_set.TO, flavor='raw') |
| 249 |
if timeout >= 0.0: |
| 250 |
ll_timeval = rffi.make(_c.timeval) |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
return _call_select(space, iwtd_w, owtd_w, ewtd_w, |
| 256 |
ll_inl, ll_outl, ll_errl, ll_timeval, timeout) |
| 257 |
finally: |
| 258 |
if ll_timeval: |
| 259 |
lltype.free(ll_timeval, flavor='raw') |
| 260 |
if ll_errl: |
| 261 |
lltype.free(ll_errl, flavor='raw') |
| 262 |
if ll_outl: |
| 263 |
lltype.free(ll_outl, flavor='raw') |
| 264 |
if ll_inl: |
| 265 |
lltype.free(ll_inl, flavor='raw') |