Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / bindings / python / bt2 / utils.py
index 8dfdfd6adc27d142b8881fe2ad1aaa6c33215001..8d2d08fbf9c5f8e0a474ac68b78d854e0ce605bf 100644 (file)
@@ -1,20 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-only
 #
 # 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 collections.abc
@@ -26,7 +13,7 @@ import collections.abc
 # The value returned by the callable is returned by run_in_component_init.
 def run_in_component_init(func):
     class MySink(bt2._UserSinkComponent):
-        def __init__(self, params, obj):
+        def __init__(self, config, params, obj):
             nonlocal res_bound
             res_bound = func(self)
 
@@ -65,7 +52,7 @@ def _get_all_message_types(with_packet=True):
     _msgs = None
 
     class MyIter(bt2._UserMessageIterator):
-        def __init__(self, self_output_port):
+        def __init__(self, config, self_output_port):
 
             nonlocal _msgs
             self._at = 0
@@ -125,7 +112,7 @@ def _get_all_message_types(with_packet=True):
             return msg
 
     class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
-        def __init__(self, params, obj):
+        def __init__(self, config, params, obj):
             tc = self._create_trace_class()
             clock_class = self._create_clock_class(frequency=1000)
 
@@ -251,15 +238,13 @@ def get_const_event_message():
 # from this port, it puts the returned message in the initialization
 # list as the first item.
 class TestProxySink(bt2._UserSinkComponent):
-    def __init__(self, params, msg_list):
+    def __init__(self, config, params, msg_list):
         assert msg_list is not None
         self._msg_list = msg_list
         self._add_input_port('in')
 
     def _user_graph_is_configured(self):
-        self._msg_iter = self._create_input_port_message_iterator(
-            self._input_ports['in']
-        )
+        self._msg_iter = self._create_message_iterator(self._input_ports['in'])
 
     def _user_consume(self):
         assert self._msg_list[0] is None
@@ -290,3 +275,51 @@ class TestOutputPortMessageIterator(collections.abc.Iterator):
         assert msg is not None
         self._msg_list[0] = None
         return msg
+
+
+# Create a const field of the given field class.
+#
+# The field is part of a dummy stream, itself part of a dummy trace created
+# from trace class `tc`.
+def create_const_field(tc, field_class, field_value_setter_fn):
+    field_name = 'const field'
+
+    class MyIter(bt2._UserMessageIterator):
+        def __init__(self, config, self_port_output):
+            nonlocal field_class
+            nonlocal field_value_setter_fn
+            trace = tc()
+            packet_context_fc = tc.create_structure_field_class()
+            packet_context_fc.append_member(field_name, field_class)
+            sc = tc.create_stream_class(
+                packet_context_field_class=packet_context_fc, supports_packets=True
+            )
+            stream = trace.create_stream(sc)
+            packet = stream.create_packet()
+
+            field_value_setter_fn(packet.context_field[field_name])
+
+            self._msgs = [
+                self._create_stream_beginning_message(stream),
+                self._create_packet_beginning_message(packet),
+            ]
+
+        def __next__(self):
+            if len(self._msgs) == 0:
+                raise StopIteration
+
+            return self._msgs.pop(0)
+
+    class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
+        def __init__(self, config, params, obj):
+            self._add_output_port('out', params)
+
+    graph = bt2.Graph()
+    src_comp = graph.add_component(MySrc, 'my_source', None)
+    msg_iter = TestOutputPortMessageIterator(graph, src_comp.output_ports['out'])
+
+    # Ignore first message, stream beginning
+    _ = next(msg_iter)
+    packet_beg_msg = next(msg_iter)
+
+    return packet_beg_msg.packet.context_field[field_name]
This page took 0.026321 seconds and 4 git commands to generate.