1 # The MIT License (MIT)
3 # Copyright (c) 2017 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
)
287 def value(self
, value
):
288 value
= self
._value
_to
_int
(value
)
290 if self
.field_type
.is_signed
:
291 ret
= native_bt
.ctf_field_signed_integer_set_value(self
._ptr
, value
)
293 ret
= native_bt
.ctf_field_unsigned_integer_set_value(self
._ptr
, value
)
295 utils
._handle
_ret
(ret
, "cannot set integer field object's value")
298 class _FloatingPointNumberField(_RealField
):
299 _NAME
= 'Floating point number'
301 def _value_to_float(self
, value
):
302 if not isinstance(value
, numbers
.Real
):
303 raise TypeError("expecting a real number object")
309 ret
, value
= native_bt
.ctf_field_floating_point_get_value(self
._ptr
)
318 def value(self
, value
):
319 value
= self
._value
_to
_float
(value
)
320 ret
= native_bt
.ctf_field_floating_point_set_value(self
._ptr
, value
)
321 utils
._handle
_ret
(ret
, "cannot set floating point number field object's value")
324 class _EnumerationField(_IntegerField
):
325 _NAME
= 'Enumeration'
328 def integer_field(self
):
329 int_field_ptr
= native_bt
.ctf_field_enumeration_get_container(self
._ptr
)
330 assert(int_field_ptr
)
331 return _create_from_ptr(int_field_ptr
)
335 return self
.integer_field
.value
338 def value(self
, value
):
339 self
.integer_field
.value
= value
343 iter_ptr
= native_bt
.ctf_field_enumeration_get_mappings(self
._ptr
)
345 return bt2
.field_types
._EnumerationFieldTypeMappingIterator
(iter_ptr
,
346 self
.field_type
.is_signed
)
349 @functools.total_ordering
350 class _StringField(_Field
, collections
.abc
.Sequence
):
353 def _value_to_str(self
, value
):
354 if isinstance(value
, self
.__class
__):
357 if not isinstance(value
, str):
358 raise TypeError("expecting a 'str' object")
364 value
= native_bt
.ctf_field_string_get_value(self
._ptr
)
373 def value(self
, value
):
374 value
= self
._value
_to
_str
(value
)
375 ret
= native_bt
.ctf_field_string_set_value(self
._ptr
, value
)
376 utils
._handle
_ret
(ret
, "cannot set string field object's value")
378 def __eq__(self
, other
):
380 other
= self
._value
_to
_str
(other
)
384 return self
.value
== other
386 def __le__(self
, other
):
387 return self
.value
<= self
._value
_to
_str
(other
)
389 def __lt__(self
, other
):
390 return self
.value
< self
._value
_to
_str
(other
)
393 return bool(self
.value
)
398 def __getitem__(self
, index
):
399 return self
.value
[index
]
402 return len(self
.value
)
404 def __iadd__(self
, value
):
405 value
= self
._value
_to
_str
(value
)
406 ret
= native_bt
.ctf_field_string_append(self
._ptr
, value
)
407 utils
._handle
_ret
(ret
, "cannot append to string field object's value")
411 class _ContainerField(_Field
):
413 return len(self
) != 0
416 count
= self
._count
()
420 def __delitem__(self
, index
):
421 raise NotImplementedError
424 class _StructureField(_ContainerField
, collections
.abc
.MutableMapping
):
428 return len(self
.field_type
)
430 def __getitem__(self
, key
):
431 utils
._check
_str
(key
)
432 ptr
= native_bt
.ctf_field_structure_get_field_by_name(self
._ptr
, key
)
437 return _create_from_ptr(ptr
)
439 def __setitem__(self
, key
, value
):
440 # we can only set numbers and strings
441 if not isinstance(value
, (numbers
.Number
, str)):
442 raise TypeError('expecting number object or string')
444 # raises if index is somehow invalid
447 if not isinstance(field
, (_NumericField
, _StringField
)):
448 raise TypeError('can only set the value of a number or string field')
450 # the field's property does the appropriate conversion or raises
451 # the appropriate exception
454 def at_index(self
, index
):
455 utils
._check
_uint
64(index
)
457 if index
>= len(self
):
460 field_ptr
= native_bt
.ctf_field_structure_get_field_by_index(self
._ptr
, index
)
462 return _create_from_ptr(field_ptr
)
466 return iter(self
.field_type
)
468 def __eq__(self
, other
):
469 if not isinstance(other
, collections
.abc
.Mapping
):
472 if len(self
) != len(other
):
475 for self_key
, self_value
in self
.items():
476 if self_key
not in other
:
479 other_value
= other
[self_key
]
481 if self_value
!= other_value
:
487 class _VariantField(_Field
):
492 field_ptr
= native_bt
.ctf_field_variant_get_tag(self
._ptr
)
494 if field_ptr
is None:
497 return _create_from_ptr(field_ptr
)
500 def selected_field(self
):
503 def field(self
, tag_field
=None):
504 if tag_field
is None:
505 field_ptr
= native_bt
.ctf_field_variant_get_current_field(self
._ptr
)
507 if field_ptr
is None:
510 utils
._check
_type
(tag_field
, _EnumerationField
)
511 field_ptr
= native_bt
.ctf_field_variant_get_field(self
._ptr
, tag_field
._ptr
)
512 utils
._handle
_ptr
(field_ptr
, "cannot select variant field object's field")
514 return _create_from_ptr(field_ptr
)
516 def __eq__(self
, other
):
517 if type(other
) is not type(self
):
520 if self
.addr
== other
.addr
:
523 return self
.selected_field
== other
.selected_field
526 return bool(self
.selected_field
)
529 class _ArraySequenceField(_ContainerField
, collections
.abc
.MutableSequence
):
530 def __getitem__(self
, index
):
531 if not isinstance(index
, numbers
.Integral
):
532 raise TypeError("'{}' is not an integral number object: invalid index".format(index
.__class
__.__name
__))
536 if index
< 0 or index
>= len(self
):
537 raise IndexError('{} field object index is out of range'.format(self
._NAME
))
539 field_ptr
= self
._get
_field
_ptr
_at
_index
(index
)
541 return _create_from_ptr(field_ptr
)
543 def __setitem__(self
, index
, value
):
544 # we can only set numbers and strings
545 if not isinstance(value
, (numbers
.Number
, _StringField
, str)):
546 raise TypeError('expecting number or string object')
548 # raises if index is somehow invalid
551 if not isinstance(field
, (_NumericField
, _StringField
)):
552 raise TypeError('can only set the value of a number or string field')
554 # the field's property does the appropriate conversion or raises
555 # the appropriate exception
558 def insert(self
, index
, value
):
559 raise NotImplementedError
561 def __eq__(self
, other
):
562 if not isinstance(other
, collections
.abc
.Sequence
):
565 if len(self
) != len(other
):
568 for self_field
, other_field
in zip(self
, other
):
569 if self_field
!= other_field
:
575 class _ArrayField(_ArraySequenceField
):
579 return self
.field_type
.length
581 def _get_field_ptr_at_index(self
, index
):
582 return native_bt
.ctf_field_array_get_field(self
._ptr
, index
)
585 class _SequenceField(_ArraySequenceField
):
589 return self
.length_field
.value
592 def length_field(self
):
593 field_ptr
= native_bt
.ctf_field_sequence_get_length(self
._ptr
)
594 utils
._handle
_ptr
("cannot get sequence field object's length field")
595 return _create_from_ptr(field_ptr
)
598 def length_field(self
, length_field
):
599 utils
._check
_type
(length_field
, _IntegerField
)
600 ret
= native_bt
.ctf_field_sequence_set_length(self
._ptr
, length_field
._ptr
)
601 utils
._handle
_ret
(ret
, "cannot set sequence field object's length field")
603 def _get_field_ptr_at_index(self
, index
):
604 return native_bt
.ctf_field_sequence_get_field(self
._ptr
, index
)
608 native_bt
.CTF_FIELD_TYPE_ID_INTEGER
: _IntegerField
,
609 native_bt
.CTF_FIELD_TYPE_ID_FLOAT
: _FloatingPointNumberField
,
610 native_bt
.CTF_FIELD_TYPE_ID_ENUM
: _EnumerationField
,
611 native_bt
.CTF_FIELD_TYPE_ID_STRING
: _StringField
,
612 native_bt
.CTF_FIELD_TYPE_ID_STRUCT
: _StructureField
,
613 native_bt
.CTF_FIELD_TYPE_ID_ARRAY
: _ArrayField
,
614 native_bt
.CTF_FIELD_TYPE_ID_SEQUENCE
: _SequenceField
,
615 native_bt
.CTF_FIELD_TYPE_ID_VARIANT
: _VariantField
,