bt2: Force usage of MapValue object on component init
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Mon, 21 Oct 2019 16:22:30 +0000 (12:22 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 30 Oct 2019 19:14:52 +0000 (15:14 -0400)
Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I95c7402a28020467d06083d231f3b5bd6eb1fe70
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2226
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/bindings/python/bt2/bt2/graph.py
tests/bindings/python/bt2/test_graph.py

index 82708025c19032b91c72b063d977567b95ca4995..ef40014ba9262e58e678193e2b9355c59a92de27 100644 (file)
@@ -129,7 +129,11 @@ class Graph(object._SharedObject):
         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')
 
         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')
 
+        if params is not None and not isinstance(params, (dict, bt2.MapValue)):
+            raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.")
+
         params = bt2.create_value(params)
         params = bt2.create_value(params)
+
         params_ptr = params._ptr if params is not None else None
 
         status, comp_ptr = add_fn(
         params_ptr = params._ptr if params is not None else None
 
         status, comp_ptr = add_fn(
index 744f6429274662c23ae5370cbb075d9ae93b5b04..8c2a0621f2f0b6bcbed107dface114a2ab5a561b 100644 (file)
@@ -159,6 +159,48 @@ class GraphTestCase(unittest.TestCase):
         with self.assertRaises(ValueError):
             self._graph.add_component(MySink, 'salut', logging_level=12345)
 
         with self.assertRaises(ValueError):
             self._graph.add_component(MySink, 'salut', logging_level=12345)
 
+    def test_add_component_invalid_params_type(self):
+        class MySink(bt2._UserSinkComponent):
+            def _user_consume(self):
+                pass
+
+        with self.assertRaises(TypeError):
+            self._graph.add_component(MySink, 'salut', params=12)
+
+    def test_add_component_params_dict(self):
+        params_obj = None
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(self, config, params, obj):
+                nonlocal params_obj
+                params_obj = params
+
+            def _user_consume(self):
+                pass
+
+        params = {'plage': 12312}
+        self._graph.add_component(MySink, 'salut', params=params)
+
+        # Check equality and not identity because `add_component()` method
+        # converts the Python `dict` to a `bt2.MapValue`.
+        self.assertEqual(params, params_obj)
+
+    def test_add_component_params_mapvalue(self):
+        params_obj = None
+
+        class MySink(bt2._UserSinkComponent):
+            def __init__(self, config, params, obj):
+                nonlocal params_obj
+                params_obj = params
+
+            def _user_consume(self):
+                pass
+
+        params = bt2.MapValue({'beachclub': '121'})
+        self._graph.add_component(MySink, 'salut', params=params)
+
+        self.assertEqual(params, params_obj)
+
     def test_add_component_logging_level(self):
         class MySink(bt2._UserSinkComponent):
             def _user_consume(self):
     def test_add_component_logging_level(self):
         class MySink(bt2._UserSinkComponent):
             def _user_consume(self):
This page took 0.026827 seconds and 4 git commands to generate.