summaryrefslogtreecommitdiff
path: root/tests/test_mainloop.py
blob: 95e5f7891f34e9367423fb09eddfc7fd6763aff0 (plain)
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
#!/usr/bin/env python

import exceptions
import os
import sys
import select
import unittest

from common import glib

class TestMainLoop(unittest.TestCase):
    def testExceptionHandling(self):
        pipe_r, pipe_w = os.pipe()

        pid = os.fork()
        if pid == 0:
            os.close(pipe_w)
            select.select([pipe_r], [], [])
            os.close(pipe_r)
            os._exit(1)

        def child_died(pid, status, loop):
            loop.quit()
            raise Exception("deadbabe")

        loop = glib.MainLoop()
        glib.child_watch_add(pid, child_died, loop)

        os.close(pipe_r)
        os.write(pipe_w, "Y")
        os.close(pipe_w)

        def excepthook(type, value, traceback):
            assert type is exceptions.Exception
            assert value.args[0] == "deadbabe"
        sys.excepthook = excepthook

        got_exception = False
        try:
            loop.run()
        except:
            got_exception = True

        #
        # The exception should be handled (by printing it)
        # immediately on return from child_died() rather
        # than here. See bug #303573
        #
        sys.excepthook = sys.__excepthook__
        assert not got_exception

if __name__ == '__main__':
    unittest.main()