80e2aec05a3164b0adc7b4befb25568aa110abb6
[platform/upstream/pygobject2.git] / tests / test_mainloop.py
1 # -*- Mode: Python -*-
2
3 import os
4 import sys
5 import select
6 import unittest
7
8 import glib
9
10 from compathelper import _bytes
11
12 class TestMainLoop(unittest.TestCase):
13     def testExceptionHandling(self):
14         pipe_r, pipe_w = os.pipe()
15
16         pid = os.fork()
17         if pid == 0:
18             os.close(pipe_w)
19             select.select([pipe_r], [], [])
20             os.close(pipe_r)
21             os._exit(1)
22
23         def child_died(pid, status, loop):
24             loop.quit()
25             raise Exception("deadbabe")
26
27         loop = glib.MainLoop()
28         glib.child_watch_add(pid, child_died, loop)
29
30         os.close(pipe_r)
31         os.write(pipe_w, _bytes("Y"))
32         os.close(pipe_w)
33
34         def excepthook(type, value, traceback):
35             assert type is Exception
36             assert value.args[0] == "deadbabe"
37         sys.excepthook = excepthook
38
39         got_exception = False
40         try:
41             loop.run()
42         except:
43             got_exception = True
44
45         #
46         # The exception should be handled (by printing it)
47         # immediately on return from child_died() rather
48         # than here. See bug #303573
49         #
50         sys.excepthook = sys.__excepthook__
51         assert not got_exception