X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbindings%2Fpython%2Fbt2%2Fbt2%2Fgraph.py;h=ed99ef079c924c1e046770ae33ee0bffd381159b;hb=f5567ea88d172767b34373bc6e402da8bfd85ef8;hp=79be1ca80905a94c8aab0de01044c96b41bf287f;hpb=2d37d7fd830af46ef9816ffceeaa2426a0a3284e;p=babeltrace.git diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index 79be1ca8..ed99ef07 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -1,24 +1,6 @@ -# The MIT License (MIT) +# SPDX-License-Identifier: MIT # # Copyright (c) 2017 Philippe Proulx -# -# 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, object, utils from bt2 import interrupter as bt2_interrupter @@ -40,32 +22,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) @@ -74,15 +30,19 @@ class Graph(object._SharedObject): utils._check_uint64(mip_version) if mip_version > bt2.get_maximal_mip_version(): - raise ValueError('unknown MIP version {}'.format(mip_version)) + raise ValueError("unknown MIP version {}".format(mip_version)) ptr = native_bt.graph_create(mip_version) if ptr is None: - raise bt2._MemoryError('cannot create graph object') + raise bt2._MemoryError("cannot create graph object") 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, @@ -127,7 +87,7 @@ class Graph(object._SharedObject): base_cc_ptr = component_class._bt_component_class_ptr() if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): - raise ValueError('cannot pass a Python object to a non-Python component') + raise ValueError("cannot pass a Python object to a non-Python component") if params is not None and not isinstance(params, (dict, bt2.MapValue)): raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.") @@ -139,9 +99,11 @@ class Graph(object._SharedObject): status, comp_ptr = add_fn( self._ptr, cc_ptr, name, params_ptr, obj, logging_level ) - utils._handle_func_status(status, 'cannot add component to graph') + 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) @@ -149,9 +111,9 @@ class Graph(object._SharedObject): status, conn_ptr = native_bt.graph_connect_ports( self._ptr, upstream_port._ptr, downstream_port._ptr ) - utils._handle_func_status(status, 'cannot connect component ports within graph') + 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): @@ -164,32 +126,24 @@ class Graph(object._SharedObject): listener_ids = fn(self._ptr, listener_from_native) if listener_ids is None: - raise bt2._Error('cannot add listener to graph object') - - def add_ports_connected_listener(self, listener): - if not callable(listener): - raise TypeError("'listener' parameter is not callable") + raise bt2._Error("cannot add listener to graph object") - 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') + # keep the partial's reference + self._listener_partials.append(listener_from_native) def run_once(self): status = native_bt.graph_run_once(self._ptr) - utils._handle_func_status(status, 'graph object could not run once') + utils._handle_func_status(status, "graph object could not run once") def run(self): status = native_bt.graph_run(self._ptr) - utils._handle_func_status(status, 'graph object stopped running') + utils._handle_func_status(status, "graph object stopped running") def add_interrupter(self, interrupter): 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)