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 | ||
c6af194f PP |
52 | def _is_in_int64_range(v): |
53 | assert(isinstance(v, int)) | |
81447b5b PP |
54 | return v >= -(2**63) and v <= (2**63 - 1) |
55 | ||
56 | ||
c6af194f PP |
57 | def _is_int64(v): |
58 | if not isinstance(v, int): | |
59 | return False | |
60 | ||
61 | return _is_in_int64_range(v) | |
62 | ||
63 | ||
64 | def _is_in_uint64_range(v): | |
65 | assert(isinstance(v, int)) | |
81447b5b PP |
66 | return v >= 0 and v <= (2**64 - 1) |
67 | ||
68 | ||
c6af194f PP |
69 | def _is_uint64(v): |
70 | if not isinstance(v, int): | |
71 | return False | |
72 | ||
73 | return _is_in_uint64_range(v) | |
74 | ||
75 | ||
81447b5b | 76 | def _check_int64(v, msg=None): |
c6af194f PP |
77 | _check_int(v) |
78 | ||
79 | if not _is_in_int64_range(v): | |
81447b5b PP |
80 | if msg is None: |
81 | msg = 'expecting a signed 64-bit integral value' | |
82 | ||
83 | msg += ' (got {})'.format(v) | |
84 | raise ValueError(msg) | |
85 | ||
86 | ||
87 | def _check_uint64(v, msg=None): | |
c6af194f PP |
88 | _check_int(v) |
89 | ||
90 | if not _is_in_uint64_range(v): | |
81447b5b PP |
91 | if msg is None: |
92 | msg = 'expecting an unsigned 64-bit integral value' | |
93 | ||
94 | msg += ' (got {})'.format(v) | |
95 | raise ValueError(msg) | |
96 | ||
97 | ||
98 | def _is_m1ull(v): | |
99 | return v == 18446744073709551615 | |
100 | ||
101 | ||
102 | def _is_pow2(v): | |
103 | return v != 0 and ((v & (v - 1)) == 0) | |
104 | ||
105 | ||
106 | def _check_alignment(a): | |
107 | _check_uint64(a) | |
108 | ||
81447b5b PP |
109 | if not _is_pow2(a): |
110 | raise ValueError('{} is not a power of two'.format(a)) | |
111 | ||
112 | ||
34af27f5 PP |
113 | def _raise_bt2_error(msg): |
114 | if msg is None: | |
115 | raise bt2.Error | |
116 | else: | |
117 | raise bt2.Error(msg) | |
118 | ||
0b03f63e | 119 | |
81447b5b PP |
120 | def _handle_ret(ret, msg=None): |
121 | if int(ret) < 0: | |
34af27f5 | 122 | _raise_bt2_error(msg) |
81447b5b PP |
123 | |
124 | ||
125 | def _handle_ptr(ptr, msg=None): | |
126 | if ptr is None: | |
34af27f5 | 127 | _raise_bt2_error(msg) |