1# Copyright 2018 Christoph Reiter
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8import sys
9
10import pytest
11
12
13@pytest.hookimpl(hookwrapper=True)
14def pytest_runtest_call(item):
15    """A pytest hook which takes over sys.excepthook and raises any uncaught
16    exception (with PyGObject this happesn often when we get called from C,
17    like any signal handler, vfuncs tc)
18    """
19
20    assert sys.excepthook is sys.__excepthook__
21
22    exceptions = []
23
24    def on_hook(type_, value, tback):
25        exceptions.append((type_, value, tback))
26
27    sys.excepthook = on_hook
28    try:
29        yield
30    finally:
31        sys.excepthook = sys.__excepthook__
32        if exceptions:
33            tp, value, tb = exceptions[0]
34            raise tp(value).with_traceback(tb)
35