From bb93d641468f118734fbaa205a9a231a85bad60c Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 30 Apr 2019 16:30:32 -0400 Subject: [PATCH] bt2: update bindings to make test_plugins pass This patch updates the necessary stuff in the bindings to make test_plugins pass. One new test is added, test_find_file, which tests passing the path to a plugin shared object to bt2.find_plugins. Change-Id: Ie2ac6e830fb18d8839d2879567509cd2f6abbd81 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1087 CI-Build: Simon Marchi Reviewed-by: Philippe Proulx Tested-by: jenkins --- bindings/python/bt2/bt2/plugin.py | 93 ++++++++++++------------ tests/bindings/python/bt2/test_plugin.py | 11 ++- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/bindings/python/bt2/bt2/plugin.py b/bindings/python/bt2/bt2/plugin.py index a7ecf9b3..51f9a2ed 100644 --- a/bindings/python/bt2/bt2/plugin.py +++ b/bindings/python/bt2/bt2/plugin.py @@ -33,9 +33,9 @@ def find_plugins(path, recurse=True): plugin_set_ptr = None if os.path.isfile(path): - plugin_set_ptr = native_bt.plugin_create_all_from_file(path) + plugin_set_ptr = native_bt.plugin_find_all_from_file(path) elif os.path.isdir(path): - plugin_set_ptr = native_bt.plugin_create_all_from_dir(path, int(recurse)) + plugin_set_ptr = native_bt.plugin_find_all_from_dir(path, int(recurse)) if plugin_set_ptr is None: return @@ -54,6 +54,9 @@ def find_plugin(name): class _PluginSet(object._SharedObject, collections.abc.Sequence): + _put_ref = native_bt.plugin_set_put_ref + _get_ref = native_bt.plugin_set_get_ref + def __len__(self): count = native_bt.plugin_set_get_plugin_count(self._ptr) assert(count >= 0) @@ -65,9 +68,9 @@ class _PluginSet(object._SharedObject, collections.abc.Sequence): if index >= len(self): raise IndexError - plugin_ptr = native_bt.plugin_set_get_plugin(self._ptr, index) - assert(plugin_ptr) - return _Plugin._create_from_ptr(plugin_ptr) + plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index) + assert plugin_ptr is not None + return _Plugin._create_from_ptr_and_get_ref(plugin_ptr) class _PluginVersion: @@ -109,68 +112,68 @@ class _PluginComponentClassesIterator(collections.abc.Iterator): def __next__(self): plugin_ptr = self._plugin_comp_cls._plugin._ptr - comp_cls_type = self._plugin_comp_cls._comp_cls_type - total = native_bt.plugin_get_component_class_count(plugin_ptr) - - while True: - if self._at == total: - raise StopIteration - - comp_cls_ptr = native_bt.plugin_get_component_class_by_index(plugin_ptr, - self._at) - assert(comp_cls_ptr) - cc_type = native_bt.component_class_get_type(comp_cls_ptr) - self._at += 1 + total = self._plugin_comp_cls._component_class_count(plugin_ptr) - if cc_type == comp_cls_type: - break + if self._at == total: + raise StopIteration - native_bt.put(comp_cls_ptr) + comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(plugin_ptr, self._at) + assert comp_cls_ptr is not None + self._at += 1 + comp_cls_type = self._plugin_comp_cls._comp_cls_type + comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type] + comp_cls_ptr = comp_cls_pycls._as_component_class_ptr(comp_cls_ptr) name = native_bt.component_class_get_name(comp_cls_ptr) - native_bt.put(comp_cls_ptr) - assert(name is not None) + assert name is not None return name class _PluginComponentClasses(collections.abc.Mapping): - def __init__(self, plugin, comp_cls_type): + def __init__(self, plugin): self._plugin = plugin - self._comp_cls_type = comp_cls_type def __getitem__(self, key): utils._check_str(key) - cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._plugin._ptr, - key, - self._comp_cls_type) + cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key) if cc_ptr is None: raise KeyError(key) - return bt2.component._create_generic_component_class_from_ptr(cc_ptr) + return bt2.component._create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type) def __len__(self): - count = 0 - total = native_bt.plugin_get_component_class_count(self._plugin._ptr) + return self._component_class_count(self._plugin._ptr) + + def __iter__(self): + return _PluginComponentClassesIterator(self) - for at in range(total): - comp_cls_ptr = native_bt.plugin_get_component_class_by_index(self._plugin._ptr, - at) - assert(comp_cls_ptr) - cc_type = native_bt.component_class_get_type(comp_cls_ptr) - if cc_type == self._comp_cls_type: - count += 1 +class _PluginSourceComponentClasses(_PluginComponentClasses): + _component_class_count = native_bt.plugin_get_source_component_class_count + _borrow_component_class_by_name = native_bt.plugin_borrow_source_component_class_by_name_const + _borrow_component_class_by_index = native_bt.plugin_borrow_source_component_class_by_index_const + _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE - native_bt.put(comp_cls_ptr) - return count +class _PluginFilterComponentClasses(_PluginComponentClasses): + _component_class_count = native_bt.plugin_get_filter_component_class_count + _borrow_component_class_by_name = native_bt.plugin_borrow_filter_component_class_by_name_const + _borrow_component_class_by_index = native_bt.plugin_borrow_filter_component_class_by_index_const + _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER - def __iter__(self): - return _PluginComponentClassesIterator(self) + +class _PluginSinkComponentClasses(_PluginComponentClasses): + _component_class_count = native_bt.plugin_get_sink_component_class_count + _borrow_component_class_by_name = native_bt.plugin_borrow_sink_component_class_by_name_const + _borrow_component_class_by_index = native_bt.plugin_borrow_sink_component_class_by_index_const + _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK class _Plugin(object._SharedObject): + _put_ref = native_bt.plugin_put_ref + _get_ref = native_bt.plugin_get_ref + @property def name(self): name = native_bt.plugin_get_name(self._ptr) @@ -197,19 +200,19 @@ class _Plugin(object._SharedObject): def version(self): status, major, minor, patch, extra = native_bt.plugin_get_version(self._ptr) - if status < 0: + if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE: return return _PluginVersion(major, minor, patch, extra) @property def source_component_classes(self): - return _PluginComponentClasses(self, native_bt.COMPONENT_CLASS_TYPE_SOURCE) + return _PluginSourceComponentClasses(self) @property def filter_component_classes(self): - return _PluginComponentClasses(self, native_bt.COMPONENT_CLASS_TYPE_FILTER) + return _PluginFilterComponentClasses(self) @property def sink_component_classes(self): - return _PluginComponentClasses(self, native_bt.COMPONENT_CLASS_TYPE_SINK) + return _PluginSinkComponentClasses(self) diff --git a/tests/bindings/python/bt2/test_plugin.py b/tests/bindings/python/bt2/test_plugin.py index 68943b39..b50af4a5 100644 --- a/tests/bindings/python/bt2/test_plugin.py +++ b/tests/bindings/python/bt2/test_plugin.py @@ -1,6 +1,4 @@ -from bt2 import value import unittest -import copy import bt2 import bt2.plugin import os @@ -9,7 +7,6 @@ import os _TEST_PLUGIN_PLUGINS_PATH = os.environ['TEST_PLUGIN_PLUGINS_PATH'] -@unittest.skip("this is broken") class PluginSetTestCase(unittest.TestCase): def test_create(self): pset = bt2.find_plugins(_TEST_PLUGIN_PLUGINS_PATH) @@ -31,7 +28,6 @@ class PluginSetTestCase(unittest.TestCase): self.assertTrue('text' in names) -@unittest.skip("this is broken") class FindPluginsTestCase(unittest.TestCase): def test_find_none(self): pset = bt2.find_plugins('/this/does/not/exist/246703df-cb85-46d5-8406-5e8dc4a88b41') @@ -41,8 +37,12 @@ class FindPluginsTestCase(unittest.TestCase): pset = bt2.find_plugins(_TEST_PLUGIN_PLUGINS_PATH) self.assertTrue(len(pset) >= 3) + def test_find_file(self): + path = os.path.join(_TEST_PLUGIN_PLUGINS_PATH, 'utils', '.libs', 'babeltrace-plugin-utils.so') + pset = bt2.find_plugins(path) + self.assertTrue(len(pset) == 1) + -@unittest.skip("this is broken") class FindPluginTestCase(unittest.TestCase): def test_find_none(self): plugin = bt2.find_plugin('this-does-not-exist-246703df-cb85-46d5-8406-5e8dc4a88b41') @@ -53,7 +53,6 @@ class FindPluginTestCase(unittest.TestCase): self.assertIsInstance(plugin, bt2.plugin._Plugin) -@unittest.skip("this is broken") class PluginTestCase(unittest.TestCase): def setUp(self): self._plugin = bt2.find_plugin('ctf') -- 2.34.1