From 5813b3a3ffb8ff44d54b134e26d604af0b5e36db Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Fri, 6 Sep 2019 11:49:57 -0400 Subject: [PATCH] bt2: Add `Const` suffix to `_*Port` classes and adapt tests Signed-off-by: Francis Deslauriers Change-Id: Ic71e640abb206c249c63316af6ff536308291301 Reviewed-on: https://review.lttng.org/c/babeltrace/+/2008 Tested-by: jenkins Reviewed-by: Simon Marchi --- src/bindings/python/bt2/bt2/component.py | 10 +++++----- src/bindings/python/bt2/bt2/connection.py | 4 ++-- src/bindings/python/bt2/bt2/graph.py | 10 +++++----- src/bindings/python/bt2/bt2/port.py | 18 +++++++++--------- .../bt2/trace_collection_message_iterator.py | 2 +- tests/bindings/python/bt2/test_connection.py | 3 +++ tests/bindings/python/bt2/test_port.py | 3 +++ 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/bindings/python/bt2/bt2/component.py b/src/bindings/python/bt2/bt2/component.py index acc5b22c..142cd269 100644 --- a/src/bindings/python/bt2/bt2/component.py +++ b/src/bindings/python/bt2/bt2/component.py @@ -247,7 +247,7 @@ class _GenericSourceComponent(object._SharedObject, _SourceComponent): native_bt.component_source_borrow_output_port_by_name_const, native_bt.component_source_borrow_output_port_by_index_const, native_bt.component_source_get_output_port_count, - bt2_port._OutputPort, + bt2_port._OutputPortConst, ) @@ -264,7 +264,7 @@ class _GenericFilterComponent(object._SharedObject, _FilterComponent): native_bt.component_filter_borrow_output_port_by_name_const, native_bt.component_filter_borrow_output_port_by_index_const, native_bt.component_filter_get_output_port_count, - bt2_port._OutputPort, + bt2_port._OutputPortConst, ) @property @@ -274,7 +274,7 @@ class _GenericFilterComponent(object._SharedObject, _FilterComponent): native_bt.component_filter_borrow_input_port_by_name_const, native_bt.component_filter_borrow_input_port_by_index_const, native_bt.component_filter_get_input_port_count, - bt2_port._InputPort, + bt2_port._InputPortConst, ) @@ -291,7 +291,7 @@ class _GenericSinkComponent(object._SharedObject, _SinkComponent): native_bt.component_sink_borrow_input_port_by_name_const, native_bt.component_sink_borrow_input_port_by_index_const, native_bt.component_sink_get_input_port_count, - bt2_port._InputPort, + bt2_port._InputPortConst, ) @@ -701,7 +701,7 @@ class _UserComponent(metaclass=_UserComponentType): else: other_port_type = native_bt.PORT_TYPE_OUTPUT - other_port = bt2_port._create_from_ptr_and_get_ref( + other_port = bt2_port._create_from_const_ptr_and_get_ref( other_port_ptr, other_port_type ) self._user_port_connected(port, other_port) diff --git a/src/bindings/python/bt2/bt2/connection.py b/src/bindings/python/bt2/bt2/connection.py index cb69ef5a..aac76dd3 100644 --- a/src/bindings/python/bt2/bt2/connection.py +++ b/src/bindings/python/bt2/bt2/connection.py @@ -32,13 +32,13 @@ class _ConnectionConst(bt2_object._SharedObject): @property def downstream_port(self): port_ptr = native_bt.connection_borrow_downstream_port_const(self._ptr) - return bt2_port._create_from_ptr_and_get_ref( + return bt2_port._create_from_const_ptr_and_get_ref( port_ptr, native_bt.PORT_TYPE_INPUT ) @property def upstream_port(self): port_ptr = native_bt.connection_borrow_upstream_port_const(self._ptr) - return bt2_port._create_from_ptr_and_get_ref( + return bt2_port._create_from_const_ptr_and_get_ref( port_ptr, native_bt.PORT_TYPE_OUTPUT ) diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index 14c3b65b..93636c93 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -36,7 +36,7 @@ def _graph_port_added_listener_from_native( component = bt2_component._create_component_from_ptr_and_get_ref( component_ptr, component_type ) - port = bt2_port._create_from_ptr_and_get_ref(port_ptr, port_type) + port = bt2_port._create_from_const_ptr_and_get_ref(port_ptr, port_type) user_listener(component, port) @@ -52,13 +52,13 @@ def _graph_ports_connected_listener_from_native( upstream_component = bt2_component._create_component_from_ptr_and_get_ref( upstream_component_ptr, upstream_component_type ) - upstream_port = bt2_port._create_from_ptr_and_get_ref( + upstream_port = bt2_port._create_from_const_ptr_and_get_ref( upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT ) downstream_component = bt2_component._create_component_from_ptr_and_get_ref( downstream_component_ptr, downstream_component_type ) - downstream_port = bt2_port._create_from_ptr_and_get_ref( + downstream_port = bt2_port._create_from_const_ptr_and_get_ref( downstream_port_ptr, native_bt.PORT_TYPE_INPUT ) user_listener( @@ -140,8 +140,8 @@ class Graph(object._SharedObject): return bt2_component._create_component_from_ptr(comp_ptr, cc_type) def connect_ports(self, upstream_port, downstream_port): - utils._check_type(upstream_port, bt2_port._OutputPort) - utils._check_type(downstream_port, bt2_port._InputPort) + utils._check_type(upstream_port, bt2_port._OutputPortConst) + utils._check_type(downstream_port, bt2_port._InputPortConst) status, conn_ptr = native_bt.graph_connect_ports( self._ptr, upstream_port._ptr, downstream_port._ptr ) diff --git a/src/bindings/python/bt2/bt2/port.py b/src/bindings/python/bt2/bt2/port.py index d3bc398f..94738d85 100644 --- a/src/bindings/python/bt2/bt2/port.py +++ b/src/bindings/python/bt2/bt2/port.py @@ -24,7 +24,7 @@ from bt2 import native_bt, object from bt2 import connection as bt2_connection -def _create_from_ptr_and_get_ref(ptr, port_type): +def _create_from_const_ptr_and_get_ref(ptr, port_type): cls = _PORT_TYPE_TO_PYCLS.get(port_type, None) if cls is None: @@ -42,7 +42,7 @@ def _create_self_from_ptr_and_get_ref(ptr, port_type): return cls._create_from_ptr_and_get_ref(ptr) -class _Port(object._SharedObject): +class _PortConst(object._SharedObject): @classmethod def _get_ref(cls, ptr): ptr = cls._as_port_ptr(ptr) @@ -75,15 +75,15 @@ class _Port(object._SharedObject): return self.connection is not None -class _InputPort(_Port): +class _InputPortConst(_PortConst): _as_port_ptr = staticmethod(native_bt.port_input_as_port_const) -class _OutputPort(_Port): +class _OutputPortConst(_PortConst): _as_port_ptr = staticmethod(native_bt.port_output_as_port_const) -class _UserComponentPort(_Port): +class _UserComponentPort(_PortConst): @classmethod def _as_port_ptr(cls, ptr): ptr = cls._as_self_port_ptr(ptr) @@ -105,21 +105,21 @@ class _UserComponentPort(_Port): return native_bt.self_component_port_get_data(ptr) -class _UserComponentInputPort(_UserComponentPort, _InputPort): +class _UserComponentInputPort(_UserComponentPort, _InputPortConst): _as_self_port_ptr = staticmethod( native_bt.self_component_port_input_as_self_component_port ) -class _UserComponentOutputPort(_UserComponentPort, _OutputPort): +class _UserComponentOutputPort(_UserComponentPort, _OutputPortConst): _as_self_port_ptr = staticmethod( native_bt.self_component_port_output_as_self_component_port ) _PORT_TYPE_TO_PYCLS = { - native_bt.PORT_TYPE_INPUT: _InputPort, - native_bt.PORT_TYPE_OUTPUT: _OutputPort, + native_bt.PORT_TYPE_INPUT: _InputPortConst, + native_bt.PORT_TYPE_OUTPUT: _OutputPortConst, } diff --git a/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py b/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py index 821b5eae..b91440df 100644 --- a/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py +++ b/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py @@ -497,7 +497,7 @@ class TraceCollectionMessageIterator(bt2_message_iterator._MessageIterator): if not self._connect_ports: return - if type(port) is bt2_port._InputPort: + if type(port) is bt2_port._InputPortConst: return if component not in [comp.comp for comp in self._src_comps_and_specs]: diff --git a/tests/bindings/python/bt2/test_connection.py b/tests/bindings/python/bt2/test_connection.py index edeefd08..77d12410 100644 --- a/tests/bindings/python/bt2/test_connection.py +++ b/tests/bindings/python/bt2/test_connection.py @@ -19,6 +19,7 @@ import unittest import bt2 from bt2 import connection as bt2_connection +from bt2 import port as bt2_port class ConnectionTestCase(unittest.TestCase): @@ -66,6 +67,7 @@ class ConnectionTestCase(unittest.TestCase): conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self.assertEqual(conn.downstream_port.addr, sink.input_ports['in'].addr) self.assertIs(type(conn), bt2_connection._ConnectionConst) + self.assertIs(type(conn.downstream_port), bt2_port._InputPortConst) def test_upstream_port(self): class MyIter(bt2._UserMessageIterator): @@ -88,3 +90,4 @@ class ConnectionTestCase(unittest.TestCase): sink = graph.add_component(MySink, 'sink') conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self.assertEqual(conn.upstream_port.addr, src.output_ports['out'].addr) + self.assertIs(type(conn.upstream_port), bt2_port._OutputPortConst) diff --git a/tests/bindings/python/bt2/test_port.py b/tests/bindings/python/bt2/test_port.py index 7db49fb2..5c1a8271 100644 --- a/tests/bindings/python/bt2/test_port.py +++ b/tests/bindings/python/bt2/test_port.py @@ -18,6 +18,7 @@ import unittest import bt2 +from bt2 import port as bt2_port class PortTestCase(unittest.TestCase): @@ -42,6 +43,7 @@ class PortTestCase(unittest.TestCase): comp = self._create_comp(MySource) self.assertEqual(len(comp.output_ports), 1) + self.assertIs(type(comp.output_ports['out']), bt2_port._OutputPortConst) def test_flt_add_output_port(self): class MyIter(bt2._UserMessageIterator): @@ -68,6 +70,7 @@ class PortTestCase(unittest.TestCase): comp = self._create_comp(MyFilter) self.assertEqual(len(comp.input_ports), 1) + self.assertIs(type(comp.input_ports['in']), bt2_port._InputPortConst) def test_sink_add_input_port(self): class MySink(bt2._UserSinkComponent): -- 2.34.1