python: make all _get_ref/_put_ref proper static methods
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message_iterator.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4
5 from bt2 import native_bt, object, utils
6 from bt2 import message as bt2_message
7 import collections.abc
8 from bt2 import stream as bt2_stream
9 from bt2 import event_class as bt2_event_class
10 from bt2 import packet as bt2_packet
11 from bt2 import port as bt2_port
12 from bt2 import clock_class as bt2_clock_class
13 import bt2
14
15
16 class _MessageIterator(collections.abc.Iterator):
17 def __next__(self):
18 raise NotImplementedError
19
20
21 class _UserComponentInputPortMessageIterator(object._SharedObject, _MessageIterator):
22 @staticmethod
23 def _get_ref(ptr):
24 native_bt.message_iterator_get_ref(ptr)
25
26 @staticmethod
27 def _put_ref(ptr):
28 native_bt.message_iterator_put_ref(ptr)
29
30 def __init__(self, ptr):
31 self._current_msgs = []
32 self._at = 0
33 super().__init__(ptr)
34
35 def __next__(self):
36 if len(self._current_msgs) == self._at:
37 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
38 self._ptr
39 )
40 utils._handle_func_status(
41 status, "unexpected error: cannot advance the message iterator"
42 )
43 self._current_msgs = msgs
44 self._at = 0
45
46 msg_ptr = self._current_msgs[self._at]
47 self._at += 1
48
49 return bt2_message._create_from_ptr(msg_ptr)
50
51 def can_seek_beginning(self):
52 (status, res) = native_bt.message_iterator_can_seek_beginning(self._ptr)
53 utils._handle_func_status(
54 status,
55 "cannot check whether or not message iterator can seek its beginning",
56 )
57 return res != 0
58
59 def seek_beginning(self):
60 # Forget about buffered messages, they won't be valid after seeking.
61 self._current_msgs.clear()
62 self._at = 0
63
64 status = native_bt.message_iterator_seek_beginning(self._ptr)
65 utils._handle_func_status(status, "cannot seek message iterator beginning")
66
67 def can_seek_ns_from_origin(self, ns_from_origin):
68 utils._check_int64(ns_from_origin)
69 (status, res) = native_bt.message_iterator_can_seek_ns_from_origin(
70 self._ptr, ns_from_origin
71 )
72 utils._handle_func_status(
73 status,
74 "cannot check whether or not message iterator can seek given ns from origin",
75 )
76 return res != 0
77
78 def seek_ns_from_origin(self, ns_from_origin):
79 utils._check_int64(ns_from_origin)
80
81 # Forget about buffered messages, they won't be valid after seeking.
82 self._current_msgs.clear()
83 self._at = 0
84
85 status = native_bt.message_iterator_seek_ns_from_origin(
86 self._ptr, ns_from_origin
87 )
88 utils._handle_func_status(
89 status, "message iterator cannot seek given ns from origin"
90 )
91
92 @property
93 def can_seek_forward(self):
94 return native_bt.message_iterator_can_seek_forward(self._ptr)
95
96
97 class _MessageIteratorConfiguration:
98 def __init__(self, ptr):
99 self._ptr = ptr
100
101 def can_seek_forward(self, value):
102 utils._check_bool(value)
103 native_bt.self_message_iterator_configuration_set_can_seek_forward(
104 self._ptr, value
105 )
106
107 can_seek_forward = property(fset=can_seek_forward)
108
109
110 # This is extended by the user to implement component classes in Python. It
111 # is created for a given output port when an input port message iterator is
112 # created on the input port on the other side of the connection.
113 #
114 # Its purpose is to feed the messages that should go out through this output
115 # port.
116 class _UserMessageIterator(_MessageIterator):
117 def __new__(cls, ptr):
118 # User iterator objects are always created by the native side,
119 # that is, never instantiated directly by Python code.
120 #
121 # The native code calls this, then manually calls
122 # self.__init__() without the `ptr` argument. The user has
123 # access to self.component during this call, thanks to this
124 # self._bt_ptr argument being set.
125 #
126 # self._bt_ptr is NOT owned by this object here, so there's nothing
127 # to do in __del__().
128 self = super().__new__(cls)
129 self._bt_ptr = ptr
130 return self
131
132 def _bt_init_from_native(self, config_ptr, self_output_port_ptr):
133 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
134 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
135 )
136 config = _MessageIteratorConfiguration(config_ptr)
137 self.__init__(config, self_output_port)
138
139 def __init__(self, config, self_output_port):
140 pass
141
142 @property
143 def _component(self):
144 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
145
146 @property
147 def _port(self):
148 port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr)
149 assert port_ptr is not None
150 return bt2_port._create_self_from_ptr_and_get_ref(
151 port_ptr, native_bt.PORT_TYPE_OUTPUT
152 )
153
154 @property
155 def addr(self):
156 return int(self._bt_ptr)
157
158 @property
159 def _is_interrupted(self):
160 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
161
162 def _user_finalize(self):
163 pass
164
165 def __next__(self):
166 raise bt2.Stop
167
168 def _bt_next_from_native(self):
169 # this can raise anything: it's catched by the native part
170 try:
171 msg = next(self)
172 except StopIteration:
173 raise bt2.Stop
174 except Exception:
175 raise
176
177 utils._check_type(msg, bt2_message._MessageConst)
178
179 # The reference we return will be given to the message array.
180 # However, the `msg` Python object may stay alive, if the user has kept
181 # a reference to it. Acquire a new reference to account for that.
182 msg._get_ref(msg._ptr)
183 return int(msg._ptr)
184
185 def _bt_can_seek_beginning_from_native(self):
186 # Here, we mimic the behavior of the C API:
187 #
188 # - If the iterator has a _user_can_seek_beginning method,
189 # read it and use that result.
190 # - Otherwise, the presence or absence of a `_user_seek_beginning`
191 # method indicates whether the iterator can seek beginning.
192 if hasattr(self, "_user_can_seek_beginning"):
193 can_seek_beginning = self._user_can_seek_beginning()
194 utils._check_bool(can_seek_beginning)
195 return can_seek_beginning
196 else:
197 return hasattr(self, "_user_seek_beginning")
198
199 def _bt_seek_beginning_from_native(self):
200 self._user_seek_beginning()
201
202 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
203 # Return whether the iterator can seek ns from origin using the
204 # user-implemented seek_ns_from_origin method. We mimic the behavior
205 # of the C API:
206 #
207 # - If the iterator has a _user_can_seek_ns_from_origin method,
208 # call it and use its return value.
209 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
210 # we presume it's possible.
211
212 if hasattr(self, "_user_can_seek_ns_from_origin"):
213 can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin)
214 utils._check_bool(can_seek_ns_from_origin)
215 return can_seek_ns_from_origin
216 else:
217 return hasattr(self, "_user_seek_ns_from_origin")
218
219 def _bt_seek_ns_from_origin_from_native(self, ns_from_origin):
220 self._user_seek_ns_from_origin(ns_from_origin)
221
222 def _create_message_iterator(self, input_port):
223 utils._check_type(input_port, bt2_port._UserComponentInputPort)
224
225 if not input_port.is_connected:
226 raise ValueError("input port is not connected")
227
228 (
229 status,
230 msg_iter_ptr,
231 ) = native_bt.bt2_message_iterator_create_from_message_iterator(
232 self._bt_ptr, input_port._ptr
233 )
234 utils._handle_func_status(status, "cannot create message iterator object")
235 assert msg_iter_ptr is not None
236
237 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
238
239 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
240 utils._check_type(event_class, bt2_event_class._EventClass)
241
242 if event_class.stream_class.supports_packets:
243 utils._check_type(parent, bt2_packet._Packet)
244 else:
245 utils._check_type(parent, bt2_stream._Stream)
246
247 if default_clock_snapshot is not None:
248 if event_class.stream_class.default_clock_class is None:
249 raise ValueError(
250 "event messages in this stream must not have a default clock snapshot"
251 )
252
253 utils._check_uint64(default_clock_snapshot)
254
255 if event_class.stream_class.supports_packets:
256 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
257 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
258 )
259 else:
260 ptr = native_bt.message_event_create_with_default_clock_snapshot(
261 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
262 )
263 else:
264 if event_class.stream_class.default_clock_class is not None:
265 raise ValueError(
266 "event messages in this stream must have a default clock snapshot"
267 )
268
269 if event_class.stream_class.supports_packets:
270 ptr = native_bt.message_event_create_with_packet(
271 self._bt_ptr, event_class._ptr, parent._ptr
272 )
273 else:
274 ptr = native_bt.message_event_create(
275 self._bt_ptr, event_class._ptr, parent._ptr
276 )
277
278 if ptr is None:
279 raise bt2._MemoryError("cannot create event message object")
280
281 return bt2_message._EventMessage(ptr)
282
283 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
284 utils._check_type(clock_class, bt2_clock_class._ClockClass)
285 ptr = native_bt.message_message_iterator_inactivity_create(
286 self._bt_ptr, clock_class._ptr, clock_snapshot
287 )
288
289 if ptr is None:
290 raise bt2._MemoryError("cannot create inactivity message object")
291
292 return bt2_message._MessageIteratorInactivityMessage(ptr)
293
294 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
295 utils._check_type(stream, bt2_stream._Stream)
296
297 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
298 if ptr is None:
299 raise bt2._MemoryError("cannot create stream beginning message object")
300
301 msg = bt2_message._StreamBeginningMessage(ptr)
302
303 if default_clock_snapshot is not None:
304 msg._default_clock_snapshot = default_clock_snapshot
305
306 return msg
307
308 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
309 utils._check_type(stream, bt2_stream._Stream)
310
311 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
312 if ptr is None:
313 raise bt2._MemoryError("cannot create stream end message object")
314
315 msg = bt2_message._StreamEndMessage(ptr)
316
317 if default_clock_snapshot is not None:
318 msg._default_clock_snapshot = default_clock_snapshot
319
320 return msg
321
322 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
323 utils._check_type(packet, bt2_packet._Packet)
324
325 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
326 if default_clock_snapshot is None:
327 raise ValueError(
328 "packet beginning messages in this stream must have a default clock snapshot"
329 )
330
331 utils._check_uint64(default_clock_snapshot)
332 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
333 self._bt_ptr, packet._ptr, default_clock_snapshot
334 )
335 else:
336 if default_clock_snapshot is not None:
337 raise ValueError(
338 "packet beginning messages in this stream must not have a default clock snapshot"
339 )
340
341 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
342
343 if ptr is None:
344 raise bt2._MemoryError("cannot create packet beginning message object")
345
346 return bt2_message._PacketBeginningMessage(ptr)
347
348 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
349 utils._check_type(packet, bt2_packet._Packet)
350
351 if packet.stream.cls.packets_have_end_default_clock_snapshot:
352 if default_clock_snapshot is None:
353 raise ValueError(
354 "packet end messages in this stream must have a default clock snapshot"
355 )
356
357 utils._check_uint64(default_clock_snapshot)
358 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
359 self._bt_ptr, packet._ptr, default_clock_snapshot
360 )
361 else:
362 if default_clock_snapshot is not None:
363 raise ValueError(
364 "packet end messages in this stream must not have a default clock snapshot"
365 )
366
367 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
368
369 if ptr is None:
370 raise bt2._MemoryError("cannot create packet end message object")
371
372 return bt2_message._PacketEndMessage(ptr)
373
374 def _create_discarded_events_message(
375 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
376 ):
377 utils._check_type(stream, bt2_stream._Stream)
378
379 if not stream.cls.supports_discarded_events:
380 raise ValueError("stream class does not support discarded events")
381
382 if stream.cls.discarded_events_have_default_clock_snapshots:
383 if beg_clock_snapshot is None or end_clock_snapshot is None:
384 raise ValueError(
385 "discarded events have default clock snapshots for this stream class"
386 )
387
388 utils._check_uint64(beg_clock_snapshot)
389 utils._check_uint64(end_clock_snapshot)
390
391 if beg_clock_snapshot > end_clock_snapshot:
392 raise ValueError(
393 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
394 beg_clock_snapshot, end_clock_snapshot
395 )
396 )
397
398 ptr = (
399 native_bt.message_discarded_events_create_with_default_clock_snapshots(
400 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
401 )
402 )
403 else:
404 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
405 raise ValueError(
406 "discarded events have no default clock snapshots for this stream class"
407 )
408
409 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
410
411 if ptr is None:
412 raise bt2._MemoryError("cannot discarded events message object")
413
414 msg = bt2_message._DiscardedEventsMessage(ptr)
415
416 if count is not None:
417 msg._count = count
418
419 return msg
420
421 def _create_discarded_packets_message(
422 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
423 ):
424 utils._check_type(stream, bt2_stream._Stream)
425
426 if not stream.cls.supports_discarded_packets:
427 raise ValueError("stream class does not support discarded packets")
428
429 if stream.cls.discarded_packets_have_default_clock_snapshots:
430 if beg_clock_snapshot is None or end_clock_snapshot is None:
431 raise ValueError(
432 "discarded packets have default clock snapshots for this stream class"
433 )
434
435 utils._check_uint64(beg_clock_snapshot)
436 utils._check_uint64(end_clock_snapshot)
437
438 if beg_clock_snapshot > end_clock_snapshot:
439 raise ValueError(
440 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
441 beg_clock_snapshot, end_clock_snapshot
442 )
443 )
444
445 ptr = (
446 native_bt.message_discarded_packets_create_with_default_clock_snapshots(
447 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
448 )
449 )
450 else:
451 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
452 raise ValueError(
453 "discarded packets have no default clock snapshots for this stream class"
454 )
455
456 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
457
458 if ptr is None:
459 raise bt2._MemoryError("cannot discarded packets message object")
460
461 msg = bt2_message._DiscardedPacketsMessage(ptr)
462
463 if count is not None:
464 msg._count = count
465
466 return msg
This page took 0.039458 seconds and 4 git commands to generate.