Remove `skip-string-normalization` in Python formatter config
[babeltrace.git] / src / bindings / python / bt2 / bt2 / utils.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
81447b5b 2#
811644b8 3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b
PP
4
5import bt2
3fb99a22 6from bt2 import logging as bt2_logging
d24d5663 7from bt2 import native_bt
81447b5b
PP
8
9
10def _check_bool(o):
11 if not isinstance(o, bool):
12 raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__))
13
14
15def _check_int(o):
16 if not isinstance(o, int):
17 raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__))
18
19
20def _check_float(o):
21 if not isinstance(o, float):
22 raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__))
23
24
25def _check_str(o):
26 if not isinstance(o, str):
27 raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__))
28
29
30def _check_type(o, expected_type):
31 if not isinstance(o, expected_type):
cfbd7cf3
FD
32 raise TypeError(
33 "'{}' is not a '{}' object".format(o.__class__.__name__, expected_type)
34 )
81447b5b
PP
35
36
c6af194f 37def _is_in_int64_range(v):
cfbd7cf3 38 assert isinstance(v, int)
768f9bcb 39 return v >= -(2**63) and v <= (2**63 - 1)
81447b5b
PP
40
41
c6af194f
PP
42def _is_int64(v):
43 if not isinstance(v, int):
44 return False
45
46 return _is_in_int64_range(v)
47
48
49def _is_in_uint64_range(v):
cfbd7cf3 50 assert isinstance(v, int)
768f9bcb 51 return v >= 0 and v <= (2**64 - 1)
81447b5b
PP
52
53
c6af194f
PP
54def _is_uint64(v):
55 if not isinstance(v, int):
56 return False
57
58 return _is_in_uint64_range(v)
59
60
81447b5b 61def _check_int64(v, msg=None):
c6af194f
PP
62 _check_int(v)
63
64 if not _is_in_int64_range(v):
81447b5b 65 if msg is None:
f5567ea8 66 msg = "expecting a signed 64-bit integral value"
81447b5b 67
f5567ea8 68 msg += " (got {})".format(v)
81447b5b
PP
69 raise ValueError(msg)
70
71
72def _check_uint64(v, msg=None):
c6af194f
PP
73 _check_int(v)
74
75 if not _is_in_uint64_range(v):
81447b5b 76 if msg is None:
f5567ea8 77 msg = "expecting an unsigned 64-bit integral value"
81447b5b 78
f5567ea8 79 msg += " (got {})".format(v)
81447b5b
PP
80 raise ValueError(msg)
81
82
83def _is_m1ull(v):
84 return v == 18446744073709551615
85
86
87def _is_pow2(v):
88 return v != 0 and ((v & (v - 1)) == 0)
89
90
91def _check_alignment(a):
92 _check_uint64(a)
93
81447b5b 94 if not _is_pow2(a):
f5567ea8 95 raise ValueError("{} is not a power of two".format(a))
81447b5b
PP
96
97
e874da19
PP
98def _check_log_level(log_level):
99 _check_int(log_level)
100
101 log_levels = (
3fb99a22
PP
102 bt2_logging.LoggingLevel.TRACE,
103 bt2_logging.LoggingLevel.DEBUG,
104 bt2_logging.LoggingLevel.INFO,
105 bt2_logging.LoggingLevel.WARNING,
106 bt2_logging.LoggingLevel.ERROR,
107 bt2_logging.LoggingLevel.FATAL,
108 bt2_logging.LoggingLevel.NONE,
e874da19
PP
109 )
110
111 if log_level not in log_levels:
112 raise ValueError("'{}' is not a valid logging level".format(log_level))
d24d5663
PP
113
114
115def _handle_func_status(status, msg=None):
116 if status == native_bt.__BT_FUNC_STATUS_OK:
117 # no error
118 return
119
4acc866e 120 if status == native_bt.__BT_FUNC_STATUS_ERROR:
ce4923b0 121 assert msg is not None
694c792b 122 raise bt2._Error(msg)
4acc866e
SM
123 elif status == native_bt.__BT_FUNC_STATUS_MEMORY_ERROR:
124 assert msg is not None
694c792b 125 raise bt2._MemoryError(msg)
d24d5663
PP
126 elif status == native_bt.__BT_FUNC_STATUS_END:
127 if msg is None:
128 raise bt2.Stop
129 else:
130 raise bt2.Stop(msg)
131 elif status == native_bt.__BT_FUNC_STATUS_AGAIN:
132 if msg is None:
133 raise bt2.TryAgain
134 else:
135 raise bt2.TryAgain(msg)
520cdc82 136 elif status == native_bt.__BT_FUNC_STATUS_OVERFLOW_ERROR:
d24d5663 137 if msg is None:
cb06aa27 138 raise bt2._OverflowError
d24d5663 139 else:
cb06aa27 140 raise bt2._OverflowError(msg)
76b6c2f7 141 elif status == native_bt.__BT_FUNC_STATUS_UNKNOWN_OBJECT:
d24d5663 142 if msg is None:
76b6c2f7 143 raise bt2.UnknownObject
d24d5663 144 else:
76b6c2f7 145 raise bt2.UnknownObject(msg)
d24d5663
PP
146 else:
147 assert False
2a52034a
PP
148
149
150class _ListenerHandle:
67de22ba
SM
151 def __init__(self, addr):
152 self._addr = addr
153 self._listener_id = None
154
155 def _set_listener_id(self, listener_id):
2a52034a 156 self._listener_id = listener_id
67de22ba
SM
157
158 def _invalidate(self):
159 self._listener_id = None
This page took 0.078765 seconds and 4 git commands to generate.