Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
f6a5e476 | 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 | ||
23 | import bt2 | |
cc81b5ab | 24 | import bt2.logging |
81447b5b PP |
25 | |
26 | ||
27 | def _check_bool(o): | |
28 | if not isinstance(o, bool): | |
29 | raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__)) | |
30 | ||
31 | ||
32 | def _check_int(o): | |
33 | if not isinstance(o, int): | |
34 | raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__)) | |
35 | ||
36 | ||
37 | def _check_float(o): | |
38 | if not isinstance(o, float): | |
39 | raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__)) | |
40 | ||
41 | ||
42 | def _check_str(o): | |
43 | if not isinstance(o, str): | |
44 | raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__)) | |
45 | ||
46 | ||
47 | def _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 | ||
dcd94213 PP |
53 | def _is_in_int64_range(v): |
54 | assert(isinstance(v, int)) | |
81447b5b PP |
55 | return v >= -(2**63) and v <= (2**63 - 1) |
56 | ||
57 | ||
dcd94213 PP |
58 | def _is_int64(v): |
59 | if not isinstance(v, int): | |
60 | return False | |
61 | ||
62 | return _is_in_int64_range(v) | |
63 | ||
64 | ||
65 | def _is_in_uint64_range(v): | |
66 | assert(isinstance(v, int)) | |
81447b5b PP |
67 | return v >= 0 and v <= (2**64 - 1) |
68 | ||
69 | ||
dcd94213 PP |
70 | def _is_uint64(v): |
71 | if not isinstance(v, int): | |
72 | return False | |
73 | ||
74 | return _is_in_uint64_range(v) | |
75 | ||
76 | ||
81447b5b | 77 | def _check_int64(v, msg=None): |
dcd94213 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 | ||
88 | def _check_uint64(v, msg=None): | |
dcd94213 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 | ||
99 | def _is_m1ull(v): | |
100 | return v == 18446744073709551615 | |
101 | ||
102 | ||
103 | def _is_pow2(v): | |
104 | return v != 0 and ((v & (v - 1)) == 0) | |
105 | ||
106 | ||
107 | def _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 | ||
447c0da9 PP |
114 | def _raise_bt2_error(msg): |
115 | if msg is None: | |
116 | raise bt2.Error | |
117 | else: | |
118 | raise bt2.Error(msg) | |
119 | ||
5c8b3186 | 120 | |
81447b5b PP |
121 | def _handle_ret(ret, msg=None): |
122 | if int(ret) < 0: | |
447c0da9 | 123 | _raise_bt2_error(msg) |
81447b5b PP |
124 | |
125 | ||
126 | def _handle_ptr(ptr, msg=None): | |
127 | if ptr is None: | |
447c0da9 | 128 | _raise_bt2_error(msg) |
cc81b5ab PP |
129 | |
130 | ||
131 | def _check_log_level(log_level): | |
132 | _check_int(log_level) | |
133 | ||
134 | log_levels = ( | |
135 | bt2.logging.LoggingLevel.VERBOSE, | |
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)) |