This is a recurrent pattern in the Python bindings:
class Something(_SomeBase):
_set_value = native_bt.something_set_value
Then template methods in `_SomeBase` can use this class attribute by
name within the template.
This function needs to be a static method, because template methods do:
self._set_value(...)
and native_bt.something_set_value() must not receive `self` as its first
argument. For some reason, when you set a class attribute to a native
function (defined within an extension module), it is automatically a
static method, so you don't need to specify it manually. It is not the
case for any other Python function.
With SWIG 3, the generated wrapper code in `native_bt.py` looks like:
def value_unsigned_integer_set(integer_obj, val):
return _native_bt.value_unsigned_integer_set(integer_obj, val)
value_unsigned_integer_set = _native_bt.value_unsigned_integer_set
There's a defined Python function, but then the same name is reassigned
to the native function itself (`_native_bt.value_unsigned_integer_set`).
I didn't investigate why SWIG does that; it could be an optimization,
but then I don't get why the Python wrapper is created in the first
place. This means that, with:
class UnsignedIntegerValue(_IntegerValue):
...
_set_value = native_bt.value_unsigned_integer_set
...
UnsignedIntegerValue._set_value() is a static method. However, with
SWIG 4, the generated wrapper looks like this:
def value_unsigned_integer_set(integer_obj, val):
return _native_bt.value_unsigned_integer_set(integer_obj, val)
For some reason, the
value_unsigned_integer_set = _native_bt.value_unsigned_integer_set
part is gone.
Now, UnsignedIntegerValue._set_value() is not a static method anymore,
so calling it with `self._set_value()` gives the expected:
TypeError: value_unsigned_integer_set() takes 2 positional arguments
but 3 were given
Instead of trying to make SWIG 4 generate this native function
assignment again, I prefer to be more explicit/clean and use
staticmethod() to force the function into a static method.
From https://docs.python.org/3/library/functions.html#staticmethod,
the documentation also suggests this:
> Like all decorators, it is also possible to call staticmethod as a
> regular function and do something with its result. This is needed in
> some cases where you need a reference to a function from a class body
> and you want to avoid the automatic transformation to instance method.
> For these cases, use this idiom:
>
> class C:
> builtin_open = staticmethod(open)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ic51397e34132adc78fddc3105878080a8bf74ff9
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1357
Tested-by: jenkins
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
class _GenericSourceComponentClass(_GenericComponentClass):
class _GenericSourceComponentClass(_GenericComponentClass):
- _get_ref = native_bt.component_class_source_get_ref
- _put_ref = native_bt.component_class_source_put_ref
- _as_component_class_ptr = native_bt.component_class_source_as_component_class
+ _get_ref = staticmethod(native_bt.component_class_source_get_ref)
+ _put_ref = staticmethod(native_bt.component_class_source_put_ref)
+ _as_component_class_ptr = staticmethod(native_bt.component_class_source_as_component_class)
class _GenericFilterComponentClass(_GenericComponentClass):
class _GenericFilterComponentClass(_GenericComponentClass):
- _get_ref = native_bt.component_class_filter_get_ref
- _put_ref = native_bt.component_class_filter_put_ref
- _as_component_class_ptr = native_bt.component_class_filter_as_component_class
+ _get_ref = staticmethod(native_bt.component_class_filter_get_ref)
+ _put_ref = staticmethod(native_bt.component_class_filter_put_ref)
+ _as_component_class_ptr = staticmethod(native_bt.component_class_filter_as_component_class)
class _GenericSinkComponentClass(_GenericComponentClass):
class _GenericSinkComponentClass(_GenericComponentClass):
- _get_ref = native_bt.component_class_sink_get_ref
- _put_ref = native_bt.component_class_sink_put_ref
- _as_component_class_ptr = native_bt.component_class_sink_as_component_class
+ _get_ref = staticmethod(native_bt.component_class_sink_get_ref)
+ _put_ref = staticmethod(native_bt.component_class_sink_put_ref)
+ _as_component_class_ptr = staticmethod(native_bt.component_class_sink_as_component_class)
def _handle_component_status(status, gen_error_msg):
def _handle_component_status(status, gen_error_msg):
class _SourceComponent(_Component):
class _SourceComponent(_Component):
- _borrow_component_class_ptr = native_bt.component_source_borrow_class_const
+ _borrow_component_class_ptr = staticmethod(native_bt.component_source_borrow_class_const)
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
- _as_component_class_ptr = native_bt.component_class_source_as_component_class
- _as_component_ptr = native_bt.component_source_as_component_const
+ _as_component_class_ptr = staticmethod(native_bt.component_class_source_as_component_class)
+ _as_component_ptr = staticmethod(native_bt.component_source_as_component_const)
class _FilterComponent(_Component):
class _FilterComponent(_Component):
- _borrow_component_class_ptr = native_bt.component_filter_borrow_class_const
+ _borrow_component_class_ptr = staticmethod(native_bt.component_filter_borrow_class_const)
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
- _as_component_class_ptr = native_bt.component_class_filter_as_component_class
- _as_component_ptr = native_bt.component_filter_as_component_const
+ _as_component_class_ptr = staticmethod(native_bt.component_class_filter_as_component_class)
+ _as_component_ptr = staticmethod(native_bt.component_filter_as_component_const)
class _SinkComponent(_Component):
class _SinkComponent(_Component):
- _borrow_component_class_ptr = native_bt.component_sink_borrow_class_const
+ _borrow_component_class_ptr = staticmethod(native_bt.component_sink_borrow_class_const)
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
- _as_component_class_ptr = native_bt.component_class_sink_as_component_class
- _as_component_ptr = native_bt.component_sink_as_component_const
+ _as_component_class_ptr = staticmethod(native_bt.component_class_sink_as_component_class)
+ _as_component_ptr = staticmethod(native_bt.component_sink_as_component_const)
# This is analogous to _GenericSourceComponentClass, but for source
# This is analogous to _GenericSourceComponentClass, but for source
# This is analogous to _GenericSinkComponentClass, but for sink
# component objects.
class _GenericSinkComponent(object._SharedObject, _SinkComponent):
# This is analogous to _GenericSinkComponentClass, but for sink
# component objects.
class _GenericSinkComponent(object._SharedObject, _SinkComponent):
- _get_ref = native_bt.component_sink_get_ref
- _put_ref = native_bt.component_sink_put_ref
+ _get_ref = staticmethod(native_bt.component_sink_get_ref)
+ _put_ref = staticmethod(native_bt.component_sink_put_ref)
@property
def input_ports(self):
@property
def input_ports(self):
class _UserSourceComponent(_UserComponent, _SourceComponent):
class _UserSourceComponent(_UserComponent, _SourceComponent):
- _as_not_self_specific_component_ptr = native_bt.self_component_source_as_component_source
+ _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_source_as_component_source)
@property
def _output_ports(self):
@property
def _output_ports(self):
class _UserFilterComponent(_UserComponent, _FilterComponent):
class _UserFilterComponent(_UserComponent, _FilterComponent):
- _as_not_self_specific_component_ptr = native_bt.self_component_filter_as_component_filter
+ _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_filter_as_component_filter)
@property
def _output_ports(self):
@property
def _output_ports(self):
class _UserSinkComponent(_UserComponent, _SinkComponent):
class _UserSinkComponent(_UserComponent, _SinkComponent):
- _as_not_self_specific_component_ptr = native_bt.self_component_sink_as_component_sink
+ _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_sink_as_component_sink)
@property
def _input_ports(self):
@property
def _input_ports(self):
class Graph(object._SharedObject):
class Graph(object._SharedObject):
- _get_ref = native_bt.graph_get_ref
- _put_ref = native_bt.graph_put_ref
+ _get_ref = staticmethod(native_bt.graph_get_ref)
+ _put_ref = staticmethod(native_bt.graph_put_ref)
def __init__(self):
ptr = native_bt.graph_create()
def __init__(self):
ptr = native_bt.graph_create()
class _PluginSet(object._SharedObject, collections.abc.Sequence):
class _PluginSet(object._SharedObject, collections.abc.Sequence):
- _put_ref = native_bt.plugin_set_put_ref
- _get_ref = native_bt.plugin_set_get_ref
+ _put_ref = staticmethod(native_bt.plugin_set_put_ref)
+ _get_ref = staticmethod(native_bt.plugin_set_get_ref)
def __len__(self):
count = native_bt.plugin_set_get_plugin_count(self._ptr)
def __len__(self):
count = native_bt.plugin_set_get_plugin_count(self._ptr)
class _PluginSourceComponentClasses(_PluginComponentClasses):
class _PluginSourceComponentClasses(_PluginComponentClasses):
- _component_class_count = native_bt.plugin_get_source_component_class_count
- _borrow_component_class_by_name = native_bt.plugin_borrow_source_component_class_by_name_const
- _borrow_component_class_by_index = native_bt.plugin_borrow_source_component_class_by_index_const
+ _component_class_count = staticmethod(native_bt.plugin_get_source_component_class_count)
+ _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_source_component_class_by_name_const)
+ _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_source_component_class_by_index_const)
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
class _PluginFilterComponentClasses(_PluginComponentClasses):
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
class _PluginFilterComponentClasses(_PluginComponentClasses):
- _component_class_count = native_bt.plugin_get_filter_component_class_count
- _borrow_component_class_by_name = native_bt.plugin_borrow_filter_component_class_by_name_const
- _borrow_component_class_by_index = native_bt.plugin_borrow_filter_component_class_by_index_const
+ _component_class_count = staticmethod(native_bt.plugin_get_filter_component_class_count)
+ _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_filter_component_class_by_name_const)
+ _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_filter_component_class_by_index_const)
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
class _PluginSinkComponentClasses(_PluginComponentClasses):
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
class _PluginSinkComponentClasses(_PluginComponentClasses):
- _component_class_count = native_bt.plugin_get_sink_component_class_count
- _borrow_component_class_by_name = native_bt.plugin_borrow_sink_component_class_by_name_const
- _borrow_component_class_by_index = native_bt.plugin_borrow_sink_component_class_by_index_const
+ _component_class_count = staticmethod(native_bt.plugin_get_sink_component_class_count)
+ _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_sink_component_class_by_name_const)
+ _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_sink_component_class_by_index_const)
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
class _Plugin(object._SharedObject):
_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
class _Plugin(object._SharedObject):
- _put_ref = native_bt.plugin_put_ref
- _get_ref = native_bt.plugin_get_ref
+ _put_ref = staticmethod(native_bt.plugin_put_ref)
+ _get_ref = staticmethod(native_bt.plugin_get_ref)
@property
def name(self):
@property
def name(self):
class QueryExecutor(object._SharedObject):
class QueryExecutor(object._SharedObject):
- _get_ref = native_bt.query_executor_get_ref
- _put_ref = native_bt.query_executor_put_ref
+ _get_ref = staticmethod(native_bt.query_executor_get_ref)
+ _put_ref = staticmethod(native_bt.query_executor_put_ref)
def _handle_status(self, status, gen_error_msg):
if status == native_bt.QUERY_EXECUTOR_STATUS_AGAIN:
def _handle_status(self, status, gen_error_msg):
if status == native_bt.QUERY_EXECUTOR_STATUS_AGAIN:
class _Value(object._SharedObject, metaclass=abc.ABCMeta):
class _Value(object._SharedObject, metaclass=abc.ABCMeta):
- _get_ref = native_bt.value_get_ref
- _put_ref = native_bt.value_put_ref
+ _get_ref = staticmethod(native_bt.value_get_ref)
+ _put_ref = staticmethod(native_bt.value_put_ref)
def __eq__(self, other):
if other is None:
def __eq__(self, other):
if other is None:
class UnsignedIntegerValue(_IntegerValue):
_check_int_range = staticmethod(utils._check_uint64)
class UnsignedIntegerValue(_IntegerValue):
_check_int_range = staticmethod(utils._check_uint64)
- _create_default_value = native_bt.value_unsigned_integer_create
- _create_value = native_bt.value_unsigned_integer_create_init
- _set_value = native_bt.value_unsigned_integer_set
- _get_value = native_bt.value_unsigned_integer_get
+ _create_default_value = staticmethod(native_bt.value_unsigned_integer_create)
+ _create_value = staticmethod(native_bt.value_unsigned_integer_create_init)
+ _set_value = staticmethod(native_bt.value_unsigned_integer_set)
+ _get_value = staticmethod(native_bt.value_unsigned_integer_get)
class SignedIntegerValue(_IntegerValue):
_check_int_range = staticmethod(utils._check_int64)
class SignedIntegerValue(_IntegerValue):
_check_int_range = staticmethod(utils._check_int64)
- _create_default_value = native_bt.value_signed_integer_create
- _create_value = native_bt.value_signed_integer_create_init
- _set_value = native_bt.value_signed_integer_set
- _get_value = native_bt.value_signed_integer_get
+ _create_default_value = staticmethod(native_bt.value_signed_integer_create)
+ _create_value = staticmethod(native_bt.value_signed_integer_create_init)
+ _set_value = staticmethod(native_bt.value_signed_integer_set)
+ _get_value = staticmethod(native_bt.value_signed_integer_get)
class RealValue(_RealValue):
class RealValue(_RealValue):