| 1 |
import contextlib |
| 2 |
import unittest |
| 3 |
import inspect |
| 4 |
import warnings |
| 5 |
import functools |
| 6 |
import sys |
| 7 |
from collections import namedtuple |
| 8 |
from io import StringIO |
| 9 |
|
| 10 |
import gi |
| 11 |
from gi import PyGIDeprecationWarning |
| 12 |
from gi.repository import GLib |
| 13 |
|
| 14 |
|
| 15 |
ExceptionInfo = namedtuple("ExceptionInfo", ["type", "value", "traceback"]) |
| 16 |
"""The type used for storing exceptions used by capture_exceptions()""" |
| 17 |
|
| 18 |
|
| 19 |
@contextlib.contextmanager |
| 20 |
def capture_exceptions(): |
| 21 |
"""Installs a temporary sys.excepthook which records all exceptions |
| 22 |
instead of printing them. |
| 23 |
""" |
| 24 |
exceptions = [] |
| 25 |
|
| 26 |
def custom_excepthook(*args): |
| 27 |
exceptions.append(ExceptionInfo(*args)) |
| 28 |
|
| 29 |
old_hook = sys.excepthook |
| 30 |
sys.excepthook = custom_excepthook |
| 31 |
try: |
| 32 |
yield exceptions |
| 33 |
finally: |
| 34 |
sys.excepthook = old_hook |
| 35 |
|
| 36 |
|
| 37 |
def ignore_gi_deprecation_warnings(func_or_class): |
| 38 |
"""A unittest class and function decorator which makes them ignore |
| 39 |
PyGIDeprecationWarning. |
| 40 |
""" |
| 41 |
if inspect.isclass(func_or_class): |
| 42 |
assert issubclass(func_or_class, unittest.TestCase) |
| 43 |
cls = func_or_class |
| 44 |
for name, value in cls.__dict__.items(): |
| 45 |
if callable(value) and name.startswith("test_"): |
| 46 |
new_value = ignore_gi_deprecation_warnings(value) |
| 47 |
setattr(cls, name, new_value) |
| 48 |
return cls |
| 49 |
func = func_or_class |
| 50 |
|
| 51 |
@functools.wraps(func) |
| 52 |
def wrapper(*args, **kwargs): |
| 53 |
with capture_gi_deprecation_warnings(): |
| 54 |
return func(*args, **kwargs) |
| 55 |
|
| 56 |
return wrapper |
| 57 |
|
| 58 |
|
| 59 |
@contextlib.contextmanager |
| 60 |
def capture_gi_deprecation_warnings(): |
| 61 |
"""Temporarily suppress PyGIDeprecationWarning output and record them.""" |
| 62 |
with warnings.catch_warnings(record=True) as warn: |
| 63 |
warnings.simplefilter("always", category=PyGIDeprecationWarning) |
| 64 |
yield warn |
| 65 |
|
| 66 |
|
| 67 |
@contextlib.contextmanager |
| 68 |
def capture_glib_warnings(allow_warnings=False, allow_criticals=False): |
| 69 |
"""Temporarily suppress glib warning output and record them. |
| 70 |
|
| 71 |
The test suite is run with G_DEBUG="fatal-warnings fatal-criticals" |
| 72 |
by default. Setting allow_warnings and allow_criticals will temporarily |
| 73 |
allow warnings or criticals without terminating the test run. |
| 74 |
""" |
| 75 |
old_mask = GLib.log_set_always_fatal(GLib.LogLevelFlags(0)) |
| 76 |
|
| 77 |
new_mask = old_mask |
| 78 |
if allow_warnings: |
| 79 |
new_mask &= ~GLib.LogLevelFlags.LEVEL_WARNING |
| 80 |
if allow_criticals: |
| 81 |
new_mask &= ~GLib.LogLevelFlags.LEVEL_CRITICAL |
| 82 |
|
| 83 |
GLib.log_set_always_fatal(GLib.LogLevelFlags(new_mask)) |
| 84 |
|
| 85 |
GLibWarning = gi._gi.Warning |
| 86 |
try: |
| 87 |
with warnings.catch_warnings(record=True) as warn: |
| 88 |
warnings.filterwarnings("always", category=GLibWarning) |
| 89 |
yield warn |
| 90 |
finally: |
| 91 |
GLib.log_set_always_fatal(old_mask) |
| 92 |
|
| 93 |
|
| 94 |
@contextlib.contextmanager |
| 95 |
def capture_glib_deprecation_warnings(): |
| 96 |
"""Temporarily suppress glib deprecation warning output and record them.""" |
| 97 |
GLibWarning = gi._gi.Warning |
| 98 |
with warnings.catch_warnings(record=True) as warn: |
| 99 |
warnings.filterwarnings( |
| 100 |
"always", |
| 101 |
category=GLibWarning, |
| 102 |
message=".+ is deprecated and shouldn't be used anymore\\. " |
| 103 |
"It will be removed in a future version\\.", |
| 104 |
) |
| 105 |
yield warn |
| 106 |
|
| 107 |
|
| 108 |
@contextlib.contextmanager |
| 109 |
def capture_output(): |
| 110 |
"""With capture_output() as (stdout, stderr): |
| 111 |
some_action() |
| 112 |
print(stdout.getvalue(), stderr.getvalue()). |
| 113 |
""" |
| 114 |
err = StringIO() |
| 115 |
out = StringIO() |
| 116 |
old_err = sys.stderr |
| 117 |
old_out = sys.stdout |
| 118 |
sys.stderr = err |
| 119 |
sys.stdout = out |
| 120 |
|
| 121 |
try: |
| 122 |
yield (out, err) |
| 123 |
finally: |
| 124 |
sys.stderr = old_err |
| 125 |
sys.stdout = old_out |