functions: revert the function init order to make pylint happy again. See #217
[pygobject.git] / tests / test_mainloop.py
blob40a8b97b144eb22c354a63b6561d9dfab13b0c11
1 # -*- Mode: Python -*-
3 from __future__ import absolute_import
5 import os
6 import select
7 import signal
8 import unittest
10 from gi.repository import GLib
12 from .helper import capture_exceptions
15 class TestMainLoop(unittest.TestCase):
17 @unittest.skipUnless(hasattr(os, "fork"), "no os.fork available")
18 def test_exception_handling(self):
19 pipe_r, pipe_w = os.pipe()
21 pid = os.fork()
22 if pid == 0:
23 os.close(pipe_w)
24 select.select([pipe_r], [], [])
25 os.close(pipe_r)
26 os._exit(1)
28 def child_died(pid, status, loop):
29 loop.quit()
30 raise Exception("deadbabe")
32 loop = GLib.MainLoop()
33 GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, child_died, loop)
35 os.close(pipe_r)
36 os.write(pipe_w, b"Y")
37 os.close(pipe_w)
39 with capture_exceptions() as exc:
40 loop.run()
42 assert len(exc) == 1
43 assert exc[0].type is Exception
44 assert exc[0].value.args[0] == "deadbabe"
46 @unittest.skipUnless(hasattr(os, "fork"), "no os.fork available")
47 @unittest.skipIf(os.environ.get("PYGI_TEST_GDB"), "SIGINT stops gdb")
48 def test_sigint(self):
49 r, w = os.pipe()
50 pid = os.fork()
51 if pid == 0:
52 # wait for the parent process loop to start
53 os.read(r, 1)
54 os.close(r)
56 os.kill(os.getppid(), signal.SIGINT)
57 os._exit(0)
59 def notify_child():
60 # tell the child that it can kill the parent
61 os.write(w, b"X")
62 os.close(w)
64 GLib.idle_add(notify_child)
65 loop = GLib.MainLoop()
66 try:
67 loop.run()
68 self.fail('expected KeyboardInterrupt exception')
69 except KeyboardInterrupt:
70 pass
71 self.assertFalse(loop.is_running())
72 os.waitpid(pid, 0)