| 1 |
import sys |
| 2 |
|
| 3 |
from pypy.module.thread.test.support import GenericTestThread |
| 4 |
|
| 5 |
class AppTestMinimal: |
| 6 |
spaceconfig = dict(usemodules=['__pypy__']) |
| 7 |
|
| 8 |
def test_signal(self): |
| 9 |
from __pypy__ import thread |
| 10 |
assert type(thread.signals_enabled).__module__ == '__pypy__.thread' |
| 11 |
with thread.signals_enabled: |
| 12 |
pass |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class AppTestThreadSignal(GenericTestThread): |
| 17 |
spaceconfig = dict(usemodules=['__pypy__', 'thread', 'signal', 'time']) |
| 18 |
|
| 19 |
def test_exit_twice(self): |
| 20 |
import __pypy__, _thread |
| 21 |
__pypy__.thread._signals_exit() |
| 22 |
try: |
| 23 |
raises(_thread.error, __pypy__.thread._signals_exit) |
| 24 |
finally: |
| 25 |
__pypy__.thread._signals_enter() |
| 26 |
|
| 27 |
def test_enable_signals(self): |
| 28 |
if not self.runappdirect: |
| 29 |
skip("test is flaky when untranslated on bencher4 and aarch64") |
| 30 |
|
| 31 |
import __pypy__, _thread, signal, time, sys |
| 32 |
|
| 33 |
def subthread(): |
| 34 |
print('subthread started') |
| 35 |
try: |
| 36 |
with __pypy__.thread.signals_enabled: |
| 37 |
_thread.interrupt_main() |
| 38 |
for i in range(10): |
| 39 |
print('x') |
| 40 |
time.sleep(0.25) |
| 41 |
except BaseException as e: |
| 42 |
interrupted.append(e) |
| 43 |
finally: |
| 44 |
print('subthread stops, interrupted=%r' % (interrupted,)) |
| 45 |
done.append(None) |
| 46 |
|
| 47 |
|
| 48 |
signal.signal(signal.SIGINT, signal.default_int_handler) |
| 49 |
|
| 50 |
if sys.platform.startswith('win'): |
| 51 |
|
| 52 |
|
| 53 |
sys.stderr |
| 54 |
|
| 55 |
for i in range(10): |
| 56 |
__pypy__.thread._signals_exit() |
| 57 |
try: |
| 58 |
done = [] |
| 59 |
interrupted = [] |
| 60 |
print('--- start ---') |
| 61 |
_thread.start_new_thread(subthread, ()) |
| 62 |
for j in range(30): |
| 63 |
if len(done): break |
| 64 |
print('.') |
| 65 |
time.sleep(0.25) |
| 66 |
print('main thread loop done') |
| 67 |
assert len(done) == 1 |
| 68 |
assert len(interrupted) == 1 |
| 69 |
assert 'KeyboardInterrupt' in interrupted[0].__class__.__name__ |
| 70 |
finally: |
| 71 |
__pypy__.thread._signals_enter() |
| 72 |
|
| 73 |
def test_thread_fork_signals(self): |
| 74 |
import __pypy__ |
| 75 |
import os, _thread, signal, time |
| 76 |
|
| 77 |
if not hasattr(os, 'fork'): |
| 78 |
skip("No fork on this platform") |
| 79 |
|
| 80 |
def fork(): |
| 81 |
time.sleep(0.1) |
| 82 |
with __pypy__.thread.signals_enabled: |
| 83 |
return os.fork() |
| 84 |
|
| 85 |
def threadfunction(): |
| 86 |
pid = fork() |
| 87 |
if pid == 0: |
| 88 |
|
| 89 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 90 |
os._exit(42) |
| 91 |
else: |
| 92 |
self.timeout_killer(pid, 5) |
| 93 |
exitcode = os.waitpid(pid, 0)[1] |
| 94 |
feedback.append(exitcode) |
| 95 |
|
| 96 |
feedback = [] |
| 97 |
_thread.start_new_thread(threadfunction, ()) |
| 98 |
time.sleep(3) |
| 99 |
self.waitfor(lambda: feedback) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
assert feedback == [42<<8] |
| 104 |
|
| 105 |
|
| 106 |
class AppTestThreadSignalLock: |
| 107 |
spaceconfig = dict(usemodules=['__pypy__', 'thread', 'signal']) |
| 108 |
|
| 109 |
def setup_class(cls): |
| 110 |
if (not cls.runappdirect): |
| 111 |
import py |
| 112 |
py.test.skip("this is only a test for -A runs on top of pypy") |
| 113 |
|
| 114 |
def test_enable_signals(self): |
| 115 |
import __pypy__, _thread, time |
| 116 |
|
| 117 |
interrupted = [] |
| 118 |
lock = _thread.allocate_lock() |
| 119 |
lock.acquire() |
| 120 |
|
| 121 |
def subthread(): |
| 122 |
try: |
| 123 |
time.sleep(0.5) |
| 124 |
with __pypy__.thread.signals_enabled: |
| 125 |
_thread.interrupt_main() |
| 126 |
except BaseException as e: |
| 127 |
interrupted.append(e) |
| 128 |
finally: |
| 129 |
lock.release() |
| 130 |
|
| 131 |
_thread.start_new_thread(subthread, ()) |
| 132 |
lock.acquire() |
| 133 |
assert len(interrupted) == 1 |
| 134 |
assert 'KeyboardInterrupt' in interrupted[0].__class__.__name__ |