1 # The MIT License (MIT)
3 # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import bt2
.field_types
25 import collections
.abc
33 def _create_from_ptr(ptr
):
34 # recreate the field type wrapper of this field's type (the identity
35 # could be different, but the underlying address should be the
37 field_type_ptr
= native_bt
.ctf_field_get_type(ptr
)
38 utils
._handle
_ptr
(field_type_ptr
, "cannot get field object's type")
39 field_type
= bt2
.field_types
._create
_from
_ptr
(field_type_ptr
)
40 typeid
= native_bt
.ctf_field_type_get_type_id(field_type
._ptr
)
41 field
= _TYPE_ID_TO_OBJ
[typeid
]._create
_from
_ptr
(ptr
)
42 field
._field
_type
= field_type
46 class _Field(object._Object
, metaclass
=abc
.ABCMeta
):
48 ptr
= native_bt
.ctf_field_copy(self
._ptr
)
49 utils
._handle
_ptr
(ptr
, 'cannot copy {} field object'.format(self
._NAME
.lower()))
50 return _create_from_ptr(ptr
)
52 def __deepcopy__(self
, memo
):
59 return self
._field
_type
62 @functools.total_ordering
63 class _NumericField(_Field
):
65 def _extract_value(other
):
66 if other
is True or other
is False:
69 if isinstance(other
, numbers
.Integral
):
72 if isinstance(other
, numbers
.Real
):
75 if isinstance(other
, numbers
.Complex
):
78 raise TypeError("'{}' object is not a number object".format(other
.__class
__.__name
__))
81 return int(self
.value
)
84 return float(self
.value
)
87 return str(self
.value
)
89 def __lt__(self
, other
):
90 if not isinstance(other
, numbers
.Number
):
91 raise TypeError('unorderable types: {}() < {}()'.format(self
.__class
__.__name
__,
92 other
.__class
__.__name
__))
94 return self
.value
< float(other
)
96 def __le__(self
, other
):
97 if not isinstance(other
, numbers
.Number
):
98 raise TypeError('unorderable types: {}() <= {}()'.format(self
.__class
__.__name
__,
99 other
.__class
__.__name
__))
101 return self
.value
<= float(other
)
103 def __eq__(self
, other
):
104 if not isinstance(other
, numbers
.Number
):
107 return self
.value
== complex(other
)
109 def __rmod__(self
, other
):
110 return self
._extract
_value
(other
) % self
.value
112 def __mod__(self
, other
):
113 return self
.value
% self
._extract
_value
(other
)
115 def __rfloordiv__(self
, other
):
116 return self
._extract
_value
(other
) // self
.value
118 def __floordiv__(self
, other
):
119 return self
.value
// self
._extract
_value
(other
)
121 def __round__(self
, ndigits
=None):
123 return round(self
.value
)
125 return round(self
.value
, ndigits
)
128 return math
.ceil(self
.value
)
131 return math
.floor(self
.value
)
134 return int(self
.value
)
137 return abs(self
.value
)
139 def __add__(self
, other
):
140 return self
.value
+ self
._extract
_value
(other
)
142 def __radd__(self
, other
):
143 return self
.__add
__(other
)
151 def __mul__(self
, other
):
152 return self
.value
* self
._extract
_value
(other
)
154 def __rmul__(self
, other
):
155 return self
.__mul
__(other
)
157 def __truediv__(self
, other
):
158 return self
.value
/ self
._extract
_value
(other
)
160 def __rtruediv__(self
, other
):
161 return self
._extract
_value
(other
) / self
.value
163 def __pow__(self
, exponent
):
164 return self
.value
** self
._extract
_value
(exponent
)
166 def __rpow__(self
, base
):
167 return self
._extract
_value
(base
) ** self
.value
169 def __iadd__(self
, other
):
170 self
.value
= self
+ other
173 def __isub__(self
, other
):
174 self
.value
= self
- other
177 def __imul__(self
, other
):
178 self
.value
= self
* other
181 def __itruediv__(self
, other
):
182 self
.value
= self
/ other
185 def __ifloordiv__(self
, other
):
186 self
.value
= self
// other
189 def __imod__(self
, other
):
190 self
.value
= self
% other
193 def __ipow__(self
, other
):
194 self
.value
= self
** other
198 class _IntegralField(_NumericField
, numbers
.Integral
):
199 def __lshift__(self
, other
):
200 return self
.value
<< self
._extract
_value
(other
)
202 def __rlshift__(self
, other
):
203 return self
._extract
_value
(other
) << self
.value
205 def __rshift__(self
, other
):
206 return self
.value
>> self
._extract
_value
(other
)
208 def __rrshift__(self
, other
):
209 return self
._extract
_value
(other
) >> self
.value
211 def __and__(self
, other
):
212 return self
.value
& self
._extract
_value
(other
)
214 def __rand__(self
, other
):
215 return self
._extract
_value
(other
) & self
.value
217 def __xor__(self
, other
):
218 return self
.value ^ self
._extract
_value
(other
)
220 def __rxor__(self
, other
):
221 return self
._extract
_value
(other
) ^ self
.value
223 def __or__(self
, other
):
224 return self
.value | self
._extract
_value
(other
)
226 def __ror__(self
, other
):
227 return self
._extract
_value
(other
) | self
.value
229 def __invert__(self
):
232 def __ilshift__(self
, other
):
233 self
.value
= self
<< other
236 def __irshift__(self
, other
):
237 self
.value
= self
>> other
240 def __iand__(self
, other
):
241 self
.value
= self
& other
244 def __ixor__(self
, other
):
245 self
.value
= self ^ other
248 def __ior__(self
, other
):
249 self
.value
= self | other
253 class _RealField(_NumericField
, numbers
.Real
):
257 class _IntegerField(_IntegralField
):
260 def _value_to_int(self
, value
):
261 if not isinstance(value
, numbers
.Real
):
262 raise TypeError('expecting a real number object')
266 if self
.field_type
.is_signed
:
267 utils
._check
_int
64(value
)
269 utils
._check
_uint
64(value
)
275 if self
.field_type
.is_signed
:
276 ret
, value
= native_bt
.ctf_field_signed_integer_get_value(self
._ptr
)
278 ret
, value
= native_bt
.ctf_field_unsigned_integer_get_value(self
._ptr
)
280 utils
._handle
_ret
(ret
, "cannot get integer field object's value")
284 def value(self
, value
):
285 value
= self
._value
_to
_int
(value
)
287 if self
.field_type
.is_signed
:
288 ret
= native_bt
.ctf_field_signed_integer_set_value(self
._ptr
, value
)
290 ret
= native_bt
.ctf_field_unsigned_integer_set_value(self
._ptr
, value
)
292 utils
._handle
_ret
(ret
, "cannot set integer field object's value")
295 class _FloatingPointNumberField(_RealField
):
296 _NAME
= 'Floating point number'
298 def _value_to_float(self
, value
):
299 if not isinstance(value
, numbers
.Real
):
300 raise TypeError("expecting a real number object")
306 ret
, value
= native_bt
.ctf_field_floating_point_get_value(self
._ptr
)
307 utils
._handle
_ret
(ret
, "cannot get floating point number field object's value")
311 def value(self
, value
):
312 value
= self
._value
_to
_float
(value
)
313 ret
= native_bt
.ctf_field_floating_point_set_value(self
._ptr
, value
)
314 utils
._handle
_ret
(ret
, "cannot set floating point number field object's value")
317 class _EnumerationField(_IntegerField
):
318 _NAME
= 'Enumeration'
321 def integer_field(self
):
322 int_field_ptr
= native_bt
.ctf_field_enumeration_get_container(self
._ptr
)
323 utils
._handle
_ptr
(int_field_ptr
,
324 "cannot get enumeration field object's underlying integer field")
325 return _create_from_ptr(int_field_ptr
)
329 return self
.integer_field
.value
332 def value(self
, value
):
333 self
.integer_field
.value
= value
337 iter_ptr
= bt_ctf_field_enumeration_get_mappings(self
._ptr
)
338 return self
.field_type
._get
_mapping
_iter
(iter_ptr
)
341 @functools.total_ordering
342 class _StringField(_Field
, collections
.abc
.Sequence
):
345 def _value_to_str(self
, value
):
346 if isinstance(value
, self
.__class
__):
349 if not isinstance(value
, str):
350 raise TypeError("expecting a 'str' object")
356 value
= native_bt
.ctf_field_string_get_value(self
._ptr
)
357 utils
._handle
_ptr
(value
, "cannot get string field object's value")
361 def value(self
, value
):
362 value
= self
._value
_to
_str
(value
)
363 ret
= native_bt
.ctf_field_string_set_value(self
._ptr
, value
)
364 utils
._handle
_ret
(ret
, "cannot set string field object's value")
366 def __eq__(self
, other
):
368 other
= self
._value
_to
_str
(other
)
372 return self
.value
== other
374 def __le__(self
, other
):
375 return self
.value
<= self
._value
_to
_str
(other
)
377 def __lt__(self
, other
):
378 return self
.value
< self
._value
_to
_str
(other
)
381 return bool(self
.value
)
386 def __getitem__(self
, index
):
387 return self
.value
[index
]
390 return len(self
.value
)
392 def __iadd__(self
, value
):
393 value
= self
._value
_to
_str
(value
)
394 ret
= native_bt
.ctf_field_string_append(self
._ptr
, value
)
395 utils
._handle
_ret
(ret
, "cannot append to string field object's value")
399 class _ContainerField(_Field
):
401 return len(self
) != 0
404 count
= self
._count
()
405 utils
._handle
_ret
(count
, "cannot get {} field object's field count".format(self
._NAME
.lower()))
408 def __delitem__(self
, index
):
409 raise NotImplementedError
412 class _StructureField(_ContainerField
, collections
.abc
.MutableMapping
):
416 return len(self
.field_type
)
418 def __getitem__(self
, key
):
419 utils
._check
_str
(key
)
420 ptr
= native_bt
.ctf_field_structure_get_field(self
._ptr
, key
)
425 return _create_from_ptr(ptr
)
427 def __setitem__(self
, key
, value
):
428 # we can only set numbers and strings
429 if not isinstance(value
, (numbers
.Number
, str)):
430 raise TypeError('expecting number object or string')
432 # raises if index is somehow invalid
435 if not isinstance(field
, (_NumericField
, _StringField
)):
436 raise TypeError('can only set the value of a number or string field')
438 # the field's property does the appropriate conversion or raises
439 # the appropriate exception
442 def at_index(self
, index
):
443 utils
._check
_uint
64(index
)
444 field_ptr
= native_bt
.ctf_field_structure_get_field_by_index(self
._ptr
, index
)
445 utils
._handle
_ptr
(field_ptr
, "cannot get structure field object's field")
446 return _create_from_ptr(field_ptr
)
450 return iter(self
.field_type
)
452 def __eq__(self
, other
):
453 if not isinstance(other
, collections
.abc
.Mapping
):
456 if len(self
) != len(other
):
459 for self_key
, self_value
in self
.items():
460 if self_key
not in other
:
463 other_value
= other
[self_key
]
465 if self_value
!= other_value
:
471 class _VariantField(_Field
):
476 field_ptr
= native_bt
.ctf_field_variant_get_tag(self
._ptr
)
477 utils
._handle
_ptr
(field_ptr
, "cannot get variant field object's tag field")
478 return _create_from_ptr(field_ptr
)
481 def selected_field(self
):
484 def field(self
, tag_field
=None):
485 if tag_field
is None:
486 field_ptr
= native_bt
.ctf_field_variant_get_current_field(self
._ptr
)
487 utils
._handle
_ptr
(field_ptr
, "cannot get variant field object's selected field")
489 utils
._check
_type
(tag_field
, _EnumerationField
)
490 field_ptr
= native_bt
.ctf_field_variant_get_field(self
._ptr
, tag_field
._ptr
)
491 utils
._handle
_ptr
(field_ptr
, "cannot select variant field object's field")
493 return _create_from_ptr(field_ptr
)
495 def __eq__(self
, other
):
496 return self
.field
== other
499 class _ArraySequenceField(_ContainerField
, collections
.abc
.MutableSequence
):
500 def __getitem__(self
, index
):
501 if not isinstance(index
, numbers
.Integral
):
502 raise TypeError("'{}' is not an integral number object: invalid index".format(index
.__class
__.__name
__))
506 if index
< 0 or index
>= len(self
):
507 raise IndexError('{} field object index is out of range'.format(self
._NAME
))
509 field_ptr
= self
._get
_field
_ptr
_at
_index
(index
)
510 utils
._handle
_ptr
(field_ptr
, "cannot get {} field object's field".format(self
._NAME
))
511 return _create_from_ptr(field_ptr
)
513 def __setitem__(self
, index
, value
):
514 # we can only set numbers and strings
515 if not isinstance(value
, (numbers
.Number
, _StringField
, str)):
516 raise TypeError('expecting number or string object')
518 # raises if index is somehow invalid
521 if not isinstance(field
, (_NumericField
, _StringField
)):
522 raise TypeError('can only set the value of a number or string field')
524 # the field's property does the appropriate conversion or raises
525 # the appropriate exception
528 def insert(self
, index
, value
):
529 raise NotImplementedError
531 def __eq__(self
, other
):
532 if not isinstance(other
, collections
.abc
.Sequence
):
535 if len(self
) != len(other
):
538 for self_field
, other_field
in zip(self
, other
):
539 if self_field
!= other_field
:
545 class _ArrayField(_ArraySequenceField
):
549 return self
.field_type
.length
551 def _get_field_ptr_at_index(self
, index
):
552 return native_bt
.ctf_field_array_get_field(self
._ptr
, index
)
555 class _SequenceField(_ArraySequenceField
):
559 return self
.length_field
.value
562 def length_field(self
):
563 field_ptr
= native_bt
.ctf_field_sequence_get_length(self
._ptr
)
564 utils
._handle
_ptr
("cannot get sequence field object's length field")
565 return _create_from_ptr(field_ptr
)
568 def length_field(self
, length_field
):
569 utils
._check
_type
(length_field
, _IntegerField
)
570 ret
= native_bt
.ctf_field_sequence_set_length(self
._ptr
, length_field
._ptr
)
571 utils
._handle
_ret
(ret
, "cannot set sequence field object's length field")
573 def _get_field_ptr_at_index(self
, index
):
574 return native_bt
.ctf_field_sequence_get_field(self
._ptr
, index
)
578 native_bt
.CTF_TYPE_ID_INTEGER
: _IntegerField
,
579 native_bt
.CTF_TYPE_ID_FLOAT
: _FloatingPointNumberField
,
580 native_bt
.CTF_TYPE_ID_ENUM
: _EnumerationField
,
581 native_bt
.CTF_TYPE_ID_STRING
: _StringField
,
582 native_bt
.CTF_TYPE_ID_STRUCT
: _StructureField
,
583 native_bt
.CTF_TYPE_ID_ARRAY
: _ArrayField
,
584 native_bt
.CTF_TYPE_ID_SEQUENCE
: _SequenceField
,
585 native_bt
.CTF_TYPE_ID_VARIANT
: _VariantField
,
586 native_bt
.CTF_TYPE_ID_UNTAGGED_VARIANT
: _VariantField
,