tests/lib: remove `test_bt_values` and `test_graph_topo`
[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
81447b5b
PP
25
26
27def _check_bool(o):
28 if not isinstance(o, bool):
29 raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__))
30
31
32def _check_int(o):
33 if not isinstance(o, int):
34 raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__))
35
36
37def _check_float(o):
38 if not isinstance(o, float):
39 raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__))
40
41
42def _check_str(o):
43 if not isinstance(o, str):
44 raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__))
45
46
47def _check_type(o, expected_type):
48 if not isinstance(o, expected_type):
49 raise TypeError("'{}' is not a '{}' object".format(o.__class__.__name__,
50 expected_type))
51
52
c6af194f
PP
53def _is_in_int64_range(v):
54 assert(isinstance(v, int))
81447b5b
PP
55 return v >= -(2**63) and v <= (2**63 - 1)
56
57
c6af194f
PP
58def _is_int64(v):
59 if not isinstance(v, int):
60 return False
61
62 return _is_in_int64_range(v)
63
64
65def _is_in_uint64_range(v):
66 assert(isinstance(v, int))
81447b5b
PP
67 return v >= 0 and v <= (2**64 - 1)
68
69
c6af194f
PP
70def _is_uint64(v):
71 if not isinstance(v, int):
72 return False
73
74 return _is_in_uint64_range(v)
75
76
81447b5b 77def _check_int64(v, msg=None):
c6af194f
PP
78 _check_int(v)
79
80 if not _is_in_int64_range(v):
81447b5b
PP
81 if msg is None:
82 msg = 'expecting a signed 64-bit integral value'
83
84 msg += ' (got {})'.format(v)
85 raise ValueError(msg)
86
87
88def _check_uint64(v, msg=None):
c6af194f
PP
89 _check_int(v)
90
91 if not _is_in_uint64_range(v):
81447b5b
PP
92 if msg is None:
93 msg = 'expecting an unsigned 64-bit integral value'
94
95 msg += ' (got {})'.format(v)
96 raise ValueError(msg)
97
98
99def _is_m1ull(v):
100 return v == 18446744073709551615
101
102
103def _is_pow2(v):
104 return v != 0 and ((v & (v - 1)) == 0)
105
106
107def _check_alignment(a):
108 _check_uint64(a)
109
81447b5b
PP
110 if not _is_pow2(a):
111 raise ValueError('{} is not a power of two'.format(a))
112
113
34af27f5
PP
114def _raise_bt2_error(msg):
115 if msg is None:
116 raise bt2.Error
117 else:
118 raise bt2.Error(msg)
119
0b03f63e 120
81447b5b
PP
121def _handle_ret(ret, msg=None):
122 if int(ret) < 0:
34af27f5 123 _raise_bt2_error(msg)
81447b5b
PP
124
125
126def _handle_ptr(ptr, msg=None):
127 if ptr is None:
34af27f5 128 _raise_bt2_error(msg)
e874da19
PP
129
130
131def _check_log_level(log_level):
132 _check_int(log_level)
133
134 log_levels = (
ef267d12 135 bt2.logging.LoggingLevel.TRACE,
e874da19
PP
136 bt2.logging.LoggingLevel.DEBUG,
137 bt2.logging.LoggingLevel.INFO,
138 bt2.logging.LoggingLevel.WARN,
139 bt2.logging.LoggingLevel.ERROR,
140 bt2.logging.LoggingLevel.FATAL,
141 bt2.logging.LoggingLevel.NONE,
142 )
143
144 if log_level not in log_levels:
145 raise ValueError("'{}' is not a valid logging level".format(log_level))
This page took 0.046241 seconds and 4 git commands to generate.