Python plugin provider: only finalize interpreter if we initialized it
[babeltrace.git] / 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
24
25
26def _check_bool(o):
27 if not isinstance(o, bool):
28 raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__))
29
30
31def _check_int(o):
32 if not isinstance(o, int):
33 raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__))
34
35
36def _check_float(o):
37 if not isinstance(o, float):
38 raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__))
39
40
41def _check_str(o):
42 if not isinstance(o, str):
43 raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__))
44
45
46def _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
52def _is_int64(v):
53 _check_int(v)
54 return v >= -(2**63) and v <= (2**63 - 1)
55
56
57def _is_uint64(v):
58 _check_int(v)
59 return v >= 0 and v <= (2**64 - 1)
60
61
62def _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
71def _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
80def _is_m1ull(v):
81 return v == 18446744073709551615
82
83
84def _is_pow2(v):
85 return v != 0 and ((v & (v - 1)) == 0)
86
87
88def _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
95def _handle_ret(ret, msg=None):
96 if int(ret) < 0:
97 if msg is None:
98 error = bt2.Error()
99 else:
100 error = bt2.Error(msg)
101
102 raise error
103
104
105def _handle_ptr(ptr, msg=None):
106 if ptr is None:
107 if msg is None:
108 error = bt2.Error()
109 else:
110 error = bt2.Error(msg)
111
112 raise error
This page took 0.032622 seconds and 4 git commands to generate.