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