From 1e92035335352f8a67cbc3de28a0ad44b7ee02a1 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 20 Jul 2019 18:47:17 -0400 Subject: [PATCH] bt2: add interrupter support This patch wraps the library's interrupter API within the `bt2` Python package. Tests are added to verify the new API. Signed-off-by: Philippe Proulx Change-Id: I4d81759d1eaeac0b8036b8cbe9a4fd46ec57cbf1 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1734 --- src/bindings/python/bt2/Makefile.am | 19 +++---- src/bindings/python/bt2/bt2/__init__.py.in | 1 + src/bindings/python/bt2/bt2/interrupter.py | 49 +++++++++++++++++++ src/bindings/python/bt2/bt2/native_bt.i | 3 +- .../python/bt2/bt2/native_bt_interrupter.i | 26 ++++++++++ tests/Makefile.am | 1 + tests/bindings/python/bt2/test_interrupter.py | 47 ++++++++++++++++++ 7 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 src/bindings/python/bt2/bt2/interrupter.py create mode 100644 src/bindings/python/bt2/bt2/native_bt_interrupter.i create mode 100644 tests/bindings/python/bt2/test_interrupter.py diff --git a/src/bindings/python/bt2/Makefile.am b/src/bindings/python/bt2/Makefile.am index c8719d66..2816279a 100644 --- a/src/bindings/python/bt2/Makefile.am +++ b/src/bindings/python/bt2/Makefile.am @@ -9,18 +9,18 @@ SWIG_INTERFACE_FILES = \ bt2/native_bt.i \ bt2/native_bt_clock_class.i \ bt2/native_bt_clock_snapshot.i \ - bt2/native_bt_component_class.i \ bt2/native_bt_component.i \ + bt2/native_bt_component_class.i \ bt2/native_bt_connection.i \ bt2/native_bt_error.i \ - bt2/native_bt_event_class.i \ bt2/native_bt_event.i \ + bt2/native_bt_event_class.i \ + bt2/native_bt_field.i \ bt2/native_bt_field_class.i \ bt2/native_bt_field_path.i \ - bt2/native_bt_field.i \ bt2/native_bt_graph.i \ bt2/native_bt_integer_range_set.i \ - bt2/native_bt.i \ + bt2/native_bt_interrupter.i \ bt2/native_bt_logging.i \ bt2/native_bt_message.i \ bt2/native_bt_message_iterator.i \ @@ -28,10 +28,10 @@ SWIG_INTERFACE_FILES = \ bt2/native_bt_plugin.i \ bt2/native_bt_port.i \ bt2/native_bt_query_exec.i \ - bt2/native_bt_stream_class.i \ bt2/native_bt_stream.i \ - bt2/native_bt_trace_class.i \ + bt2/native_bt_stream_class.i \ bt2/native_bt_trace.i \ + bt2/native_bt_trace_class.i \ bt2/native_bt_value.i \ bt2/native_bt_version.i @@ -46,24 +46,25 @@ STATIC_BINDINGS_DEPS = \ bt2/component.py \ bt2/connection.py \ bt2/error.py \ - bt2/event_class.py \ bt2/event.py \ + bt2/event_class.py \ bt2/field.py \ bt2/field_class.py \ bt2/field_path.py \ bt2/graph.py \ bt2/integer_range_set.py \ + bt2/interrupter.py \ bt2/logging.py \ - bt2/message_iterator.py \ bt2/message.py \ + bt2/message_iterator.py \ bt2/object.py \ bt2/packet.py \ bt2/plugin.py \ bt2/port.py \ bt2/py_plugin.py \ bt2/query_executor.py \ - bt2/stream_class.py \ bt2/stream.py \ + bt2/stream_class.py \ bt2/trace.py \ bt2/trace_class.py \ bt2/trace_collection_message_iterator.py \ diff --git a/src/bindings/python/bt2/bt2/__init__.py.in b/src/bindings/python/bt2/bt2/__init__.py.in index 7d63b697..a801446c 100644 --- a/src/bindings/python/bt2/bt2/__init__.py.in +++ b/src/bindings/python/bt2/bt2/__init__.py.in @@ -23,6 +23,7 @@ __version__ = '@PACKAGE_VERSION@' +from bt2.interrupter import * from bt2.clock_class import * from bt2.clock_snapshot import * from bt2.component import * diff --git a/src/bindings/python/bt2/bt2/interrupter.py b/src/bindings/python/bt2/bt2/interrupter.py new file mode 100644 index 00000000..9c9868d1 --- /dev/null +++ b/src/bindings/python/bt2/bt2/interrupter.py @@ -0,0 +1,49 @@ +# The MIT License (MIT) +# +# Copyright (c) 2019 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 + + +class Interrupter(object._SharedObject): + _get_ref = staticmethod(native_bt.interrupter_get_ref) + _put_ref = staticmethod(native_bt.interrupter_put_ref) + + def __init__(self): + ptr = native_bt.interrupter_create() + + if ptr is None: + raise bt2.CreationError('cannot create interrupter object') + + super().__init__(ptr) + + @property + def is_set(self): + return bool(native_bt.interrupter_is_set(self._ptr)) + + def __bool__(self): + return self.is_set + + def set(self): + native_bt.interrupter_set(self._ptr) + + def reset(self): + native_bt.interrupter_reset(self._ptr) diff --git a/src/bindings/python/bt2/bt2/native_bt.i b/src/bindings/python/bt2/bt2/native_bt.i index d27e4279..7b032b17 100644 --- a/src/bindings/python/bt2/bt2/native_bt.i +++ b/src/bindings/python/bt2/bt2/native_bt.i @@ -209,6 +209,8 @@ typedef int bt_bool; %include "native_bt_field_class.i" %include "native_bt_field_path.i" %include "native_bt_graph.i" +%include "native_bt_integer_range_set.i" +%include "native_bt_interrupter.i" %include "native_bt_logging.i" %include "native_bt_message.i" %include "native_bt_message_iterator.i" @@ -216,7 +218,6 @@ typedef int bt_bool; %include "native_bt_plugin.i" %include "native_bt_port.i" %include "native_bt_query_exec.i" -%include "native_bt_integer_range_set.i" %include "native_bt_stream.i" %include "native_bt_stream_class.i" %include "native_bt_trace.i" diff --git a/src/bindings/python/bt2/bt2/native_bt_interrupter.i b/src/bindings/python/bt2/bt2/native_bt_interrupter.i new file mode 100644 index 00000000..43991446 --- /dev/null +++ b/src/bindings/python/bt2/bt2/native_bt_interrupter.i @@ -0,0 +1,26 @@ +/* + * The MIT License (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. + */ + +%include +%include diff --git a/tests/Makefile.am b/tests/Makefile.am index 7ed75415..00c003b4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -16,6 +16,7 @@ dist_check_SCRIPTS = \ bindings/python/bt2/test_field.py \ bindings/python/bt2/test_graph.py \ bindings/python/bt2/test_integer_range_set.py \ + bindings/python/bt2/test_interrupter.py \ bindings/python/bt2/test_message_iterator.py \ bindings/python/bt2/test_message.py \ bindings/python/bt2/test_packet.py \ diff --git a/tests/bindings/python/bt2/test_interrupter.py b/tests/bindings/python/bt2/test_interrupter.py new file mode 100644 index 00000000..5c11f031 --- /dev/null +++ b/tests/bindings/python/bt2/test_interrupter.py @@ -0,0 +1,47 @@ +# +# Copyright (C) 2019 EfficiOS Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; only version 2 +# of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + +import bt2 +import unittest + + +class InterrupterTestCase(unittest.TestCase): + def setUp(self): + self._interrupter = bt2.Interrupter() + + def test_create(self): + self.assertFalse(self._interrupter.is_set) + + def test_is_set(self): + self.assertFalse(self._interrupter.is_set) + + def test_bool(self): + self.assertFalse(self._interrupter) + self._interrupter.set() + self.assertTrue(self._interrupter) + + def test_set(self): + self.assertFalse(self._interrupter) + self._interrupter.set() + self.assertTrue(self._interrupter) + + def test_reset(self): + self._interrupter.set() + self.assertTrue(self._interrupter) + self._interrupter.reset() + self.assertFalse(self._interrupter) -- 2.34.1