~lttng/babeltrace/trunk

« back to all changes in this revision

Viewing changes to src/bindings/python/bt2/bt2/field_class.py

  • Committer: Philippe Proulx
  • Author(s): Simon Marchi
  • Date: 2023-09-21 17:29:44 UTC
  • Revision ID: git-v1:e5914347c8eea0f26c07348d0ac64dbe020de44a
python: standardize intra-bt2 imports

A subsequent patch wants to refer to `object` (the Python type), but it
is shadowed by our `from bt2 import object` import.  It seems like a
good time to standardize how we import intra-bt2 modules, to use the
pattern:

    from bt2 import potato as bt2_potato

Note that I didn't add the bt2_ prefix to the native_bt import, since it
would be a bit redundant.

Change-Id: Icdb4339075a1888463c4f84b292deb272ad49943
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10386
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#
3
3
# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4
4
 
5
 
from bt2 import native_bt, object, utils
 
5
from bt2 import native_bt
 
6
from bt2 import object as bt2_object
 
7
from bt2 import utils as bt2_utils
6
8
import collections.abc
7
9
from bt2 import field_path as bt2_field_path
8
10
from bt2 import integer_range_set as bt2_integer_range_set
44
46
    HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
45
47
 
46
48
 
47
 
class _FieldClassConst(object._SharedObject):
 
49
class _FieldClassConst(bt2_object._SharedObject):
48
50
    @staticmethod
49
51
    def _get_ref(ptr):
50
52
        native_bt.field_class_get_ref(ptr)
83
85
 
84
86
    def _user_attributes(self, user_attributes):
85
87
        value = bt2_value.create_value(user_attributes)
86
 
        utils._check_type(value, bt2_value.MapValue)
 
88
        bt2_utils._check_type(value, bt2_value.MapValue)
87
89
        native_bt.field_class_set_user_attributes(self._ptr, value._ptr)
88
90
 
89
91
    _user_attributes = property(fset=_user_attributes)
134
136
    _field_value_range = property(fset=_field_value_range)
135
137
 
136
138
    def _preferred_display_base(self, base):
137
 
        utils._check_uint64(base)
 
139
        bt2_utils._check_uint64(base)
138
140
 
