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
|
# Copyright 2017 Christoph Reiter
#
# This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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 library; if not, see <http://www.gnu.org/licenses/>.
import os
import signal
import unittest
import threading
from contextlib import contextmanager
try:
from gi.repository import Gtk
Gtk_version = Gtk._version
except ImportError:
Gtk = None
Gtk_version = None
from gi.repository import Gio, GLib
from gi._ossighelper import wakeup_on_signal, register_sigint_fallback
class TestOverridesWakeupOnAlarm(unittest.TestCase):
@contextmanager
def _run_with_timeout(self, timeout, abort_func):
failed = []
def fail():
abort_func()
failed.append(1)
return True
fail_id = GLib.timeout_add(timeout, fail)
try:
yield
finally:
GLib.source_remove(fail_id)
self.assertFalse(failed)
def test_basic(self):
self.assertEqual(signal.set_wakeup_fd(-1), -1)
with wakeup_on_signal():
pass
self.assertEqual(signal.set_wakeup_fd(-1), -1)
def test_in_thread(self):
failed = []
def target():
try:
with wakeup_on_signal():
pass
except ValueError or EnvironmentError:
failed.append(1)
t = threading.Thread(target=target)
t.start()
t.join(5)
self.assertFalse(failed)
@unittest.skipIf(os.name == "nt", "not on Windows")
def test_glib_mainloop(self):
loop = GLib.MainLoop()
signal.signal(signal.SIGALRM, lambda *args: loop.quit())
GLib.idle_add(signal.setitimer, signal.ITIMER_REAL, 0.001)
with self._run_with_timeout(2000, loop.quit):
loop.run()
@unittest.skipIf(os.name == "nt", "not on Windows")
def test_gio_application(self):
app = Gio.Application()
signal.signal(signal.SIGALRM, lambda *args: app.quit())
GLib.idle_add(signal.setitimer, signal.ITIMER_REAL, 0.001)
with self._run_with_timeout(2000, app.quit):
app.hold()
app.connect("activate", lambda *args: None)
app.run()
@unittest.skipIf(Gtk is None or os.name == "nt", "not on Windows")
@unittest.skipIf(Gtk is None or Gtk_version == "4.0", "not in gtk4")
def test_gtk_main(self):
signal.signal(signal.SIGALRM, lambda *args: Gtk.main_quit())
GLib.idle_add(signal.setitimer, signal.ITIMER_REAL, 0.001)
with self._run_with_timeout(2000, Gtk.main_quit):
Gtk.main()
@unittest.skipIf(Gtk is None or os.name == "nt", "not on Windows")
@unittest.skipIf(Gtk is None or Gtk_version == "4.0", "not in gtk4")
def test_gtk_dialog_run(self):
w = Gtk.Window()
d = Gtk.Dialog(transient_for=w)
signal.signal(signal.SIGALRM, lambda *args: d.destroy())
GLib.idle_add(signal.setitimer, signal.ITIMER_REAL, 0.001)
with self._run_with_timeout(2000, d.destroy):
d.run()
class TestSigintFallback(unittest.TestCase):
def setUp(self):
self.assertEqual(signal.getsignal(signal.SIGINT), signal.default_int_handler)
def tearDown(self):
self.assertEqual(signal.getsignal(signal.SIGINT), signal.default_int_handler)
def test_replace_handler_and_restore_nested(self):
with register_sigint_fallback(lambda: None):
new_handler = signal.getsignal(signal.SIGINT)
self.assertNotEqual(new_handler, signal.default_int_handler)
with register_sigint_fallback(lambda: None):
self.assertTrue(signal.getsignal(signal.SIGINT) is new_handler)
self.assertEqual(signal.getsignal(signal.SIGINT), signal.default_int_handler)
def test_no_replace_if_not_default(self):
def new_handler(*args):
return None
signal.signal(signal.SIGINT, new_handler)
try:
with register_sigint_fallback(lambda: None):
self.assertTrue(signal.getsignal(signal.SIGINT) is new_handler)
with register_sigint_fallback(lambda: None):
self.assertTrue(signal.getsignal(signal.SIGINT) is new_handler)
self.assertTrue(signal.getsignal(signal.SIGINT) is new_handler)
finally:
signal.signal(signal.SIGINT, signal.default_int_handler)
def test_noop_in_threads(self):
failed = []
def target():
try:
with (
register_sigint_fallback(lambda: None),
register_sigint_fallback(lambda: None),
):
self.assertTrue(
signal.getsignal(signal.SIGINT) is signal.default_int_handler
)
except:
failed.append(1)
t = threading.Thread(target=target)
t.start()
t.join(5)
self.assertFalse(failed)
@unittest.skipIf(os.name == "nt", "not on Windows")
def test_no_replace_if_set_by_glib(self):
id_ = GLib.unix_signal_add(
GLib.PRIORITY_DEFAULT, signal.SIGINT, lambda *args: None
)
try:
# signal.getsignal() doesn't pick up that unix_signal_add()
# has changed the handler, but we should anyway.
self.assertEqual(
signal.getsignal(signal.SIGINT), signal.default_int_handler
)
with register_sigint_fallback(lambda: None):
self.assertEqual(
signal.getsignal(signal.SIGINT), signal.default_int_handler
)
self.assertEqual(
signal.getsignal(signal.SIGINT), signal.default_int_handler
)
finally:
GLib.source_remove(id_)
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.default_int_handler)
|