| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import signal as cpy_signal |
| 4 |
import sys |
| 5 |
import os |
| 6 |
import errno |
| 7 |
|
| 8 |
from pypy.interpreter.error import ( |
| 9 |
OperationError, exception_from_saved_errno, oefmt, wrap_oserror) |
| 10 |
from pypy.interpreter.executioncontext import (AsyncAction, AbstractActionFlag, |
| 11 |
PeriodicAsyncAction) |
| 12 |
from pypy.interpreter.gateway import unwrap_spec |
| 13 |
|
| 14 |
from rpython.rlib import jit, rgc, rposix, rposix_stat, rthread |
| 15 |
from rpython.rlib.objectmodel import we_are_translated |
| 16 |
from rpython.rlib.rarithmetic import intmask, widen, r_uint |
| 17 |
from rpython.rlib.rsignal import * |
| 18 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 19 |
|
| 20 |
|
| 21 |
WIN32 = sys.platform == 'win32' |
| 22 |
|
| 23 |
|
| 24 |
class SignalActionFlag(AbstractActionFlag): |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
def get_ticker(self): |
| 31 |
p = pypysig_getaddr_occurred() |
| 32 |
return p.c_value |
| 33 |
|
| 34 |
def reset_ticker(self, value): |
| 35 |
p = pypysig_getaddr_occurred() |
| 36 |
p.c_value = value |
| 37 |
|
| 38 |
def rearm_ticker(self): |
| 39 |
p = pypysig_getaddr_occurred() |
| 40 |
p.c_value = -1 |
| 41 |
|
| 42 |
def decrement_ticker(self, by): |
| 43 |
p = pypysig_getaddr_occurred() |
| 44 |
value = p.c_value |
| 45 |
if self.has_bytecode_counter: |
| 46 |
if jit.isconstant(by) and by == 0: |
| 47 |
pass |
| 48 |
else: |
| 49 |
value -= by |
| 50 |
p.c_value = value |
| 51 |
return value |
| 52 |
|
| 53 |
|
| 54 |
class CheckSignalAction(PeriodicAsyncAction): |
| 55 |
"""An action that is automatically invoked when a signal is received.""" |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
def __init__(self, space): |
| 63 |
"NOT_RPYTHON" |
| 64 |
AsyncAction.__init__(self, space) |
| 65 |
self.pending_signal = -1 |
| 66 |
self.fire_in_another_thread = False |
| 67 |
self._in_poll = False |
| 68 |
|
| 69 |
@rgc.no_collect |
| 70 |
def _after_thread_switch(): |
| 71 |
|
| 72 |
|
| 73 |
ec = self.space.threadlocals.get_ec() |
| 74 |
if ec is not None and ec.w_async_exception_type: |
| 75 |
self.space.actionflag.rearm_ticker() |
| 76 |
return |
| 77 |
if self.fire_in_another_thread: |
| 78 |
if self.space.threadlocals.signals_enabled(): |
| 79 |
self.fire_in_another_thread = False |
| 80 |
self.space.actionflag.rearm_ticker() |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
self._after_thread_switch = _after_thread_switch |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
def startup(self, space): |
| 89 |
|
| 90 |
if space.config.objspace.usemodules.thread: |
| 91 |
from rpython.rlib import rgil |
| 92 |
rgil.invoke_after_thread_switch(self._after_thread_switch) |
| 93 |
|
| 94 |
def perform(self, executioncontext, frame): |
| 95 |
w_exc = executioncontext.w_async_exception_type |
| 96 |
if w_exc is not None: |
| 97 |
executioncontext.w_async_exception_type = None |
| 98 |
raise oefmt(w_exc, "asynchronous exception triggered from another thread") |
| 99 |
self._poll_for_signals() |
| 100 |
|
| 101 |
@jit.dont_look_inside |
| 102 |
def _poll_for_signals(self): |
| 103 |
if self._in_poll: |
| 104 |
return |
| 105 |
self._in_poll = True |
| 106 |
try: |
| 107 |
self._poll_for_signals_unlocked() |
| 108 |
finally: |
| 109 |
self._in_poll = False |
| 110 |
|
| 111 |
@jit.dont_look_inside |
| 112 |
def _poll_for_signals_unlocked(self): |
| 113 |
|
| 114 |
e = pypysig_get_wakeup_fd_write_errno() |
| 115 |
if e != 0: |
| 116 |
_report_wakeup_fd_error(self.space, e) |
| 117 |
|
| 118 |
n = self.pending_signal |
| 119 |
p = pypysig_getaddr_occurred_fullstruct() |
| 120 |
if p.c_debugger_pending_call: |
| 121 |
path = rffi.charp2str(p.c_debugger_script_path) |
| 122 |
p.c_debugger_pending_call = 0 |
| 123 |
run_debugger(self.space, path) |
| 124 |
return |
| 125 |
if n < 0: |
| 126 |
n = pypysig_poll() |
| 127 |
while n >= 0: |
| 128 |
if self.space.threadlocals.signals_enabled(): |
| 129 |
|
| 130 |
|
| 131 |
self.pending_signal = -1 |
| 132 |
report_signal(self.space, n) |
| 133 |
n = self.pending_signal |
| 134 |
if n < 0: |
| 135 |
n = pypysig_poll() |
| 136 |
else: |
| 137 |
|
| 138 |
|
| 139 |
self.pending_signal = n |
| 140 |
self.fire_in_another_thread = True |
| 141 |
break |
| 142 |
|
| 143 |
def set_interrupt(self, signum=cpy_signal.SIGINT): |
| 144 |
"Simulates the effect of a signal arriving, defaults to SIGINT" |
| 145 |
if not (1 <= signum < NSIG): |
| 146 |
return |
| 147 |
if not we_are_translated(): |
| 148 |
self.pending_signal = signum |
| 149 |
|
| 150 |
self.fire_in_another_thread = True |
| 151 |
else: |
| 152 |
pypysig_pushback(signum) |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
class Handlers: |
| 158 |
def __init__(self, space): |
| 159 |
self.handlers_w = {} |
| 160 |
for signum in range(1, NSIG): |
| 161 |
if WIN32 and signum not in signal_values: |
| 162 |
self.handlers_w[signum] = space.w_None |
| 163 |
else: |
| 164 |
self.handlers_w[signum] = space.newint(SIG_DFL) |
| 165 |
|
| 166 |
def _get_handlers(space): |
| 167 |
return space.fromcache(Handlers).handlers_w |
| 168 |
|
| 169 |
def _report_wakeup_fd_error(space, errno_val): |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
from pypy.interpreter.error import OperationError, strerror |
| 175 |
from pypy.interpreter.pytraceback import PyTraceback |
| 176 |
try: |
| 177 |
try: |
| 178 |
msg, lgt = strerror(errno_val) |
| 179 |
except ValueError: |
| 180 |
msg = 'error %d' % errno_val |
| 181 |
lgt = len(msg) |
| 182 |
w_exc = space.call_function(space.w_OSError, space.newint(errno_val), |
| 183 |
space.newtext(msg, lgt)) |
| 184 |
frame = space.getexecutioncontext().gettopframe_nohidden() |
| 185 |
if frame is not None: |
| 186 |
w_tb = PyTraceback(space, frame, frame.last_instr, None) |
| 187 |
else: |
| 188 |
w_tb = space.w_None |
| 189 |
OperationError.write_unraisable_default( |
| 190 |
space, space.w_OSError, w_exc, w_tb, |
| 191 |
'Exception ignored when trying to write to the signal wakeup fd:', |
| 192 |
space.w_None, '') |
| 193 |
except Exception: |
| 194 |
pass |
| 195 |
|
| 196 |
def report_signal(space, n): |
| 197 |
handlers_w = _get_handlers(space) |
| 198 |
try: |
| 199 |
w_handler = handlers_w[n] |
| 200 |
except KeyError: |
| 201 |
return |
| 202 |
if not space.is_true(space.callable(w_handler)): |
| 203 |
return |
| 204 |
|
| 205 |
pypysig_reinstall(n) |
| 206 |
|
| 207 |
ec = space.getexecutioncontext() |
| 208 |
w_frame = ec.gettopframe_nohidden() |
| 209 |
space.call_function(w_handler, space.newint(n), w_frame) |
| 210 |
|
| 211 |
def run_debugger(space, path): |
| 212 |
from pypy.interpreter.streamutil import wrap_streamerror |
| 213 |
from pypy.interpreter.eval import Code |
| 214 |
from rpython.rlib import streamio |
| 215 |
w_pypymod = space.getbuiltinmodule('__pypy__') |
| 216 |
if space.is_true(space.getattr(w_pypymod, space.newtext('_pypy_disable_remote_debugger'))): |
| 217 |
return |
| 218 |
try: |
| 219 |
w_file = space.call_method(space.newbytes(path), 'decode', space.newtext('utf-8')) |
| 220 |
space.audit('remote_exec', [w_file]) |
| 221 |
msg = "Executing remote debugger script " |
| 222 |
w_msg = space.add(space.newtext(msg), w_file) |
| 223 |
w_msg = space.add(w_msg, space.newtext('\n')) |
| 224 |
space.call_method(space.getattr(space.sys, space.newtext('stdout')), 'write', w_msg) |
| 225 |
try: |
| 226 |
stream = streamio.open_file_as_stream(path, 'r') |
| 227 |
source = stream.readall() |
| 228 |
except streamio.StreamErrors as e: |
| 229 |
raise wrap_streamerror(space, e) |
| 230 |
ec = space.getexecutioncontext() |
| 231 |
pycode = ec.compiler.compile(source, path, 'exec', 0) |
| 232 |
w_globals = space.newdict() |
| 233 |
pycode.exec_code(space, w_globals, w_globals) |
| 234 |
except OperationError as e: |
| 235 |
e.write_unraisable(space, "in remote debugger invocation") |
| 236 |
|
| 237 |
|
| 238 |
@unwrap_spec(signum=int) |
| 239 |
def getsignal(space, signum): |
| 240 |
""" |
| 241 |
getsignal(sig) -> action |
| 242 |
|
| 243 |
Return the current action for the given signal. The return value can be: |
| 244 |
SIG_IGN -- if the signal is being ignored |
| 245 |
SIG_DFL -- if the default action for the signal is in effect |
| 246 |
None -- if an unknown handler is in effect |
| 247 |
anything else -- the callable Python object used as a handler |
| 248 |
""" |
| 249 |
check_signum_in_range(space, signum) |
| 250 |
handlers_w = _get_handlers(space) |
| 251 |
return handlers_w[signum] |
| 252 |
|
| 253 |
|
| 254 |
def default_int_handler(space, args_w): |
| 255 |
""" |
| 256 |
default_int_handler(...) |
| 257 |
|
| 258 |
The default handler for SIGINT installed by Python. |
| 259 |
It raises KeyboardInterrupt. |
| 260 |
""" |
| 261 |
|
| 262 |
raise OperationError(space.w_KeyboardInterrupt, space.w_None) |
| 263 |
|
| 264 |
|
| 265 |
@jit.dont_look_inside |
| 266 |
@unwrap_spec(timeout=int) |
| 267 |
def alarm(space, timeout): |
| 268 |
"""alarm(seconds) |
| 269 |
|
| 270 |
Arrange for SIGALRM to arrive after the given number of seconds. |
| 271 |
""" |
| 272 |
return space.newint(c_alarm(timeout)) |
| 273 |
|
| 274 |
|
| 275 |
@jit.dont_look_inside |
| 276 |
def pause(space): |
| 277 |
"""pause() |
| 278 |
|
| 279 |
Wait until a signal arrives. |
| 280 |
""" |
| 281 |
c_pause() |
| 282 |
return space.w_None |
| 283 |
|
| 284 |
|
| 285 |
def check_signum_in_range(space, signum): |
| 286 |
if 1 <= signum < NSIG: |
| 287 |
return |
| 288 |
raise oefmt(space.w_ValueError, "signal number out of range") |
| 289 |
|
| 290 |
|
| 291 |
@jit.dont_look_inside |
| 292 |
@unwrap_spec(signum=int) |
| 293 |
def signal(space, signum, w_handler): |
| 294 |
""" |
| 295 |
signal(sig, action) -> action |
| 296 |
|
| 297 |
Set the action for the given signal. The action can be SIG_DFL, |
| 298 |
SIG_IGN, or a callable Python object. The previous action is |
| 299 |
returned. See getsignal() for possible return values. |
| 300 |
|
| 301 |
*** IMPORTANT NOTICE *** |
| 302 |
A signal handler function is called with two arguments: |
| 303 |
the first is the signal number, the second is the interrupted stack frame. |
| 304 |
""" |
| 305 |
if WIN32 and signum not in signal_values: |
| 306 |
raise oefmt(space.w_ValueError, "invalid signal value") |
| 307 |
if not space.threadlocals.signals_enabled(): |
| 308 |
raise oefmt(space.w_ValueError, |
| 309 |
"signal only works in main thread or with " |
| 310 |
"__pypy__.thread.enable_signals()") |
| 311 |
check_signum_in_range(space, signum) |
| 312 |
|
| 313 |
if space.eq_w(w_handler, space.newint(SIG_DFL)): |
| 314 |
pypysig_default(signum) |
| 315 |
elif space.eq_w(w_handler, space.newint(SIG_IGN)): |
| 316 |
pypysig_ignore(signum) |
| 317 |
else: |
| 318 |
if not space.is_true(space.callable(w_handler)): |
| 319 |
raise oefmt(space.w_TypeError, |
| 320 |
"'handler' must be a callable or SIG_DFL or SIG_IGN") |
| 321 |
pypysig_setflag(signum) |
| 322 |
|
| 323 |
handlers_w = _get_handlers(space) |
| 324 |
old_handler = handlers_w[signum] |
| 325 |
handlers_w[signum] = w_handler |
| 326 |
return old_handler |
| 327 |
|
| 328 |
|
| 329 |
@jit.dont_look_inside |
| 330 |
@unwrap_spec(fd="c_int", warn_on_full_buffer=bool) |
| 331 |
def set_wakeup_fd(space, fd, __kwonly__, warn_on_full_buffer=True): |
| 332 |
"""Sets the fd to be written to (with the signal number) when a signal |
| 333 |
comes in. Returns the old fd. A library can use this to |
| 334 |
wakeup select or poll. The previous fd is returned. |
| 335 |
|
| 336 |
The fd must be non-blocking. |
| 337 |
""" |
| 338 |
if not space.threadlocals.signals_enabled(): |
| 339 |
raise oefmt(space.w_ValueError, |
| 340 |
"set_wakeup_fd only works in main thread or with " |
| 341 |
"__pypy__.thread.enable_signals()") |
| 342 |
|
| 343 |
send_flags = 0 |
| 344 |
if fd != -1: |
| 345 |
if WIN32: |
| 346 |
from rpython.rlib._rsocket_rffi import SOL_SOCKET, SO_TYPE |
| 347 |
from rpython.rlib.rsocket import getsockopt_int, SocketError |
| 348 |
|
| 349 |
try: |
| 350 |
type = getsockopt_int(fd, SOL_SOCKET, SO_TYPE) |
| 351 |
is_socket = True |
| 352 |
except SocketError as e: |
| 353 |
is_socket = False |
| 354 |
if is_socket: |
| 355 |
send_flags |= PYPYSIG_USE_SEND |
| 356 |
else: |
| 357 |
try: |
| 358 |
os.fstat(fd) |
| 359 |
except OSError as e: |
| 360 |
if e.errno == errno.EBADF: |
| 361 |
raise oefmt(space.w_ValueError, "invalid fd") |
| 362 |
else: |
| 363 |
try: |
| 364 |
os.fstat(fd) |
| 365 |
flags = rposix.get_status_flags(fd) |
| 366 |
except OSError as e: |
| 367 |
if e.errno == errno.EBADF: |
| 368 |
raise oefmt(space.w_ValueError, "invalid fd") |
| 369 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 370 |
if flags & rposix.O_NONBLOCK == 0: |
| 371 |
raise oefmt(space.w_ValueError, |
| 372 |
"the fd %d must be in non-blocking mode", fd) |
| 373 |
|
| 374 |
if not warn_on_full_buffer: |
| 375 |
send_flags |= PYPYSIG_NO_WARN_FULL |
| 376 |
old_fd = pypysig_set_wakeup_fd(fd, send_flags) |
| 377 |
return space.newint(intmask(old_fd)) |
| 378 |
|
| 379 |
@jit.dont_look_inside |
| 380 |
@unwrap_spec(signum=int, flag=int) |
| 381 |
def siginterrupt(space, signum, flag): |
| 382 |
"""siginterrupt(sig, flag) -> None |
| 383 |
|
| 384 |
change system call restart behaviour: if flag is False, system calls |
| 385 |
will be restarted when interrupted by signal sig, else system calls |
| 386 |
will be interrupted. |
| 387 |
""" |
| 388 |
check_signum_in_range(space, signum) |
| 389 |
if rffi.cast(lltype.Signed, c_siginterrupt(signum, flag)) < 0: |
| 390 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
def timeval_from_double(d, timeval): |
| 396 |
c_tv_sec = int(d) |
| 397 |
c_tv_usec = int((d - int(d)) * 1000000) |
| 398 |
|
| 399 |
if d > 0.0 and c_tv_sec == 0 and c_tv_usec == 0: |
| 400 |
c_tv_usec = 1 |
| 401 |
rffi.setintfield(timeval, 'c_tv_sec', c_tv_sec) |
| 402 |
rffi.setintfield(timeval, 'c_tv_usec', c_tv_usec) |
| 403 |
|
| 404 |
|
| 405 |
def double_from_timeval(tv): |
| 406 |
return rffi.getintfield(tv, 'c_tv_sec') + ( |
| 407 |
rffi.getintfield(tv, 'c_tv_usec') / 1000000.0) |
| 408 |
|
| 409 |
|
| 410 |
def itimer_retval(space, val): |
| 411 |
w_value = space.newfloat(double_from_timeval(val.c_it_value)) |
| 412 |
w_interval = space.newfloat(double_from_timeval(val.c_it_interval)) |
| 413 |
return space.newtuple2(w_value, w_interval) |
| 414 |
|
| 415 |
|
| 416 |
class Cache: |
| 417 |
def __init__(self, space): |
| 418 |
self.w_itimererror = space.new_exception_class("signal.ItimerError", |
| 419 |
space.w_IOError) |
| 420 |
|
| 421 |
|
| 422 |
def get_itimer_error(space): |
| 423 |
return space.fromcache(Cache).w_itimererror |
| 424 |
|
| 425 |
|
| 426 |
@jit.dont_look_inside |
| 427 |
@unwrap_spec(which=int, first=float, interval=float) |
| 428 |
def setitimer(space, which, first, interval=0): |
| 429 |
"""setitimer(which, seconds[, interval]) |
| 430 |
Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL |
| 431 |
|
| 432 |
or ITIMER_PROF) to fire after value seconds and after |
| 433 |
that every interval seconds. |
| 434 |
The itimer can be cleared by setting seconds to zero. |
| 435 |
|
| 436 |
Returns old values as a tuple: (delay, interval). |
| 437 |
""" |
| 438 |
with lltype.scoped_alloc(itimervalP.TO, 1) as new: |
| 439 |
|
| 440 |
timeval_from_double(first, new[0].c_it_value) |
| 441 |
timeval_from_double(interval, new[0].c_it_interval) |
| 442 |
|
| 443 |
with lltype.scoped_alloc(itimervalP.TO, 1) as old: |
| 444 |
|
| 445 |
ret = c_setitimer(which, new, old) |
| 446 |
if ret != 0: |
| 447 |
raise exception_from_saved_errno(space, get_itimer_error(space)) |
| 448 |
|
| 449 |
return itimer_retval(space, old[0]) |
| 450 |
|
| 451 |
|
| 452 |
@jit.dont_look_inside |
| 453 |
@unwrap_spec(which=int) |
| 454 |
def getitimer(space, which): |
| 455 |
"""getitimer(which) |
| 456 |
|
| 457 |
Returns current value of given itimer. |
| 458 |
""" |
| 459 |
with lltype.scoped_alloc(itimervalP.TO, 1) as old: |
| 460 |
|
| 461 |
c_getitimer(which, old) |
| 462 |
|
| 463 |
return itimer_retval(space, old[0]) |
| 464 |
|
| 465 |
|
| 466 |
@unwrap_spec(tid=r_uint, signum=int) |
| 467 |
def pthread_kill(space, tid, signum): |
| 468 |
"Send a signal to a thread." |
| 469 |
ret = rthread.c_pthread_kill(intmask(tid), signum) |
| 470 |
if widen(ret) < 0: |
| 471 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 472 |
|
| 473 |
space.getexecutioncontext().checksignals() |
| 474 |
|
| 475 |
|
| 476 |
class SignalMask(object): |
| 477 |
def __init__(self, space, w_signals): |
| 478 |
self.space = space |
| 479 |
self.w_signals = w_signals |
| 480 |
|
| 481 |
def __enter__(self): |
| 482 |
space = self.space |
| 483 |
self.mask = lltype.malloc(c_sigset_t.TO, flavor='raw') |
| 484 |
c_sigemptyset(self.mask) |
| 485 |
for w_signum in space.unpackiterable(self.w_signals): |
| 486 |
try: |
| 487 |
signum = space.int_w(w_signum) |
| 488 |
except OperationError as e: |
| 489 |
if e.match(space, space.w_OverflowError): |
| 490 |
raise oefmt(space.w_ValueError, "signal number out of range") |
| 491 |
raise |
| 492 |
check_signum_in_range(space, signum) |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
c_sigaddset(self.mask, signum) |
| 497 |
return self.mask |
| 498 |
|
| 499 |
def __exit__(self, *args): |
| 500 |
lltype.free(self.mask, flavor='raw') |
| 501 |
|
| 502 |
def _sigset_to_signals(space, mask): |
| 503 |
signals_w = [] |
| 504 |
for sig in range(1, NSIG): |
| 505 |
if c_sigismember(mask, sig) != 1: |
| 506 |
continue |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
signals_w.append(space.newint(sig)) |
| 513 |
return space.call_function(space.w_set, space.newtuple(signals_w)) |
| 514 |
|
| 515 |
def sigwait(space, w_signals): |
| 516 |
"""Suspend execution of the calling thread until the delivery of one of the |
| 517 |
signals specified in the signal set signals. """ |
| 518 |
with SignalMask(space, w_signals) as sigset: |
| 519 |
with lltype.scoped_alloc(rffi.INT_realP.TO, 1) as signum_ptr: |
| 520 |
ret = widen(c_sigwait(sigset, signum_ptr)) |
| 521 |
if ret != 0: |
| 522 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 523 |
signum = signum_ptr[0] |
| 524 |
return space.newint(signum) |
| 525 |
|
| 526 |
def sigpending(space): |
| 527 |
"""Examine pending signals. |
| 528 |
|
| 529 |
Returns a set of signal numbers that are pending for delivery to |
| 530 |
the calling thread. |
| 531 |
""" |
| 532 |
with lltype.scoped_alloc(c_sigset_t.TO) as mask: |
| 533 |
ret = c_sigpending(mask) |
| 534 |
if ret != 0: |
| 535 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 536 |
return _sigset_to_signals(space, mask) |
| 537 |
|
| 538 |
@unwrap_spec(how=int) |
| 539 |
def pthread_sigmask(space, how, w_signals): |
| 540 |
'Fetch and/or change the signal mask of the calling thread.' |
| 541 |
with SignalMask(space, w_signals) as sigset: |
| 542 |
with lltype.scoped_alloc(c_sigset_t.TO) as previous: |
| 543 |
ret = c_pthread_sigmask(how, sigset, previous) |
| 544 |
if ret != 0: |
| 545 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 546 |
|
| 547 |
space.getexecutioncontext().checksignals() |
| 548 |
return _sigset_to_signals(space, previous) |
| 549 |
|
| 550 |
def valid_signals(space): |
| 551 |
'''Return a set of valid signal numbers on this platform. |
| 552 |
|
| 553 |
The signal numbers returned by this function can be safely passed to |
| 554 |
functions like `pthread_sigmask`.''' |
| 555 |
if WIN32: |
| 556 |
|
| 557 |
signals_w = [space.newint(SIGABRT), space.newint(SIGBREAK), |
| 558 |
space.newint(SIGFPE), space.newint(SIGILL), |
| 559 |
space.newint(SIGINT), space.newint(SIGSEGV), |
| 560 |
space.newint(SIGTERM), |
| 561 |
] |
| 562 |
return space.call_function(space.w_set, space.newtuple(signals_w)) |
| 563 |
else: |
| 564 |
mask = lltype.malloc(c_sigset_t.TO, flavor='raw') |
| 565 |
try: |
| 566 |
ret = c_sigemptyset(mask) |
| 567 |
if ret != 0: |
| 568 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 569 |
ret = c_sigfillset(mask) |
| 570 |
if ret != 0: |
| 571 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 572 |
return _sigset_to_signals(space, mask) |
| 573 |
finally: |
| 574 |
lltype.free(mask, flavor='raw') |
| 575 |
|
| 576 |
@unwrap_spec(signalnum=int) |
| 577 |
def raise_signal(space, signalnum): |
| 578 |
'Send a signal to the executing process.' |
| 579 |
with rposix.SuppressIPH(): |
| 580 |
err = c_raise(signalnum) |
| 581 |
if err != 0: |
| 582 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 583 |
|
| 584 |
space.getexecutioncontext().checksignals() |
| 585 |
|
| 586 |
@unwrap_spec(signalnum=int) |
| 587 |
def strsignal(space, signalnum): |
| 588 |
'''Return the system description of the given signal. |
| 589 |
The return values can be such as "Interrupt", "Segmentation fault", etc. |
| 590 |
Returns None if the signal is not recognized.''' |
| 591 |
from rpython.rlib import rsignal |
| 592 |
if signalnum < 1 or signalnum > NSIG: |
| 593 |
raise oefmt(space.w_ValueError, 'signal number out of range') |
| 594 |
res = rsignal.strsignal(signalnum) |
| 595 |
if res is None: |
| 596 |
return space.w_None |
| 597 |
return space.newtext(res) |
| 598 |
|
| 599 |
|