| 1 |
import os, pytest, sys |
| 2 |
import signal as cpy_signal |
| 3 |
from pypy.tool.pytest.objspace import gettestobjspace |
| 4 |
|
| 5 |
GET_POSIX = "(): import %s as m ; return m" % os.name |
| 6 |
USEMODULES = ['posix', 'signal'] |
| 7 |
|
| 8 |
def setup_module(mod): |
| 9 |
mod.space = gettestobjspace(usemodules=USEMODULES) |
| 10 |
|
| 11 |
class TestCheckSignals: |
| 12 |
spaceconfig = {'usemodules': USEMODULES} |
| 13 |
|
| 14 |
def setup_class(cls): |
| 15 |
if not hasattr(os, 'kill') or not hasattr(os, 'getpid'): |
| 16 |
pytest.skip("requires os.kill() and os.getpid()") |
| 17 |
if not hasattr(cpy_signal, 'SIGUSR1'): |
| 18 |
pytest.skip("requires SIGUSR1 in signal") |
| 19 |
|
| 20 |
def test_checksignals(self): |
| 21 |
import os |
| 22 |
space = self.space |
| 23 |
w_received = space.appexec([], """(): |
| 24 |
import _signal as signal |
| 25 |
received = [] |
| 26 |
def myhandler(signum, frame): |
| 27 |
received.append(signum) |
| 28 |
signal.signal(signal.SIGUSR1, myhandler) |
| 29 |
return received""") |
| 30 |
|
| 31 |
assert not space.is_true(w_received) |
| 32 |
|
| 33 |
|
| 34 |
os.kill(os.getpid(), cpy_signal.SIGUSR1) |
| 35 |
|
| 36 |
|
| 37 |
assert not space.is_true(w_received) |
| 38 |
|
| 39 |
|
| 40 |
print(space.getexecutioncontext().checksignals) |
| 41 |
space.getexecutioncontext().checksignals() |
| 42 |
assert space.is_true(w_received) |
| 43 |
|
| 44 |
|
| 45 |
class AppTestSignal: |
| 46 |
spaceconfig = { |
| 47 |
"usemodules": ['signal', 'time', '_socket'] + (['fcntl'] if os.name != 'nt' else []), |
| 48 |
} |
| 49 |
|
| 50 |
def setup_class(cls): |
| 51 |
cls.w_temppath = cls.space.wrap( |
| 52 |
str(pytest.ensuretemp("signal").join("foo.txt"))) |
| 53 |
cls.w_appdirect = cls.space.wrap(cls.runappdirect) |
| 54 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 55 |
|
| 56 |
def test_exported_names(self): |
| 57 |
import sys, _signal |
| 58 |
_signal.__dict__ |
| 59 |
if sys.platform == 'win32': |
| 60 |
assert _signal.CTRL_BREAK_EVENT == 1 |
| 61 |
assert _signal.CTRL_C_EVENT == 0 |
| 62 |
|
| 63 |
def test_basics(self): |
| 64 |
import types, _signal |
| 65 |
os = self.posix |
| 66 |
if not hasattr(os, 'kill') or not hasattr(os, 'getpid'): |
| 67 |
skip("requires os.kill() and os.getpid()") |
| 68 |
signal = _signal |
| 69 |
if not hasattr(signal, 'SIGUSR1'): |
| 70 |
skip("requires SIGUSR1 in signal") |
| 71 |
signum = signal.SIGUSR1 |
| 72 |
|
| 73 |
received = [] |
| 74 |
def myhandler(signum, frame): |
| 75 |
assert isinstance(frame, types.FrameType) |
| 76 |
received.append(signum) |
| 77 |
signal.signal(signum, myhandler) |
| 78 |
|
| 79 |
os.kill(os.getpid(), signum) |
| 80 |
|
| 81 |
assert received == [signum] |
| 82 |
del received[:] |
| 83 |
|
| 84 |
os.kill(os.getpid(), signum) |
| 85 |
|
| 86 |
assert received == [signum] |
| 87 |
del received[:] |
| 88 |
|
| 89 |
signal.signal(signum, signal.SIG_IGN) |
| 90 |
|
| 91 |
os.kill(os.getpid(), signum) |
| 92 |
for i in range(10000): |
| 93 |
|
| 94 |
if received: |
| 95 |
break |
| 96 |
assert received == [] |
| 97 |
|
| 98 |
signal.signal(signum, signal.SIG_DFL) |
| 99 |
|
| 100 |
def test_default_return(self): |
| 101 |
""" |
| 102 |
Test that signal.signal returns SIG_DFL if that is the current handler. |
| 103 |
""" |
| 104 |
from _signal import signal, SIGINT, SIG_DFL, SIG_IGN |
| 105 |
|
| 106 |
try: |
| 107 |
for handler in SIG_DFL, SIG_IGN, lambda *a: None: |
| 108 |
signal(SIGINT, SIG_DFL) |
| 109 |
assert signal(SIGINT, handler) == SIG_DFL |
| 110 |
finally: |
| 111 |
signal(SIGINT, SIG_DFL) |
| 112 |
|
| 113 |
def test_ignore_return(self): |
| 114 |
""" |
| 115 |
Test that signal.signal returns SIG_IGN if that is the current handler. |
| 116 |
""" |
| 117 |
from _signal import signal, SIGINT, SIG_DFL, SIG_IGN |
| 118 |
|
| 119 |
try: |
| 120 |
for handler in SIG_DFL, SIG_IGN, lambda *a: None: |
| 121 |
signal(SIGINT, SIG_IGN) |
| 122 |
assert signal(SIGINT, handler) == SIG_IGN |
| 123 |
finally: |
| 124 |
signal(SIGINT, SIG_DFL) |
| 125 |
|
| 126 |
def test_obj_return(self): |
| 127 |
""" |
| 128 |
Test that signal.signal returns a Python object if one is the current |
| 129 |
handler. |
| 130 |
""" |
| 131 |
from _signal import signal, SIGINT, SIG_DFL, SIG_IGN |
| 132 |
def installed(*a): |
| 133 |
pass |
| 134 |
|
| 135 |
try: |
| 136 |
for handler in SIG_DFL, SIG_IGN, lambda *a: None: |
| 137 |
signal(SIGINT, installed) |
| 138 |
assert signal(SIGINT, handler) is installed |
| 139 |
finally: |
| 140 |
signal(SIGINT, SIG_DFL) |
| 141 |
|
| 142 |
def test_getsignal(self): |
| 143 |
""" |
| 144 |
Test that signal.getsignal returns the currently installed handler. |
| 145 |
""" |
| 146 |
from _signal import getsignal, signal, SIGINT, SIG_DFL, SIG_IGN |
| 147 |
|
| 148 |
def handler(*a): |
| 149 |
pass |
| 150 |
|
| 151 |
try: |
| 152 |
if not self.appdirect: |
| 153 |
assert getsignal(SIGINT) == SIG_DFL |
| 154 |
signal(SIGINT, SIG_DFL) |
| 155 |
assert getsignal(SIGINT) == SIG_DFL |
| 156 |
signal(SIGINT, SIG_IGN) |
| 157 |
assert getsignal(SIGINT) == SIG_IGN |
| 158 |
signal(SIGINT, handler) |
| 159 |
assert getsignal(SIGINT) is handler |
| 160 |
finally: |
| 161 |
signal(SIGINT, SIG_DFL) |
| 162 |
|
| 163 |
def test_check_signum(self): |
| 164 |
import sys |
| 165 |
from _signal import getsignal, signal, NSIG |
| 166 |
|
| 167 |
|
| 168 |
raises(ValueError, getsignal, NSIG) |
| 169 |
raises(ValueError, signal, NSIG, lambda *args: None) |
| 170 |
|
| 171 |
|
| 172 |
if sys.platform == 'win32': |
| 173 |
assert getsignal(7) == None |
| 174 |
raises(ValueError, signal, 7, lambda *args: None) |
| 175 |
|
| 176 |
def test_alarm(self): |
| 177 |
try: |
| 178 |
from _signal import alarm, signal, SIG_DFL, SIGALRM |
| 179 |
except: |
| 180 |
skip('no alarm on this platform') |
| 181 |
import time |
| 182 |
l = [] |
| 183 |
def handler(*a): |
| 184 |
l.append(42) |
| 185 |
|
| 186 |
try: |
| 187 |
signal(SIGALRM, handler) |
| 188 |
alarm(1) |
| 189 |
time.sleep(2) |
| 190 |
assert l == [42] |
| 191 |
alarm(0) |
| 192 |
assert l == [42] |
| 193 |
finally: |
| 194 |
signal(SIGALRM, SIG_DFL) |
| 195 |
|
| 196 |
def test_set_wakeup_fd(self): |
| 197 |
try: |
| 198 |
import _signal as signal, posix, fcntl |
| 199 |
except ImportError: |
| 200 |
skip('cannot import posix or fcntl') |
| 201 |
def myhandler(signum, frame): |
| 202 |
pass |
| 203 |
signal.signal(signal.SIGINT, myhandler) |
| 204 |
|
| 205 |
def cannot_read(): |
| 206 |
try: |
| 207 |
posix.read(fd_read, 1) |
| 208 |
except OSError: |
| 209 |
pass |
| 210 |
else: |
| 211 |
raise AssertionError("posix.read(fd_read, 1) succeeded?") |
| 212 |
|
| 213 |
fd_read, fd_write = posix.pipe() |
| 214 |
flags = fcntl.fcntl(fd_write, fcntl.F_GETFL, 0) |
| 215 |
flags = flags | posix.O_NONBLOCK |
| 216 |
fcntl.fcntl(fd_write, fcntl.F_SETFL, flags) |
| 217 |
flags = fcntl.fcntl(fd_read, fcntl.F_GETFL, 0) |
| 218 |
flags = flags | posix.O_NONBLOCK |
| 219 |
fcntl.fcntl(fd_read, fcntl.F_SETFL, flags) |
| 220 |
|
| 221 |
old_wakeup = signal.set_wakeup_fd(fd_write, warn_on_full_buffer=False) |
| 222 |
try: |
| 223 |
cannot_read() |
| 224 |
posix.kill(posix.getpid(), signal.SIGINT) |
| 225 |
res = posix.read(fd_read, 1) |
| 226 |
assert res == bytes([signal.SIGINT]) |
| 227 |
cannot_read() |
| 228 |
finally: |
| 229 |
old_wakeup = signal.set_wakeup_fd(old_wakeup) |
| 230 |
|
| 231 |
signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 232 |
|
| 233 |
def test_set_wakeup_fd_socket_result(self): |
| 234 |
import _socket as socket |
| 235 |
import _signal as signal |
| 236 |
sock1 = socket.socket() |
| 237 |
sock2 = socket.socket() |
| 238 |
try: |
| 239 |
sock1.setblocking(False) |
| 240 |
fd1 = sock1.fileno() |
| 241 |
sock2.setblocking(False) |
| 242 |
fd2 = sock2.fileno() |
| 243 |
|
| 244 |
signal.set_wakeup_fd(fd1) |
| 245 |
signal.set_wakeup_fd(fd2) == fd1 |
| 246 |
assert signal.set_wakeup_fd(-1) == fd2 |
| 247 |
finally: |
| 248 |
sock1.close() |
| 249 |
sock2.close() |
| 250 |
|
| 251 |
def test_set_wakeup_fd_invalid(self): |
| 252 |
import _signal as signal |
| 253 |
with open(self.temppath, 'wb') as f: |
| 254 |
fd = f.fileno() |
| 255 |
raises((ValueError, OSError), signal.set_wakeup_fd, fd) |
| 256 |
|
| 257 |
def test_siginterrupt(self): |
| 258 |
import _signal as signal, time |
| 259 |
os = self.posix |
| 260 |
if not hasattr(signal, 'siginterrupt'): |
| 261 |
skip('non siginterrupt in signal') |
| 262 |
signum = signal.SIGUSR1 |
| 263 |
def readpipe_is_not_interrupted(): |
| 264 |
|
| 265 |
r, w = os.pipe() |
| 266 |
ppid = os.getpid() |
| 267 |
pid = os.fork() |
| 268 |
if pid == 0: |
| 269 |
try: |
| 270 |
time.sleep(1) |
| 271 |
os.kill(ppid, signum) |
| 272 |
time.sleep(1) |
| 273 |
finally: |
| 274 |
os._exit(0) |
| 275 |
else: |
| 276 |
try: |
| 277 |
os.close(w) |
| 278 |
|
| 279 |
|
| 280 |
os.read(r, 1) |
| 281 |
finally: |
| 282 |
os.waitpid(pid, 0) |
| 283 |
os.close(r) |
| 284 |
|
| 285 |
oldhandler = signal.signal(signum, lambda x,y: None) |
| 286 |
try: |
| 287 |
signal.siginterrupt(signum, 0) |
| 288 |
readpipe_is_not_interrupted() |
| 289 |
readpipe_is_not_interrupted() |
| 290 |
finally: |
| 291 |
signal.signal(signum, oldhandler) |
| 292 |
|
| 293 |
def test_default_int_handler(self): |
| 294 |
import signal |
| 295 |
for args in [(), (1, 2)]: |
| 296 |
try: |
| 297 |
signal.default_int_handler(*args) |
| 298 |
except KeyboardInterrupt: |
| 299 |
pass |
| 300 |
else: |
| 301 |
raise AssertionError("did not raise!") |
| 302 |
|
| 303 |
def test_valid_signals(self): |
| 304 |
import signal, sys |
| 305 |
s = signal.valid_signals() |
| 306 |
assert isinstance(s, set) |
| 307 |
assert signal.Signals.SIGINT in s |
| 308 |
if sys.platform != "win32": |
| 309 |
assert signal.Signals.SIGALRM in s |
| 310 |
assert 0 not in s |
| 311 |
assert signal.NSIG not in s |
| 312 |
assert len(s) < signal.NSIG |
| 313 |
|
| 314 |
def test_strsignal(self): |
| 315 |
import signal |
| 316 |
assert signal.strsignal(signal.Signals.SIGSEGV).startswith("Segmentation fault") |
| 317 |
raises(ValueError, signal.strsignal, 4242) |
| 318 |
|
| 319 |
|
| 320 |
class AppTestSignalSocket: |
| 321 |
spaceconfig = dict(usemodules=['signal', '_socket']) |
| 322 |
|
| 323 |
def test_alarm_raise(self): |
| 324 |
try: |
| 325 |
from _signal import alarm, signal, SIG_DFL, SIGALRM |
| 326 |
except ImportError: |
| 327 |
skip("no SIGALRM on this platform") |
| 328 |
import _socket |
| 329 |
class Alarm(Exception): |
| 330 |
pass |
| 331 |
def handler(*a): |
| 332 |
raise Alarm() |
| 333 |
|
| 334 |
s = _socket.socket() |
| 335 |
s.listen(1) |
| 336 |
try: |
| 337 |
signal(SIGALRM, handler) |
| 338 |
alarm(1) |
| 339 |
try: |
| 340 |
s._accept() |
| 341 |
except Alarm: |
| 342 |
pass |
| 343 |
else: |
| 344 |
raise Exception("should have raised Alarm") |
| 345 |
alarm(0) |
| 346 |
finally: |
| 347 |
signal(SIGALRM, SIG_DFL) |
| 348 |
|
| 349 |
|
| 350 |
@pytest.mark.skipif(sys.platform == 'win32', reason='posix-only') |
| 351 |
class AppTestItimer: |
| 352 |
spaceconfig = dict(usemodules=['signal']) |
| 353 |
|
| 354 |
def test_itimer_real(self): |
| 355 |
import _signal as signal |
| 356 |
|
| 357 |
def sig_alrm(*args): |
| 358 |
self.called = True |
| 359 |
|
| 360 |
signal.signal(signal.SIGALRM, sig_alrm) |
| 361 |
old = signal.setitimer(signal.ITIMER_REAL, 1.0) |
| 362 |
assert old == (0, 0) |
| 363 |
|
| 364 |
val, interval = signal.getitimer(signal.ITIMER_REAL) |
| 365 |
assert val <= 1.0 |
| 366 |
assert interval == 0.0 |
| 367 |
|
| 368 |
signal.pause() |
| 369 |
assert self.called |
| 370 |
|
| 371 |
def test_itimer_exc(self): |
| 372 |
import _signal as signal |
| 373 |
|
| 374 |
raises(signal.ItimerError, signal.setitimer, -1, 0) |
| 375 |
|
| 376 |
@pytest.mark.skipif(sys.platform == 'win32', reason='posix-only') |
| 377 |
class AppTestPThread: |
| 378 |
spaceconfig = dict(usemodules=['signal', 'thread', 'time']) |
| 379 |
|
| 380 |
def test_pthread_kill(self): |
| 381 |
import _signal as signal |
| 382 |
import _thread |
| 383 |
signum = signal.SIGUSR1 |
| 384 |
def handler(signum, frame): |
| 385 |
1/0 |
| 386 |
signal.signal(signum, handler) |
| 387 |
tid = _thread.get_ident() |
| 388 |
raises(ZeroDivisionError, signal.pthread_kill, tid, signum) |
| 389 |
|
| 390 |
def test_sigwait(self): |
| 391 |
import _signal as signal |
| 392 |
def handler(signum, frame): |
| 393 |
1/0 |
| 394 |
signal.signal(signal.SIGALRM, handler) |
| 395 |
signal.alarm(1) |
| 396 |
received = signal.sigwait([signal.SIGALRM]) |
| 397 |
assert received == signal.SIGALRM |
| 398 |
|
| 399 |
def test_sigmask(self): |
| 400 |
import _signal as signal, posix |
| 401 |
signum1 = signal.SIGUSR1 |
| 402 |
signum2 = signal.SIGUSR2 |
| 403 |
|
| 404 |
def handler(signum, frame): |
| 405 |
pass |
| 406 |
signal.signal(signum1, handler) |
| 407 |
signal.signal(signum2, handler) |
| 408 |
|
| 409 |
signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2)) |
| 410 |
posix.kill(posix.getpid(), signum1) |
| 411 |
posix.kill(posix.getpid(), signum2) |
| 412 |
assert signal.sigpending() == set((signum1, signum2)) |
| 413 |
|
| 414 |
signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2)) |
| 415 |
assert signal.sigpending() == set() |
| 416 |
|
| 417 |
def test_raise_signal(self): |
| 418 |
import types, _signal, sys |
| 419 |
signal = _signal |
| 420 |
if sys.platform == 'win32': |
| 421 |
raises(OSError, signal.raise_signal, 1) |
| 422 |
if not hasattr(signal, 'SIGUSR1'): |
| 423 |
skip("requires SIGUSR1 in signal") |
| 424 |
signum = signal.SIGUSR1 |
| 425 |
|
| 426 |
received = [] |
| 427 |
def myhandler(signum, frame): |
| 428 |
assert isinstance(frame, types.FrameType) |
| 429 |
received.append(signum) |
| 430 |
signal.signal(signum, myhandler) |
| 431 |
|
| 432 |
signal.raise_signal(signum) |
| 433 |
|
| 434 |
assert received == [signum] |
| 435 |
del received[:] |
| 436 |
|
| 437 |
signal.raise_signal(signum) |
| 438 |
|
| 439 |
assert received == [signum] |
| 440 |
del received[:] |
| 441 |
|
| 442 |
signal.signal(signum, signal.SIG_IGN) |
| 443 |
|
| 444 |
signal.raise_signal(signum) |
| 445 |
for i in range(10000): |
| 446 |
|
| 447 |
if received: |
| 448 |
break |
| 449 |
assert received == [] |
| 450 |
|
| 451 |
signal.signal(signum, signal.SIG_DFL) |
| 452 |
|
| 453 |
|
| 454 |
class AppTestRemotelyTriggeredDebugger: |
| 455 |
spaceconfig = { |
| 456 |
"usemodules": USEMODULES, |
| 457 |
} |
| 458 |
|
| 459 |
def setup_class(cls): |
| 460 |
from pypy.interpreter.gateway import interp2app |
| 461 |
from rpython.rlib.rsignal import pypysig_getaddr_occurred_fullstruct |
| 462 |
if cls.runappdirect: |
| 463 |
pytest.skip("can only be run untranslated") |
| 464 |
tmpdir = pytest.ensuretemp("signal") |
| 465 |
outfile = tmpdir.join('out.txt') |
| 466 |
tmpfile = tmpdir.join("debugger.py") |
| 467 |
tmpfile.write(''' |
| 468 |
with open(%r, 'w') as f: |
| 469 |
f.write('done') |
| 470 |
print('done') |
| 471 |
''' % str(outfile)) |
| 472 |
cls.w_tmpfile = cls.space.wrap(str(tmpfile)) |
| 473 |
cls.w_outfile = cls.space.wrap( |
| 474 |
str(outfile)) |
| 475 |
def trigger_debugger(space): |
| 476 |
addr = pypysig_getaddr_occurred_fullstruct() |
| 477 |
for index, c in enumerate(str(tmpfile)): |
| 478 |
addr.c_debugger_script_path[index] = c |
| 479 |
addr.c_debugger_script_path[index + 1] = '\x00' |
| 480 |
addr.c_debugger_pending_call = 1 |
| 481 |
addr.c_value = -1 |
| 482 |
cls.w_trigger_debugger = cls.space.wrap(interp2app(trigger_debugger)) |
| 483 |
|
| 484 |
def test_run_debugger(self): |
| 485 |
self.trigger_debugger() |
| 486 |
with open(self.outfile) as f: |
| 487 |
content = f.read() |
| 488 |
assert content == 'done' |
| 489 |
|
| 490 |
def test_audit_hook(self): |
| 491 |
import __pypy__ |
| 492 |
import sys |
| 493 |
l = [] |
| 494 |
def f(*args): |
| 495 |
l.append(args) |
| 496 |
sys.addaudithook(f) |
| 497 |
try: |
| 498 |
self.trigger_debugger() |
| 499 |
assert l[0] == ('remote_exec', (self.tmpfile, )) |
| 500 |
finally: |
| 501 |
__pypy__._testing_clear_audithooks() |
| 502 |
|
| 503 |
def test_disable_debugger(self): |
| 504 |
import __pypy__ |
| 505 |
with open(self.outfile, 'w') as f: |
| 506 |
f.write('nothing') |
| 507 |
assert __pypy__._pypy_disable_remote_debugger == False |
| 508 |
__pypy__._pypy_disable_remote_debugger = True |
| 509 |
try: |
| 510 |
self.trigger_debugger() |
| 511 |
finally: |
| 512 |
__pypy__._pypy_disable_remote_debugger = False |
| 513 |
with open(self.outfile) as f: |
| 514 |
content = f.read() |
| 515 |
assert content == 'nothing' |