lib: graph API: return borrowed references when adding to an object
[babeltrace.git] / src / bindings / python / bt2 / bt2 / graph.py
index a36c85d1b7176cb37626aa1976775a40ec8454b8..9611c6e287b9f7f69c8a1560317c5d75a56848f0 100644 (file)
@@ -40,32 +40,6 @@ def _graph_port_added_listener_from_native(
     user_listener(component, port)
 
 
-def _graph_ports_connected_listener_from_native(
-    user_listener,
-    upstream_component_ptr,
-    upstream_component_type,
-    upstream_port_ptr,
-    downstream_component_ptr,
-    downstream_component_type,
-    downstream_port_ptr,
-):
-    upstream_component = bt2_component._create_component_from_const_ptr_and_get_ref(
-        upstream_component_ptr, upstream_component_type
-    )
-    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_const_ptr_and_get_ref(
-        downstream_component_ptr, downstream_component_type
-    )
-    downstream_port = bt2_port._create_from_const_ptr_and_get_ref(
-        downstream_port_ptr, native_bt.PORT_TYPE_INPUT
-    )
-    user_listener(
-        upstream_component, upstream_port, downstream_component, downstream_port
-    )
-
-
 class Graph(object._SharedObject):
     _get_ref = staticmethod(native_bt.graph_get_ref)
     _put_ref = staticmethod(native_bt.graph_put_ref)
@@ -83,6 +57,10 @@ class Graph(object._SharedObject):
 
         super().__init__(ptr)
 
+        # list of listener partials to keep a reference as long as
+        # this graph exists
+        self._listener_partials = []
+
     def add_component(
         self,
         component_class,
@@ -141,7 +119,9 @@ class Graph(object._SharedObject):
         )
         utils._handle_func_status(status, 'cannot add component to graph')
         assert comp_ptr
-        return bt2_component._create_component_from_const_ptr(comp_ptr, cc_type)
+        return bt2_component._create_component_from_const_ptr_and_get_ref(
+            comp_ptr, cc_type
+        )
 
     def connect_ports(self, upstream_port, downstream_port):
         utils._check_type(upstream_port, bt2_port._OutputPortConst)
@@ -151,7 +131,7 @@ class Graph(object._SharedObject):
         )
         utils._handle_func_status(status, 'cannot connect component ports within graph')
         assert conn_ptr
-        return bt2_connection._ConnectionConst._create_from_ptr(conn_ptr)
+        return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr)
 
     def add_port_added_listener(self, listener):
         if not callable(listener):
@@ -166,22 +146,8 @@ class Graph(object._SharedObject):
         if listener_ids is None:
             raise bt2._Error('cannot add listener to graph object')
 
-        return utils._ListenerHandle(listener_ids, self)
-
-    def add_ports_connected_listener(self, listener):
-        if not callable(listener):
-            raise TypeError("'listener' parameter is not callable")
-
-        fn = native_bt.bt2_graph_add_ports_connected_listener
-        listener_from_native = functools.partial(
-            _graph_ports_connected_listener_from_native, listener
-        )
-
-        listener_ids = fn(self._ptr, listener_from_native)
-        if listener_ids is None:
-            raise bt2._Error('cannot add listener to graph object')
-
-        return utils._ListenerHandle(listener_ids, self)
+        # keep the partial's reference
+        self._listener_partials.append(listener_from_native)
 
     def run_once(self):
         status = native_bt.graph_run_once(self._ptr)
@@ -195,5 +161,7 @@ class Graph(object._SharedObject):
         utils._check_type(interrupter, bt2_interrupter.Interrupter)
         native_bt.graph_add_interrupter(self._ptr, interrupter._ptr)
 
-    def interrupt(self):
-        native_bt.graph_interrupt(self._ptr)
+    @property
+    def default_interrupter(self):
+        ptr = native_bt.graph_borrow_default_interrupter(self._ptr)
+        return bt2_interrupter.Interrupter._create_from_ptr_and_get_ref(ptr)
This page took 0.026596 seconds and 4 git commands to generate.