Commit | Line | Data |
---|---|---|
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 | ||
23 | import bt2 | |
24 | ||
25 | ||
26 | def _check_bool(o): | |
27 | if not isinstance(o, bool): | |
28 | raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__)) | |
29 | ||
30 | ||
31 | def _check_int(o): | |
32 | if not isinstance(o, int): | |
33 | raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__)) | |
34 | ||
35 | ||
36 | def _check_float(o): | |
37 | if not isinstance(o, float): | |
38 | raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__)) | |
39 | ||
40 | ||
41 | def _check_str(o): | |
42 | if not isinstance(o, str): | |
43 | raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__)) | |
44 | ||
45 | ||
46 | def _check_type(o, expected_type): | |
47 | if not isinstance(o, expected_type): | |
48 | raise TypeError("'{}' is not a '{}' object".format(o.__class__.__name__, | |
49 | expected_type)) | |
50 | ||
51 | ||
52 | def _is_int64(v): | |
53 | _check_int(v) | |
54 | return v >= -(2**63) and v <= (2**63 - 1) | |
55 | ||
56 | ||
57 | def _is_uint64(v): | |
58 | _check_int(v) | |
59 | return v >= 0 and v <= (2**64 - 1) | |
60 | ||
61 | ||
62 | def _check_int64(v, msg=None): | |
63 | if not _is_int64(v): | |
64 | if msg is None: | |
65 | msg = 'expecting a signed 64-bit integral value' | |
66 | ||
67 | msg += ' (got {})'.format(v) | |
68 | raise ValueError(msg) | |
69 | ||
70 | ||
71 | def _check_uint64(v, msg=None): | |
72 | if not _is_uint64(v): | |
73 | if msg is None: | |
74 | msg = 'expecting an unsigned 64-bit integral value' | |
75 | ||
76 | msg += ' (got {})'.format(v) | |
77 | raise ValueError(msg) | |
78 | ||
79 | ||
80 | def _is_m1ull(v): | |
81 | return v == 18446744073709551615 | |
82 | ||
83 | ||
84 | def _is_pow2(v): | |
85 | return v != 0 and ((v & (v - 1)) == 0) | |
86 | ||
87 | ||
88 | def _check_alignment(a): | |
89 | _check_uint64(a) | |
90 | ||
81447b5b PP |
91 | if not _is_pow2(a): |
92 | raise ValueError('{} is not a power of two'.format(a)) | |
93 | ||
94 | ||
34af27f5 PP |
95 | def _raise_bt2_error(msg): |
96 | if msg is None: | |
97 | raise bt2.Error | |
98 | else: | |
99 | raise bt2.Error(msg) | |
100 | ||
0b03f63e | 101 | |
81447b5b PP |
102 | def _handle_ret(ret, msg=None): |
103 | if int(ret) < 0: | |
34af27f5 | 104 | _raise_bt2_error(msg) |
81447b5b PP |
105 | |
106 | ||
107 | def _handle_ptr(ptr, msg=None): | |
108 | if ptr is None: | |
34af27f5 | 109 | _raise_bt2_error(msg) |