src/lib/{current-thread,error}.c: put precond. assertion at right place
[babeltrace.git] / src / bindings / python / bt2 / bt2 / utils.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
811644b8 3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b
PP
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
23import bt2
e874da19 24import bt2.logging
d24d5663 25from bt2 import native_bt
81447b5b
PP
26
27
28def _check_bool(o):
29 if not isinstance(o, bool):
30 raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__))
31
32
33def _check_int(o):
34 if not isinstance(o, int):
35 raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__))
36
37
38def _check_float(o):
39 if not isinstance(o, float):
40 raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__))
41
42
43def _check_str(o):
44 if not isinstance(o, str):
45 raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__))
46
47
48def _check_type(o, expected_type):
49 if not isinstance(o, expected_type):
50 raise TypeError("'{}' is not a '{}' object".format(o.__class__.__name__,
51 expected_type))
52
53
c6af194f
PP
54def _is_in_int64_range(v):
55 assert(isinstance(v, int))
81447b5b
PP
56 return v >= -(2**63) and v <= (2**63 - 1)
57
58
c6af194f
PP
59def _is_int64(v):
60 if not isinstance(v, int):
61 return False
62
63 return _is_in_int64_range(v)
64
65
66def _is_in_uint64_range(v):
67 assert(isinstance(v, int))
81447b5b
PP
68 return v >= 0 and v <= (2**64 - 1)
69
70
c6af194f
PP
71def _is_uint64(v):
72 if not isinstance(v, int):
73 return False
74
75 return _is_in_uint64_range(v)
76
77
81447b5b 78def _check_int64(v, msg=None):
c6af194f
PP
79 _check_int(v)
80
81 if not _is_in_int64_range(v):
81447b5b
PP
82 if msg is None:
83 msg = 'expecting a signed 64-bit integral value'
84
85 msg += ' (got {})'.format(v)
86 raise ValueError(msg)
87
88
89def _check_uint64(v, msg=None):
c6af194f
PP
90 _check_int(v)
91
92 if not _is_in_uint64_range(v):
81447b5b
PP
93 if msg is None:
94 msg = 'expecting an unsigned 64-bit integral value'
95
96 msg += ' (got {})'.format(v)
97 raise ValueError(msg)
98
99
100def _is_m1ull(v):
101 return v == 18446744073709551615
102
103
104def _is_pow2(v):
105 return v != 0 and ((v & (v - 1)) == 0)
106
107
108def _check_alignment(a):
109 _check_uint64(a)
110
81447b5b
PP
111 if not _is_pow2(a):
112 raise ValueError('{} is not a power of two'.format(a))
113
114
34af27f5
PP
115def _raise_bt2_error(msg):
116 if msg is None:
117 raise bt2.Error
118 else:
119 raise bt2.Error(msg)
120
0b03f63e 121
81447b5b
PP
122def _handle_ptr(ptr, msg=None):
123 if ptr is None:
34af27f5 124 _raise_bt2_error(msg)
e874da19
PP
125
126
127def _check_log_level(log_level):
128 _check_int(log_level)
129
130 log_levels = (
ef267d12 131 bt2.logging.LoggingLevel.TRACE,
e874da19
PP
132 bt2.logging.LoggingLevel.DEBUG,
133 bt2.logging.LoggingLevel.INFO,
134 bt2.logging.LoggingLevel.WARN,
135 bt2.logging.LoggingLevel.ERROR,
136 bt2.logging.LoggingLevel.FATAL,
137 bt2.logging.LoggingLevel.NONE,
138 )
139
140 if log_level not in log_levels:
141 raise ValueError("'{}' is not a valid logging level".format(log_level))
d24d5663
PP
142
143
144def _handle_func_status(status, msg=None):
145 if status == native_bt.__BT_FUNC_STATUS_OK:
146 # no error
147 return
148
149 if status == native_bt.__BT_FUNC_STATUS_ERROR or status == native_bt.__BT_FUNC_STATUS_MEMORY_ERROR:
150 if msg is None:
151 raise bt2.Error
152 else:
153 raise bt2.Error(msg)
154 elif status == native_bt.__BT_FUNC_STATUS_END:
155 if msg is None:
156 raise bt2.Stop
157 else:
158 raise bt2.Stop(msg)
159 elif status == native_bt.__BT_FUNC_STATUS_AGAIN:
160 if msg is None:
161 raise bt2.TryAgain
162 else:
163 raise bt2.TryAgain(msg)
164 elif status == native_bt.__BT_FUNC_STATUS_CANCELED:
165 if msg is None:
166 raise bt2.Canceled
167 else:
168 raise bt2.Canceled(msg)
169 elif status == native_bt.__BT_FUNC_STATUS_LOADING_ERROR:
170 if msg is None:
171 raise bt2.LoadingError
172 else:
173 raise bt2.LoadingError(msg)
174 elif status == native_bt.__BT_FUNC_STATUS_OVERFLOW:
175 if msg is None:
176 raise bt2.OverflowError
177 else:
178 raise bt2.OverflowError(msg)
179 elif status == native_bt.__BT_FUNC_STATUS_INVALID_OBJECT:
180 if msg is None:
181 raise bt2.InvalidObject
182 else:
183 raise bt2.InvalidObject(msg)
184 elif status == native_bt.__BT_FUNC_STATUS_INVALID_PARAMS:
185 if msg is None:
186 raise bt2.InvalidParams
187 else:
188 raise bt2.InvalidParams(msg)
189 elif status == native_bt.__BT_FUNC_STATUS_UNSUPPORTED:
190 if msg is None:
191 raise bt2.Unsupported
192 else:
193 raise bt2.Unsupported(msg)
194 else:
195 assert False
This page took 0.055847 seconds and 4 git commands to generate.