X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbindings%2Fpython%2Fbt2%2Fbt2%2Ftrace_class.py;h=f6efe71c2135ac224cbfa8851222bc1dfc97c0cf;hb=c345b07873b0cb0ed344bde32a322a1b1edf60ae;hp=d510a36e1e2fbdddd96e94a5cbfb4f59847d4a52;hpb=fe4df857056b4a03898f1031f136359ce733b0f5;p=babeltrace.git diff --git a/src/bindings/python/bt2/bt2/trace_class.py b/src/bindings/python/bt2/bt2/trace_class.py index d510a36e..f6efe71c 100644 --- a/src/bindings/python/bt2/bt2/trace_class.py +++ b/src/bindings/python/bt2/bt2/trace_class.py @@ -1,45 +1,39 @@ -# The MIT License (MIT) +# SPDX-License-Identifier: MIT # # Copyright (c) 2017 Philippe Proulx # Copyright (c) 2018 Francis Deslauriers # Copyright (c) 2019 Simon Marchi -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. - -from bt2 import native_bt, utils, object + +from bt2 import native_bt +from bt2 import utils as bt2_utils +from bt2 import object as bt2_object from bt2 import stream_class as bt2_stream_class from bt2 import field_class as bt2_field_class +from bt2 import integer_range_set as bt2_integer_range_set from bt2 import trace as bt2_trace +from bt2 import error as bt2_error from bt2 import value as bt2_value import collections.abc import functools -import bt2 -def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr): +def _trace_class_destruction_listener_from_native( + user_listener, handle, trace_class_ptr +): trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) user_listener(trace_class) + handle._invalidate() + +class _TraceClassConst(bt2_object._SharedObject, collections.abc.Mapping): + @staticmethod + def _get_ref(ptr): + native_bt.trace_class_get_ref(ptr) + + @staticmethod + def _put_ref(ptr): + native_bt.trace_class_put_ref(ptr) -class _TraceClassConst(object._SharedObject, collections.abc.Mapping): - _get_ref = staticmethod(native_bt.trace_class_get_ref) - _put_ref = staticmethod(native_bt.trace_class_put_ref) _borrow_stream_class_ptr_by_index = staticmethod( native_bt.trace_class_borrow_stream_class_by_index_const ) @@ -70,7 +64,7 @@ class _TraceClassConst(object._SharedObject, collections.abc.Mapping): # Get a stream class by stream id. def __getitem__(self, key): - utils._check_uint64(key) + bt2_utils._check_uint64(key) sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key) if sc_ptr is None: @@ -95,40 +89,43 @@ class _TraceClassConst(object._SharedObject, collections.abc.Mapping): # Add a listener to be called when the trace class is destroyed. def add_destruction_listener(self, listener): - if not callable(listener): raise TypeError("'listener' parameter is not callable") - fn = native_bt.bt2_trace_class_add_destruction_listener + handle = bt2_utils._ListenerHandle(self.addr) + listener_from_native = functools.partial( - _trace_class_destruction_listener_from_native, listener + _trace_class_destruction_listener_from_native, listener, handle ) + fn = native_bt.bt2_trace_class_add_destruction_listener status, listener_id = fn(self._ptr, listener_from_native) - utils._handle_func_status( - status, 'cannot add destruction listener to trace class object' + bt2_utils._handle_func_status( + status, "cannot add destruction listener to trace class object" ) - return utils._ListenerHandle(listener_id, self) + handle._set_listener_id(listener_id) + + return handle def remove_destruction_listener(self, listener_handle): - utils._check_type(listener_handle, utils._ListenerHandle) + bt2_utils._check_type(listener_handle, bt2_utils._ListenerHandle) - if listener_handle._obj.addr != self.addr: + if listener_handle._addr != self.addr: raise ValueError( - 'This trace class destruction listener does not match the trace object.' + "This trace class destruction listener does not match the trace class object." ) if listener_handle._listener_id is None: raise ValueError( - 'This trace class destruction listener was already removed.' + "This trace class destruction listener was already removed." ) status = native_bt.trace_class_remove_destruction_listener( self._ptr, listener_handle._listener_id ) - utils._handle_func_status(status) - listener_handle._listener_id = None + bt2_utils._handle_func_status(status) + listener_handle._invalidate() class _TraceClass(_TraceClassConst): @@ -152,7 +149,7 @@ class _TraceClass(_TraceClassConst): trace_ptr = native_bt.trace_create(self._ptr) if trace_ptr is None: - raise bt2._MemoryError('cannot create trace class object') + raise bt2_error._MemoryError("cannot create trace class object") trace = bt2_trace._Trace._create_from_ptr(trace_ptr) @@ -189,21 +186,38 @@ class _TraceClass(_TraceClassConst): supports_discarded_packets=False, discarded_packets_have_default_clock_snapshots=False, ): + # Validate parameters before we create the object. + bt2_stream_class._StreamClass._validate_create_params( + name, + user_attributes, + packet_context_field_class, + event_common_context_field_class, + default_clock_class, + assigns_automatic_event_class_id, + assigns_automatic_stream_id, + supports_packets, + packets_have_beginning_default_clock_snapshot, + packets_have_end_default_clock_snapshot, + supports_discarded_events, + discarded_events_have_default_clock_snapshots, + supports_discarded_packets, + discarded_packets_have_default_clock_snapshots, + ) if self.assigns_automatic_stream_class_id: if id is not None: raise ValueError( - 'id provided, but trace class assigns automatic stream class ids' + "id provided, but trace class assigns automatic stream class ids" ) sc_ptr = native_bt.stream_class_create(self._ptr) else: if id is None: raise ValueError( - 'id not provided, but trace class does not assign automatic stream class ids' + "id not provided, but trace class does not assign automatic stream class ids" ) - utils._check_uint64(id) + bt2_utils._check_uint64(id) sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr) @@ -248,13 +262,13 @@ class _TraceClass(_TraceClassConst): def _user_attributes(self, user_attributes): value = bt2_value.create_value(user_attributes) - utils._check_type(value, bt2_value.MapValue) + bt2_utils._check_type(value, bt2_value.MapValue) native_bt.trace_class_set_user_attributes(self._ptr, value._ptr) _user_attributes = property(fset=_user_attributes) def _assigns_automatic_stream_class_id(self, auto_id): - utils._check_bool(auto_id) + bt2_utils._check_bool(auto_id) return native_bt.trace_class_set_assigns_automatic_stream_class_id( self._ptr, auto_id ) @@ -267,7 +281,9 @@ class _TraceClass(_TraceClassConst): def _check_field_class_create_status(self, ptr, type_name): if ptr is None: - raise bt2._MemoryError('cannot create {} field class'.format(type_name)) + raise bt2_error._MemoryError( + "cannot create {} field class".format(type_name) + ) @staticmethod def _set_field_class_user_attrs(fc, user_attributes): @@ -276,23 +292,23 @@ class _TraceClass(_TraceClassConst): def create_bool_field_class(self, user_attributes=None): field_class_ptr = native_bt.field_class_bool_create(self._ptr) - self._check_field_class_create_status(field_class_ptr, 'boolean') + self._check_field_class_create_status(field_class_ptr, "boolean") fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc def create_bit_array_field_class(self, length, user_attributes=None): - utils._check_uint64(length) + bt2_utils._check_uint64(length) if length < 1 or length > 64: raise ValueError( - 'invalid length {}: expecting a value in the [1, 64] range'.format( + "invalid length {}: expecting a value in the [1, 64] range".format( length ) ) field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length) - self._check_field_class_create_status(field_class_ptr, 'bit array') + self._check_field_class_create_status(field_class_ptr, "bit array") fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc @@ -326,7 +342,7 @@ class _TraceClass(_TraceClassConst): return self._create_integer_field_class( native_bt.field_class_integer_signed_create, bt2_field_class._SignedIntegerFieldClass, - 'signed integer', + "signed integer", field_value_range, preferred_display_base, user_attributes, @@ -338,7 +354,7 @@ class _TraceClass(_TraceClassConst): return self._create_integer_field_class( native_bt.field_class_integer_unsigned_create, bt2_field_class._UnsignedIntegerFieldClass, - 'unsigned integer', + "unsigned integer", field_value_range, preferred_display_base, user_attributes, @@ -350,7 +366,7 @@ class _TraceClass(_TraceClassConst): return self._create_integer_field_class( native_bt.field_class_enumeration_signed_create, bt2_field_class._SignedEnumerationFieldClass, - 'signed enumeration', + "signed enumeration", field_value_range, preferred_display_base, user_attributes, @@ -362,7 +378,7 @@ class _TraceClass(_TraceClassConst): return self._create_integer_field_class( native_bt.field_class_enumeration_unsigned_create, bt2_field_class._UnsignedEnumerationFieldClass, - 'unsigned enumeration', + "unsigned enumeration", field_value_range, preferred_display_base, user_attributes, @@ -370,7 +386,7 @@ class _TraceClass(_TraceClassConst): def create_single_precision_real_field_class(self, user_attributes=None): field_class_ptr = native_bt.field_class_real_single_precision_create(self._ptr) - self._check_field_class_create_status(field_class_ptr, 'single-precision real') + self._check_field_class_create_status(field_class_ptr, "single-precision real") field_class = bt2_field_class._SinglePrecisionRealFieldClass._create_from_ptr( field_class_ptr @@ -382,7 +398,7 @@ class _TraceClass(_TraceClassConst): def create_double_precision_real_field_class(self, user_attributes=None): field_class_ptr = native_bt.field_class_real_double_precision_create(self._ptr) - self._check_field_class_create_status(field_class_ptr, 'double-precision real') + self._check_field_class_create_status(field_class_ptr, "double-precision real") field_class = bt2_field_class._DoublePrecisionRealFieldClass._create_from_ptr( field_class_ptr @@ -394,61 +410,96 @@ class _TraceClass(_TraceClassConst): def create_structure_field_class(self, user_attributes=None): field_class_ptr = native_bt.field_class_structure_create(self._ptr) - self._check_field_class_create_status(field_class_ptr, 'structure') + self._check_field_class_create_status(field_class_ptr, "structure") fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc def create_string_field_class(self, user_attributes=None): field_class_ptr = native_bt.field_class_string_create(self._ptr) - self._check_field_class_create_status(field_class_ptr, 'string') + self._check_field_class_create_status(field_class_ptr, "string") fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc def create_static_array_field_class(self, elem_fc, length, user_attributes=None): - utils._check_type(elem_fc, bt2_field_class._FieldClass) - utils._check_uint64(length) + bt2_utils._check_type(elem_fc, bt2_field_class._FieldClass) + bt2_utils._check_uint64(length) ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length) - self._check_field_class_create_status(ptr, 'static array') - fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr) + self._check_field_class_create_status(ptr, "static array") + fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr(ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc def create_dynamic_array_field_class( self, elem_fc, length_fc=None, user_attributes=None ): - utils._check_type(elem_fc, bt2_field_class._FieldClass) + bt2_utils._check_type(elem_fc, bt2_field_class._FieldClass) length_fc_ptr = None if length_fc is not None: - utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass) + bt2_utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass) length_fc_ptr = length_fc._ptr ptr = native_bt.field_class_array_dynamic_create( self._ptr, elem_fc._ptr, length_fc_ptr ) - self._check_field_class_create_status(ptr, 'dynamic array') - fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr) + self._check_field_class_create_status(ptr, "dynamic array") + fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc - def create_option_field_class( - self, content_fc, selector_fc=None, user_attributes=None + def create_option_without_selector_field_class( + self, content_fc, user_attributes=None ): - utils._check_type(content_fc, bt2_field_class._FieldClass) + bt2_utils._check_type(content_fc, bt2_field_class._FieldClass) + ptr = native_bt.field_class_option_without_selector_create( + self._ptr, content_fc._ptr + ) + self._check_field_class_create_status(ptr, "option") + fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr) + self._set_field_class_user_attrs(fc, user_attributes) + return fc - selector_fc_ptr = None + def create_option_with_bool_selector_field_class( + self, content_fc, selector_fc, selector_is_reversed=False, user_attributes=None + ): + bt2_utils._check_type(content_fc, bt2_field_class._FieldClass) + bt2_utils._check_bool(selector_is_reversed) + bt2_utils._check_type(selector_fc, bt2_field_class._BoolFieldClass) + ptr = native_bt.field_class_option_with_selector_field_bool_create( + self._ptr, content_fc._ptr, selector_fc._ptr + ) + self._check_field_class_create_status(ptr, "option") + fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr) + self._set_field_class_user_attrs(fc, user_attributes) + fc._selector_is_reversed = selector_is_reversed + return fc - if selector_fc is not None: - utils._check_type(selector_fc, bt2_field_class._BoolFieldClass) - selector_fc_ptr = selector_fc._ptr + def create_option_with_integer_selector_field_class( + self, content_fc, selector_fc, ranges, user_attributes=None + ): + bt2_utils._check_type(content_fc, bt2_field_class._FieldClass) + bt2_utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass) - ptr = native_bt.field_class_option_create( - self._ptr, content_fc._ptr, selector_fc_ptr - ) - self._check_field_class_create_status(ptr, 'option') - fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr) + if len(ranges) == 0: + raise ValueError("integer range set is empty") + + if isinstance(selector_fc, bt2_field_class._UnsignedIntegerFieldClass): + bt2_utils._check_type(ranges, bt2_integer_range_set.UnsignedIntegerRangeSet) + ptr = native_bt.field_class_option_with_selector_field_integer_unsigned_create( + self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr + ) + else: + bt2_utils._check_type(ranges, bt2_integer_range_set.SignedIntegerRangeSet) + ptr = ( + native_bt.field_class_option_with_selector_field_integer_signed_create( + self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr + ) + ) + + self._check_field_class_create_status(ptr, "option") + fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc @@ -456,11 +507,11 @@ class _TraceClass(_TraceClassConst): selector_fc_ptr = None if selector_fc is not None: - utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass) + bt2_utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass) selector_fc_ptr = selector_fc._ptr ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr) - self._check_field_class_create_status(ptr, 'variant') - fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr) + self._check_field_class_create_status(ptr, "variant") + fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr) self._set_field_class_user_attrs(fc, user_attributes) return fc