summaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: 50b3f75f235fa786116af08b5ff6bd1a216fcb70 (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
# -*- coding: utf-8 -*-

import sys

import pytest

from gi._compat import reraise


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
    """A pytest hook which takes over sys.excepthook and raises any uncaught
    exception (with PyGObject this happesn often when we get called from C,
    like any signal handler, vfuncs tc)
    """

    assert sys.excepthook is sys.__excepthook__

    exceptions = []

    def on_hook(type_, value, tback):
        exceptions.append((type_, value, tback))

    sys.excepthook = on_hook
    try:
        yield
    finally:
        assert sys.excepthook in (on_hook, sys.__excepthook__)
        sys.excepthook = sys.__excepthook__
        if exceptions:
            reraise(*exceptions[0])