139
141
        if base not in (
140
142
            IntegerDisplayBase.BINARY,
245
247
        self._check_int_type(value)
246
248
 
247
249
        status, labels = self._get_mapping_labels_for_value(self._ptr, value)
248
 
        utils._handle_func_status(
 
250
        bt2_utils._handle_func_status(
249
251
            status, "cannot get mapping labels for value {}".format(value)
250
252
        )
251
253
        return [self[label] for label in labels]
256
258
            yield self._mapping_pycls(mapping_ptr).label
257
259
 
258
260
    def __getitem__(self, label):
259
 
        utils._check_str(label)
 
261
        bt2_utils._check_str(label)
260
262
        mapping_ptr = self._borrow_mapping_ptr_by_label(self._ptr, label)
261
263
 
262
264
        if mapping_ptr is None:
267
269
 
268
270
class _EnumerationFieldClass(_EnumerationFieldClassConst, _IntegerFieldClass):
269
271
    def add_mapping(self, label, ranges):
270
 
        utils._check_str(label)
271
 
        utils._check_type(ranges, self._range_set_pycls)
 
272
        bt2_utils._check_str(label)
 
273
        bt2_utils._check_type(ranges, self._range_set_pycls)
272
274
 
273
275
        if label in self:
274
276
            raise ValueError("duplicate mapping label '{}'".format(label))
275
277
 
276
278
        status = self._add_mapping(self._ptr, label, ranges._ptr)
277
 
        utils._handle_func_status(
 
279
        bt2_utils._handle_func_status(
278
280
            status, "cannot add mapping to enumeration field class object"
279
281
        )
280
282
 
299
301
    _get_mapping_labels_for_value = staticmethod(
300
302
        native_bt.field_class_enumeration_unsigned_get_mapping_labels_for_value
301
303
    )
302
 
    _check_int_type = staticmethod(utils._check_uint64)
 
304
    _check_int_type = staticmethod(bt2_utils._check_uint64)
303
305
 
304
306
 
305
307
class _UnsignedEnumerationFieldClass(
326
328
    _get_mapping_labels_for_value = staticmethod(
327
329
        native_bt.field_class_enumeration_signed_get_mapping_labels_for_value
328
330
    )
329
 
    _check_int_type = staticmethod(utils._check_int64)
 
331
    _check_int_type = staticmethod(bt2_utils._check_int64)
330
332
 
331
333
 
332
334
class _SignedEnumerationFieldClass(
400
402
 
401
403
    def _user_attributes(self, user_attributes):
402
404
        value = bt2_value.create_value(user_attributes)
403
 
        utils._check_type(value, bt2_value.MapValue)
 
405
        bt2_utils._check_type(value, bt2_value.MapValue)
404
406
        native_bt.field_class_structure_member_set_user_attributes(
405
407
            self._ptr, value._ptr
406
408
        )
445
447
            yield native_bt.field_class_structure_member_get_name(member_ptr)
446
448
 
447
449
    def member_at_index(self, index):
448
 
        utils._check_uint64(index)
 
450
        bt2_utils._check_uint64(index)
449
451
 
450
452
        if index >= len(self):
451
453
            raise IndexError
466
468
    _structure_member_field_class_pycls = property(lambda _: _StructureFieldClassMember)
467
469
 
468
470
    def append_member(self, name, field_class, user_attributes=None):
469
 
        utils._check_str(name)
470
 
        utils._check_type(field_class, _FieldClass)
 
471
        bt2_utils._check_str(name)
 
472
        bt2_utils._check_type(field_class, _FieldClass)
471
473
 
472
474
        if name in self:
473
475
            raise ValueError("duplicate member name '{}'".format(name))
481
483
        status = native_bt.field_class_structure_append_member(
482
484
            self._ptr, name, field_class._ptr
483
485
        )
484
 
        utils._handle_func_status(
 
486
        bt2_utils._handle_func_status(
485
487
            status, "cannot append member to structure field class object"
486
488
        )
487
489
 
588
590
    _NAME = "Option (with boolean selector)"
589
591
 
590
592
    def _selector_is_reversed(self, selector_is_reversed):
591
 
        utils._check_bool(selector_is_reversed)
 
593
        bt2_utils._check_bool(selector_is_reversed)
592
594
        native_bt.field_class_option_with_selector_field_bool_set_selector_is_reversed(
593
595
            self._ptr, selector_is_reversed
594
596
        )
671
673
 
672
674
    def _user_attributes(self, user_attributes):
673
675
        value = bt2_value.create_value(user_attributes)
674
 
        utils._check_type(value, bt2_value.MapValue)
 
676
        bt2_utils._check_type(value, bt2_value.MapValue)
675
677
        native_bt.field_class_variant_option_set_user_attributes(self._ptr, value._ptr)
676
678
 
677
679
    _user_attributes = property(fset=_user_attributes)
776
778
            yield native_bt.field_class_variant_option_get_name(base_opt_ptr)
777
779
 
778
780
    def option_at_index(self, index):
779
 
        utils._check_uint64(index)
 
781
        bt2_utils._check_uint64(index)
780
782
 
781
783
        if index >= len(self):
782
784
            raise IndexError
807
809
    _NAME = "Variant (without selector)"
808
810
 
809
811
    def append_option(self, name, field_class, user_attributes=None):
810
 
        utils._check_str(name)
811
 
        utils._check_type(field_class, _FieldClass)
 
812
        bt2_utils._check_str(name)
 
813
        bt2_utils._check_type(field_class, _FieldClass)
812
814
 
813
815
        if name in self:
814
816
            raise ValueError("duplicate option name '{}'".format(name))
822
824
        status = native_bt.field_class_variant_without_selector_append_option(
823
825
            self._ptr, name, field_class._ptr
824
826
        )
825
 
        utils._handle_func_status(
 
827
        bt2_utils._handle_func_status(
826
828
            status, "cannot append option to variant field class object"
827
829
        )
828
830
 
857
859
    _NAME = "Variant (with selector)"
858
860
 
859
861
    def append_option(self, name, field_class, ranges, user_attributes=None):
860
 
        utils._check_str(name)
861
 
        utils._check_type(field_class, _FieldClass)
862
 
        utils._check_type(ranges, self._variant_option_pycls._range_set_pycls)
 
862
        bt2_utils._check_str(name)
 
863
        bt2_utils._check_type(field_class, _FieldClass)
 
864
        bt2_utils._check_type(ranges, self._variant_option_pycls._range_set_pycls)
863
865
 
864
866
        if name in self:
865
867
            raise ValueError("duplicate option name '{}'".format(name))
876
878
        # TODO: check overlaps (precondition of self._append_option())
877
879
 
878
880
        status = self._append_option(self._ptr, name, field_class._ptr, ranges._ptr)
879
 
        utils._handle_func_status(
 
881
        bt2_utils._handle_func_status(
880
882
            status, "cannot append option to variant field class object"
881
883
        )
882
884