| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 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 | from bt2 import native_bt, object, utils |
| 24 | import bt2.field_class |
| 25 | import collections.abc |
| 26 | import functools |
| 27 | import numbers |
| 28 | import math |
| 29 | import abc |
| 30 | import bt2 |
| 31 | |
| 32 | |
| 33 | def _get_leaf_field(obj): |
| 34 | if type(obj) is not _VariantField: |
| 35 | return obj |
| 36 | |
| 37 | return _get_leaf_field(obj.selected_field) |
| 38 | |
| 39 | |
| 40 | def _create_from_ptr(ptr): |
| 41 | # recreate the field class wrapper of this field's type (the identity |
| 42 | # could be different, but the underlying address should be the |
| 43 | # same) |
| 44 | field_class_ptr = native_bt.field_get_type(ptr) |
| 45 | utils._handle_ptr(field_class_ptr, "cannot get field object's type") |
| 46 | field_class = bt2.field_class._create_from_ptr(field_class_ptr) |
| 47 | typeid = native_bt.field_class_get_type_id(field_class._ptr) |
| 48 | field = _TYPE_ID_TO_OBJ[typeid]._create_from_ptr(ptr) |
| 49 | field._field_class = field_class |
| 50 | return field |
| 51 | |
| 52 | |
| 53 | class _Field(object._UniqueObject, metaclass=abc.ABCMeta): |
| 54 | def __copy__(self): |
| 55 | ptr = native_bt.field_copy(self._ptr) |
| 56 | utils._handle_ptr(ptr, 'cannot copy {} field object'.format(self._NAME.lower())) |
| 57 | return _create_from_ptr(ptr) |
| 58 | |
| 59 | def __deepcopy__(self, memo): |
| 60 | cpy = self.__copy__() |
| 61 | memo[id(self)] = cpy |
| 62 | return cpy |
| 63 | |
| 64 | def __eq__(self, other): |
| 65 | # special case: two unset fields with the same field class are equal |
| 66 | if isinstance(other, _Field): |
| 67 | if not self.is_set or not other.is_set: |
| 68 | if not self.is_set and not other.is_set and self.field_class == other.field_class: |
| 69 | return True |
| 70 | return False |
| 71 | |
| 72 | other = _get_leaf_field(other) |
| 73 | return self._spec_eq(other) |
| 74 | |
| 75 | @property |
| 76 | def field_class(self): |
| 77 | return self._field_class |
| 78 | |
| 79 | @property |
| 80 | def is_set(self): |
| 81 | is_set = native_bt.field_is_set(self._ptr) |
| 82 | return is_set > 0 |
| 83 | |
| 84 | def reset(self): |
| 85 | ret = native_bt.field_reset(self._ptr) |
| 86 | utils._handle_ret(ret, "cannot reset field object's value") |
| 87 | |
| 88 | def _repr(self): |
| 89 | raise NotImplementedError |
| 90 | |
| 91 | def __repr__(self): |
| 92 | return self._repr() if self.is_set else 'Unset' |
| 93 | |
| 94 | |
| 95 | @functools.total_ordering |
| 96 | class _NumericField(_Field): |
| 97 | @staticmethod |
| 98 | def _extract_value(other): |
| 99 | if other is True or other is False: |
| 100 | return other |
| 101 | |
| 102 | if isinstance(other, numbers.Integral): |
| 103 | return int(other) |
| 104 | |
| 105 | if isinstance(other, numbers.Real): |
| 106 | return float(other) |
| 107 | |
| 108 | if isinstance(other, numbers.Complex): |
| 109 | return complex(other) |
| 110 | |
| 111 | raise TypeError("'{}' object is not a number object".format(other.__class__.__name__)) |
| 112 | |
| 113 | def __int__(self): |
| 114 | return int(self._value) |
| 115 | |
| 116 | def __float__(self): |
| 117 | return float(self._value) |
| 118 | |
| 119 | def _repr(self): |
| 120 | return repr(self._value) |
| 121 | |
| 122 | def __lt__(self, other): |
| 123 | if not isinstance(other, numbers.Number): |
| 124 | raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__, |
| 125 | other.__class__.__name__)) |
| 126 | |
| 127 | return self._value < float(other) |
| 128 | |
| 129 | def __le__(self, other): |
| 130 | if not isinstance(other, numbers.Number): |
| 131 | raise TypeError('unorderable types: {}() <= {}()'.format(self.__class__.__name__, |
| 132 | other.__class__.__name__)) |
| 133 | |
| 134 | return self._value <= float(other) |
| 135 | |
| 136 | def _spec_eq(self, other): |
| 137 | if not isinstance(other, numbers.Number): |
| 138 | return False |
| 139 | |
| 140 | return self._value == complex(other) |
| 141 | |
| 142 | def __rmod__(self, other): |
| 143 | return self._extract_value(other) % self._value |
| 144 | |
| 145 | def __mod__(self, other): |
| 146 | return self._value % self._extract_value(other) |
| 147 | |
| 148 | def __rfloordiv__(self, other): |
| 149 | return self._extract_value(other) // self._value |
| 150 | |
| 151 | def __floordiv__(self, other): |
| 152 | return self._value // self._extract_value(other) |
| 153 | |
| 154 | def __round__(self, ndigits=None): |
| 155 | if ndigits is None: |
| 156 | return round(self._value) |
| 157 | else: |
| 158 | return round(self._value, ndigits) |
| 159 | |
| 160 | def __ceil__(self): |
| 161 | return math.ceil(self._value) |
| 162 | |
| 163 | def __floor__(self): |
| 164 | return math.floor(self._value) |
| 165 | |
| 166 | def __trunc__(self): |
| 167 | return int(self._value) |
| 168 | |
| 169 | def __abs__(self): |
| 170 | return abs(self._value) |
| 171 | |
| 172 | def __add__(self, other): |
| 173 | return self._value + self._extract_value(other) |
| 174 | |
| 175 | def __radd__(self, other): |
| 176 | return self.__add__(other) |
| 177 | |
| 178 | def __neg__(self): |
| 179 | return -self._value |
| 180 | |
| 181 | def __pos__(self): |
| 182 | return +self._value |
| 183 | |
| 184 | def __mul__(self, other): |
| 185 | return self._value * self._extract_value(other) |
| 186 | |
| 187 | def __rmul__(self, other): |
| 188 | return self.__mul__(other) |
| 189 | |
| 190 | def __truediv__(self, other): |
| 191 | return self._value / self._extract_value(other) |
| 192 | |
| 193 | def __rtruediv__(self, other): |
| 194 | return self._extract_value(other) / self._value |
| 195 | |
| 196 | def __pow__(self, exponent): |
| 197 | return self._value ** self._extract_value(exponent) |
| 198 | |
| 199 | def __rpow__(self, base): |
| 200 | return self._extract_value(base) ** self._value |
| 201 | |
| 202 | def __iadd__(self, other): |
| 203 | self.value = self + other |
| 204 | return self |
| 205 | |
| 206 | def __isub__(self, other): |
| 207 | self.value = self - other |
| 208 | return self |
| 209 | |
| 210 | def __imul__(self, other): |
| 211 | self.value = self * other |
| 212 | return self |
| 213 | |
| 214 | def __itruediv__(self, other): |
| 215 | self.value = self / other |
| 216 | return self |
| 217 | |
| 218 | def __ifloordiv__(self, other): |
| 219 | self.value = self // other |
| 220 | return self |
| 221 | |
| 222 | def __imod__(self, other): |
| 223 | self.value = self % other |
| 224 | return self |
| 225 | |
| 226 | def __ipow__(self, other): |
| 227 | self.value = self ** other |
| 228 | return self |
| 229 | |
| 230 | |
| 231 | class _IntegralField(_NumericField, numbers.Integral): |
| 232 | def __lshift__(self, other): |
| 233 | return self._value << self._extract_value(other) |
| 234 | |
| 235 | def __rlshift__(self, other): |
| 236 | return self._extract_value(other) << self._value |
| 237 | |
| 238 | def __rshift__(self, other): |
| 239 | return self._value >> self._extract_value(other) |
| 240 | |
| 241 | def __rrshift__(self, other): |
| 242 | return self._extract_value(other) >> self._value |
| 243 | |
| 244 | def __and__(self, other): |
| 245 | return self._value & self._extract_value(other) |
| 246 | |
| 247 | def __rand__(self, other): |
| 248 | return self._extract_value(other) & self._value |
| 249 | |
| 250 | def __xor__(self, other): |
| 251 | return self._value ^ self._extract_value(other) |
| 252 | |
| 253 | def __rxor__(self, other): |
| 254 | return self._extract_value(other) ^ self._value |
| 255 | |
| 256 | def __or__(self, other): |
| 257 | return self._value | self._extract_value(other) |
| 258 | |
| 259 | def __ror__(self, other): |
| 260 | return self._extract_value(other) | self._value |
| 261 | |
| 262 | def __invert__(self): |
| 263 | return ~self._value |
| 264 | |
| 265 | def __ilshift__(self, other): |
| 266 | self.value = self << other |
| 267 | return self |
| 268 | |
| 269 | def __irshift__(self, other): |
| 270 | self.value = self >> other |
| 271 | return self |
| 272 | |
| 273 | def __iand__(self, other): |
| 274 | self.value = self & other |
| 275 | return self |
| 276 | |
| 277 | def __ixor__(self, other): |
| 278 | self.value = self ^ other |
| 279 | return self |
| 280 | |
| 281 | def __ior__(self, other): |
| 282 | self.value = self | other |
| 283 | return self |
| 284 | |
| 285 | |
| 286 | class _RealField(_NumericField, numbers.Real): |
| 287 | pass |
| 288 | |
| 289 | |
| 290 | class _IntegerField(_IntegralField): |
| 291 | _NAME = 'Integer' |
| 292 | |
| 293 | def _value_to_int(self, value): |
| 294 | if not isinstance(value, numbers.Real): |
| 295 | raise TypeError('expecting a real number object') |
| 296 | |
| 297 | value = int(value) |
| 298 | |
| 299 | if self.field_class.is_signed: |
| 300 | utils._check_int64(value) |
| 301 | else: |
| 302 | utils._check_uint64(value) |
| 303 | |
| 304 | return value |
| 305 | |
| 306 | @property |
| 307 | def _value(self): |
| 308 | if self.field_class.is_signed: |
| 309 | ret, value = native_bt.field_signed_integer_get_value(self._ptr) |
| 310 | else: |
| 311 | ret, value = native_bt.field_unsigned_integer_get_value(self._ptr) |
| 312 | |
| 313 | if ret < 0: |
| 314 | if not self.is_set: |
| 315 | return |
| 316 | |
| 317 | utils._handle_ret(ret, "cannot get integer field's value") |
| 318 | |
| 319 | return value |
| 320 | |
| 321 | def _set_value(self, value): |
| 322 | value = self._value_to_int(value) |
| 323 | |
| 324 | if self.field_class.is_signed: |
| 325 | ret = native_bt.field_signed_integer_set_value(self._ptr, value) |
| 326 | else: |
| 327 | ret = native_bt.field_unsigned_integer_set_value(self._ptr, value) |
| 328 | |
| 329 | utils._handle_ret(ret, "cannot set integer field object's value") |
| 330 | |
| 331 | value = property(fset=_set_value) |
| 332 | |
| 333 | |
| 334 | class _FloatingPointNumberField(_RealField): |
| 335 | _NAME = 'Floating point number' |
| 336 | |
| 337 | def _value_to_float(self, value): |
| 338 | if not isinstance(value, numbers.Real): |
| 339 | raise TypeError("expecting a real number object") |
| 340 | |
| 341 | return float(value) |
| 342 | |
| 343 | @property |
| 344 | def _value(self): |
| 345 | ret, value = native_bt.field_floating_point_get_value(self._ptr) |
| 346 | |
| 347 | if ret < 0: |
| 348 | if not self.is_set: |
| 349 | return |
| 350 | |
| 351 | utils._handle_ret(ret, "cannot get floating point number field's value") |
| 352 | |
| 353 | return value |
| 354 | |
| 355 | def _set_value(self, value): |
| 356 | value = self._value_to_float(value) |
| 357 | ret = native_bt.field_floating_point_set_value(self._ptr, value) |
| 358 | utils._handle_ret(ret, "cannot set floating point number field object's value") |
| 359 | |
| 360 | value = property(fset=_set_value) |
| 361 | |
| 362 | |
| 363 | class _EnumerationField(_IntegerField): |
| 364 | _NAME = 'Enumeration' |
| 365 | |
| 366 | @property |
| 367 | def integer_field(self): |
| 368 | int_field_ptr = native_bt.field_enumeration_get_container(self._ptr) |
| 369 | assert(int_field_ptr) |
| 370 | return _create_from_ptr(int_field_ptr) |
| 371 | |
| 372 | def _set_value(self, value): |
| 373 | self.integer_field.value = value |
| 374 | |
| 375 | def _repr(self): |
| 376 | labels = [repr(v.name) for v in self.mappings] |
| 377 | return '{} ({})'.format(self._value, ', '.join(labels)) |
| 378 | |
| 379 | value = property(fset=_set_value) |
| 380 | |
| 381 | @property |
| 382 | def _value(self): |
| 383 | return self.integer_field._value |
| 384 | |
| 385 | @property |
| 386 | def mappings(self): |
| 387 | iter_ptr = native_bt.field_enumeration_get_mappings(self._ptr) |
| 388 | assert(iter_ptr) |
| 389 | return bt2.field_class._EnumerationFieldClassMappingIterator(iter_ptr, |
| 390 | self.field_class.is_signed) |
| 391 | |
| 392 | |
| 393 | @functools.total_ordering |
| 394 | class _StringField(_Field, collections.abc.Sequence): |
| 395 | _NAME = 'String' |
| 396 | |
| 397 | def _value_to_str(self, value): |
| 398 | if isinstance(value, self.__class__): |
| 399 | value = value._value |
| 400 | |
| 401 | if not isinstance(value, str): |
| 402 | raise TypeError("expecting a 'str' object") |
| 403 | |
| 404 | return value |
| 405 | |
| 406 | @property |
| 407 | def _value(self): |
| 408 | value = native_bt.field_string_get_value(self._ptr) |
| 409 | return value |
| 410 | |
| 411 | def _set_value(self, value): |
| 412 | value = self._value_to_str(value) |
| 413 | ret = native_bt.field_string_set_value(self._ptr, value) |
| 414 | utils._handle_ret(ret, "cannot set string field object's value") |
| 415 | |
| 416 | value = property(fset=_set_value) |
| 417 | |
| 418 | def _spec_eq(self, other): |
| 419 | try: |
| 420 | other = self._value_to_str(other) |
| 421 | except: |
| 422 | return False |
| 423 | |
| 424 | return self._value == other |
| 425 | |
| 426 | def __le__(self, other): |
| 427 | return self._value <= self._value_to_str(other) |
| 428 | |
| 429 | def __lt__(self, other): |
| 430 | return self._value < self._value_to_str(other) |
| 431 | |
| 432 | def __bool__(self): |
| 433 | return bool(self._value) |
| 434 | |
| 435 | def _repr(self): |
| 436 | return repr(self._value) |
| 437 | |
| 438 | def __str__(self): |
| 439 | return self._value if self.is_set else repr(self) |
| 440 | |
| 441 | def __getitem__(self, index): |
| 442 | return self._value[index] |
| 443 | |
| 444 | def __len__(self): |
| 445 | return len(self._value) |
| 446 | |
| 447 | def __iadd__(self, value): |
| 448 | value = self._value_to_str(value) |
| 449 | ret = native_bt.field_string_append(self._ptr, value) |
| 450 | utils._handle_ret(ret, "cannot append to string field object's value") |
| 451 | return self |
| 452 | |
| 453 | |
| 454 | class _ContainerField(_Field): |
| 455 | def __bool__(self): |
| 456 | return len(self) != 0 |
| 457 | |
| 458 | def __len__(self): |
| 459 | count = self._count() |
| 460 | assert(count >= 0) |
| 461 | return count |
| 462 | |
| 463 | def __delitem__(self, index): |
| 464 | raise NotImplementedError |
| 465 | |
| 466 | |
| 467 | class _StructureField(_ContainerField, collections.abc.MutableMapping): |
| 468 | _NAME = 'Structure' |
| 469 | |
| 470 | def _count(self): |
| 471 | return len(self.field_class) |
| 472 | |
| 473 | def __getitem__(self, key): |
| 474 | utils._check_str(key) |
| 475 | ptr = native_bt.field_structure_get_field_by_name(self._ptr, key) |
| 476 | |
| 477 | if ptr is None: |
| 478 | raise KeyError(key) |
| 479 | |
| 480 | return _create_from_ptr(ptr) |
| 481 | |
| 482 | def __setitem__(self, key, value): |
| 483 | # raises if key is somehow invalid |
| 484 | field = self[key] |
| 485 | |
| 486 | # the field's property does the appropriate conversion or raises |
| 487 | # the appropriate exception |
| 488 | field.value = value |
| 489 | |
| 490 | def at_index(self, index): |
| 491 | utils._check_uint64(index) |
| 492 | |
| 493 | if index >= len(self): |
| 494 | raise IndexError |
| 495 | |
| 496 | field_ptr = native_bt.field_structure_get_field_by_index(self._ptr, index) |
| 497 | assert(field_ptr) |
| 498 | return _create_from_ptr(field_ptr) |
| 499 | |
| 500 | def __iter__(self): |
| 501 | # same name iterator |
| 502 | return iter(self.field_class) |
| 503 | |
| 504 | def _spec_eq(self, other): |
| 505 | try: |
| 506 | if len(self) != len(other): |
| 507 | return False |
| 508 | |
| 509 | for self_key, self_value in self.items(): |
| 510 | if self_key not in other: |
| 511 | return False |
| 512 | |
| 513 | other_value = other[self_key] |
| 514 | |
| 515 | if self_value != other_value: |
| 516 | return False |
| 517 | |
| 518 | return True |
| 519 | except: |
| 520 | return False |
| 521 | |
| 522 | @property |
| 523 | def _value(self): |
| 524 | return {key: value._value for key, value in self.items()} |
| 525 | |
| 526 | def _set_value(self, values): |
| 527 | original_values = self._value |
| 528 | |
| 529 | try: |
| 530 | for key, value in values.items(): |
| 531 | self[key].value = value |
| 532 | except: |
| 533 | self.value = original_values |
| 534 | raise |
| 535 | |
| 536 | value = property(fset=_set_value) |
| 537 | |
| 538 | def _repr(self): |
| 539 | items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()] |
| 540 | return '{{{}}}'.format(', '.join(items)) |
| 541 | |
| 542 | |
| 543 | class _VariantField(_Field): |
| 544 | _NAME = 'Variant' |
| 545 | |
| 546 | @property |
| 547 | def tag_field(self): |
| 548 | field_ptr = native_bt.field_variant_get_tag(self._ptr) |
| 549 | |
| 550 | if field_ptr is None: |
| 551 | return |
| 552 | |
| 553 | return _create_from_ptr(field_ptr) |
| 554 | |
| 555 | @property |
| 556 | def selected_field(self): |
| 557 | return self.field() |
| 558 | |
| 559 | def field(self, tag_field=None): |
| 560 | if tag_field is None: |
| 561 | field_ptr = native_bt.field_variant_get_current_field(self._ptr) |
| 562 | |
| 563 | if field_ptr is None: |
| 564 | return |
| 565 | else: |
| 566 | utils._check_type(tag_field, _EnumerationField) |
| 567 | field_ptr = native_bt.field_variant_get_field(self._ptr, tag_field._ptr) |
| 568 | utils._handle_ptr(field_ptr, "cannot select variant field object's field") |
| 569 | |
| 570 | return _create_from_ptr(field_ptr) |
| 571 | |
| 572 | def _spec_eq(self, other): |
| 573 | return _get_leaf_field(self) == other |
| 574 | |
| 575 | def __bool__(self): |
| 576 | return bool(self.selected_field) |
| 577 | |
| 578 | def __str__(self): |
| 579 | return str(self.selected_field) if self.is_set else repr(self) |
| 580 | |
| 581 | def _repr(self): |
| 582 | return repr(self.selected_field) |
| 583 | |
| 584 | @property |
| 585 | def _value(self): |
| 586 | if self.selected_field is not None: |
| 587 | return self.selected_field._value |
| 588 | |
| 589 | def _set_value(self, value): |
| 590 | self.selected_field.value = value |
| 591 | |
| 592 | value = property(fset=_set_value) |
| 593 | |
| 594 | |
| 595 | class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence): |
| 596 | def __getitem__(self, index): |
| 597 | if not isinstance(index, numbers.Integral): |
| 598 | raise TypeError("'{}' is not an integral number object: invalid index".format(index.__class__.__name__)) |
| 599 | |
| 600 | index = int(index) |
| 601 | |
| 602 | if index < 0 or index >= len(self): |
| 603 | raise IndexError('{} field object index is out of range'.format(self._NAME)) |
| 604 | |
| 605 | field_ptr = self._get_field_ptr_at_index(index) |
| 606 | assert(field_ptr) |
| 607 | return _create_from_ptr(field_ptr) |
| 608 | |
| 609 | def __setitem__(self, index, value): |
| 610 | # we can only set numbers and strings |
| 611 | if not isinstance(value, (numbers.Number, _StringField, str)): |
| 612 | raise TypeError('expecting number or string object') |
| 613 | |
| 614 | # raises if index is somehow invalid |
| 615 | field = self[index] |
| 616 | |
| 617 | if not isinstance(field, (_NumericField, _StringField)): |
| 618 | raise TypeError('can only set the value of a number or string field') |
| 619 | |
| 620 | # the field's property does the appropriate conversion or raises |
| 621 | # the appropriate exception |
| 622 | field.value = value |
| 623 | |
| 624 | def insert(self, index, value): |
| 625 | raise NotImplementedError |
| 626 | |
| 627 | def _spec_eq(self, other): |
| 628 | try: |
| 629 | if len(self) != len(other): |
| 630 | return False |
| 631 | |
| 632 | for self_field, other_field in zip(self, other): |
| 633 | if self_field != other_field: |
| 634 | return False |
| 635 | |
| 636 | return True |
| 637 | except: |
| 638 | return False |
| 639 | |
| 640 | @property |
| 641 | def _value(self): |
| 642 | return [field._value for field in self] |
| 643 | |
| 644 | def _repr(self): |
| 645 | return '[{}]'.format(', '.join([repr(v) for v in self])) |
| 646 | |
| 647 | |
| 648 | class _ArrayField(_ArraySequenceField): |
| 649 | _NAME = 'Array' |
| 650 | |
| 651 | def _count(self): |
| 652 | return self.field_class.length |
| 653 | |
| 654 | def _get_field_ptr_at_index(self, index): |
| 655 | return native_bt.field_array_get_field(self._ptr, index) |
| 656 | |
| 657 | def _set_value(self, values): |
| 658 | if len(self) != len(values): |
| 659 | raise ValueError( |
| 660 | 'expected length of value and array field to match') |
| 661 | |
| 662 | original_values = self._value |
| 663 | try: |
| 664 | for index, value in enumerate(values): |
| 665 | if value is not None: |
| 666 | self[index].value = value |
| 667 | else: |
| 668 | self[index].reset() |
| 669 | except: |
| 670 | self.value = original_values |
| 671 | raise |
| 672 | |
| 673 | value = property(fset=_set_value) |
| 674 | |
| 675 | |
| 676 | class _SequenceField(_ArraySequenceField): |
| 677 | _NAME = 'Sequence' |
| 678 | |
| 679 | def _count(self): |
| 680 | return int(self.length_field) |
| 681 | |
| 682 | @property |
| 683 | def length_field(self): |
| 684 | field_ptr = native_bt.field_sequence_get_length(self._ptr) |
| 685 | if field_ptr is None: |
| 686 | return |
| 687 | return _create_from_ptr(field_ptr) |
| 688 | |
| 689 | @length_field.setter |
| 690 | def length_field(self, length_field): |
| 691 | utils._check_type(length_field, _IntegerField) |
| 692 | ret = native_bt.field_sequence_set_length(self._ptr, length_field._ptr) |
| 693 | utils._handle_ret(ret, "cannot set sequence field object's length field") |
| 694 | |
| 695 | def _get_field_ptr_at_index(self, index): |
| 696 | return native_bt.field_sequence_get_field(self._ptr, index) |
| 697 | |
| 698 | def _set_value(self, values): |
| 699 | original_length_field = self.length_field |
| 700 | if original_length_field is not None: |
| 701 | original_values = self._value |
| 702 | |
| 703 | if len(values) != self.length_field: |
| 704 | if self.length_field is not None: |
| 705 | length_fc = self.length_field.field_class |
| 706 | else: |
| 707 | length_fc = bt2.IntegerFieldClass(size=64, is_signed=False) |
| 708 | self.length_field = length_fc(len(values)) |
| 709 | |
| 710 | try: |
| 711 | for index, value in enumerate(values): |
| 712 | if value is not None: |
| 713 | self[index].value = value |
| 714 | else: |
| 715 | self[index].reset() |
| 716 | except: |
| 717 | if original_length_field is not None: |
| 718 | self.length_field = original_length_field |
| 719 | self.value = original_values |
| 720 | else: |
| 721 | self.reset() |
| 722 | raise |
| 723 | |
| 724 | value = property(fset=_set_value) |
| 725 | |
| 726 | |
| 727 | _TYPE_ID_TO_OBJ = { |
| 728 | } |