From e34fb94a005b92ad9f57bb0fd5a0f71abe31975f Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 1 May 2019 14:09:13 -0400 Subject: [PATCH] bt2: update bindings to make test_component pass This patch updates component.py such that the test_component test passes. With the API, it is no longer possible to obtain the graph from a component, so things related to this are removed from both files. Change-Id: I3ab0e6aaaa8ec1ad7378abb78072e6e130e1c2a4 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/1085 Tested-by: jenkins Reviewed-by: Philippe Proulx --- bindings/python/bt2/bt2/component.py | 56 ++++++++++++--------- tests/bindings/python/bt2/test_component.py | 31 ++---------- 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/bindings/python/bt2/bt2/component.py b/bindings/python/bt2/bt2/component.py index 459ae955..8e07da32 100644 --- a/bindings/python/bt2/bt2/component.py +++ b/bindings/python/bt2/bt2/component.py @@ -184,20 +184,17 @@ class _ComponentPorts(collections.abc.Mapping): # passed specialized component pointer (e.g. 'bt_component_sink *'). # - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* # constants. +# - _as_component_ptr: static method, must return the passed specialized +# component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'. class _Component: @property def name(self): - name = native_bt.component_get_name(self._ptr) - assert(name is not None) + ptr = self._as_component_ptr(self._ptr) + name = native_bt.component_get_name(ptr) + assert name is not None return name - @property - def graph(self): - ptr = native_bt.component_get_graph(self._ptr) - assert(ptr) - return bt2.Graph._create_from_ptr(ptr) - @property def component_class(self): cc_ptr = self._borrow_component_class_ptr(self._ptr) @@ -215,18 +212,21 @@ class _SourceComponent(_Component): _borrow_component_class_ptr = native_bt.component_source_borrow_class_const _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 class _FilterComponent(_Component): _borrow_component_class_ptr = native_bt.component_filter_borrow_class_const _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 class _SinkComponent(_Component): _borrow_component_class_ptr = native_bt.component_sink_borrow_class_const _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 # This is analogous to _GenericSourceComponentClass, but for source @@ -548,31 +548,31 @@ class _UserComponentType(type): cc_ptr = cls._as_component_class_ptr(cls._cc_ptr) native_bt.component_class_put_ref(cc_ptr) +# Subclasses must provide these methods or property: +# +# - _as_not_self_specific_component_ptr: static method, must return the passed +# specialized self component pointer (e.g. 'bt_self_component_sink *') as a +# specialized non-self pointer (e.g. 'bt_component_sink *'). +# - _borrow_component_class_ptr: static method, must return a pointer to the +# specialized component class (e.g. 'bt_component_class_sink *') of the +# passed specialized component pointer (e.g. 'bt_component_sink *'). +# - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* +# constants. class _UserComponent(metaclass=_UserComponentType): @property def name(self): - pub_ptr = native_bt.component_from_private(self._ptr) - name = native_bt.component_get_name(pub_ptr) - native_bt.put(pub_ptr) - assert(name is not None) + ptr = self._as_not_self_specific_component_ptr(self._ptr) + ptr = self._as_component_ptr(ptr) + name = native_bt.component_get_name(ptr) + assert name is not None return name - @property - def graph(self): - pub_ptr = native_bt.component_from_private(self._ptr) - ptr = native_bt.component_get_graph(pub_ptr) - native_bt.put(pub_ptr) - assert(ptr) - return bt2.Graph._create_from_ptr(ptr) - @property def component_class(self): - pub_ptr = native_bt.component_from_private(self._ptr) - cc_ptr = native_bt.component_get_class(pub_ptr) - native_bt.put(pub_ptr) - assert(cc_ptr) - return _create_generic_component_class_from_ptr(cc_ptr) + comp_ptr = self._as_not_self_specific_component_ptr(self._ptr) + cc_ptr = self._borrow_component_class_ptr(comp_ptr) + return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type) @property def addr(self): @@ -629,6 +629,8 @@ class _UserComponent(metaclass=_UserComponentType): class _UserSourceComponent(_UserComponent, _SourceComponent): + _as_not_self_specific_component_ptr = native_bt.self_component_source_as_component_source + @property def _output_ports(self): return _ComponentPorts(True, self, @@ -647,6 +649,8 @@ class _UserSourceComponent(_UserComponent, _SourceComponent): class _UserFilterComponent(_UserComponent, _FilterComponent): + _as_not_self_specific_component_ptr = native_bt.self_component_filter_as_component_filter + @property def _output_ports(self): return _ComponentPorts(True, self, @@ -681,6 +685,8 @@ class _UserFilterComponent(_UserComponent, _FilterComponent): class _UserSinkComponent(_UserComponent, _SinkComponent): + _as_not_self_specific_component_ptr = native_bt.self_component_sink_as_component_sink + @property def _input_ports(self): return _ComponentPorts(True, self, diff --git a/tests/bindings/python/bt2/test_component.py b/tests/bindings/python/bt2/test_component.py index 77d346b6..a0e05b91 100644 --- a/tests/bindings/python/bt2/test_component.py +++ b/tests/bindings/python/bt2/test_component.py @@ -4,7 +4,6 @@ import copy import bt2 -@unittest.skip("this is broken") class UserComponentTestCase(unittest.TestCase): @staticmethod def _create_comp(comp_cls, name=None): @@ -13,7 +12,7 @@ class UserComponentTestCase(unittest.TestCase): if name is None: name = 'comp' - return graph.add_component(comp_cls, name) + return graph.add_sink_component(comp_cls, name) def test_name(self): class MySink(bt2._UserSinkComponent): @@ -25,19 +24,6 @@ class UserComponentTestCase(unittest.TestCase): comp = self._create_comp(MySink, 'yaes') - def test_graph(self): - class MySink(bt2._UserSinkComponent): - def __init__(comp_self, params): - nonlocal graph - self.assertEqual(comp_self.graph, graph) - - def _consume(self): - pass - - graph = bt2.Graph() - comp = graph.add_component(MySink, 'lel') - del graph - def test_class(self): class MySink(bt2._UserSinkComponent): def __init__(comp_self, params): @@ -71,13 +57,13 @@ class UserComponentTestCase(unittest.TestCase): finalized = True graph = bt2.Graph() - comp = graph.add_component(MySink, 'lel') + comp = graph.add_sink_component(MySink, 'lel') + del graph del comp self.assertTrue(finalized) -@unittest.skip("this is broken") class GenericComponentTestCase(unittest.TestCase): @staticmethod def _create_comp(comp_cls, name=None): @@ -86,7 +72,7 @@ class GenericComponentTestCase(unittest.TestCase): if name is None: name = 'comp' - return graph.add_component(comp_cls, name) + return graph.add_sink_component(comp_cls, name) def test_name(self): class MySink(bt2._UserSinkComponent): @@ -96,15 +82,6 @@ class GenericComponentTestCase(unittest.TestCase): comp = self._create_comp(MySink, 'yaes') self.assertEqual(comp.name, 'yaes') - def test_graph(self): - class MySink(bt2._UserSinkComponent): - def _consume(self): - pass - - graph = bt2.Graph() - comp = graph.add_component(MySink, 'lel') - self.assertEqual(comp.graph, graph) - def test_class(self): class MySink(bt2._UserSinkComponent): def _consume(self): -- 2.34.1