Python bindings: work around Python 3.5 behaviour change
[babeltrace.git] / bindings / python / babeltrace.i.in
index 5a06f0ba4639db4ca12a76723b5c5198e5779be9..f07f2fb7af025f321e3664c39d0f424121f2a39c 100644 (file)
@@ -99,6 +99,7 @@ const char *_bt_python_get_array_string(struct bt_definition *field);
 const char *_bt_python_get_sequence_string(struct bt_definition *field);
 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field);
 enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field);
+struct bt_iter_pos *_bt_python_create_iter_pos(void);
 
 /* =================================================================
                CONTEXT.H, CONTEXT-INTERNAL.H
@@ -211,22 +212,36 @@ class TraceCollection:
                """
                Generator function to iterate over the events of open in the current
                TraceCollection.
+
+               Due to limitations of the native Babeltrace API, only one event
+               may be "alive" at a time (i.e. a user should never store a copy
+               of the events returned by this function for ulterior use). Users
+               shall make sure to copy the information they need from an event
+               before accessing the next one.
+
+               Furthermore, event objects become invalid when the generator goes
+               out of scope as the underlying iterator will be reclaimed. Using an
+               event after the the generator has gone out of scope may result in a
+               crash or data corruption.
                """
-               begin_pos_ptr = _bt_iter_pos()
-               end_pos_ptr = _bt_iter_pos()
+               begin_pos_ptr = _bt_python_create_iter_pos()
+               end_pos_ptr = _bt_python_create_iter_pos()
                begin_pos_ptr.type = SEEK_BEGIN
                end_pos_ptr.type = SEEK_LAST
 
                for event in self._events(begin_pos_ptr, end_pos_ptr):
                        yield event
 
+               _bt_iter_free_pos(begin_pos_ptr)
+               _bt_iter_free_pos(end_pos_ptr)
+
        def events_timestamps(self, timestamp_begin, timestamp_end):
                """
                Generator function to iterate over the events of open in the current
                TraceCollection from timestamp_begin to timestamp_end.
                """
-               begin_pos_ptr = _bt_iter_pos()
-               end_pos_ptr = _bt_iter_pos()
+               begin_pos_ptr = _bt_python_create_iter_pos()
+               end_pos_ptr = _bt_python_create_iter_pos()
                begin_pos_ptr.type = end_pos_ptr.type = SEEK_TIME
                begin_pos_ptr.u.seek_time = timestamp_begin
                end_pos_ptr.u.seek_time = timestamp_end
@@ -234,6 +249,9 @@ class TraceCollection:
                for event in self._events(begin_pos_ptr, end_pos_ptr):
                        yield event
 
+               _bt_iter_free_pos(begin_pos_ptr)
+               _bt_iter_free_pos(end_pos_ptr)
+
        @property
        def timestamp_begin(self):
                pos_ptr = _bt_iter_pos()
This page took 0.023094 seconds and 4 git commands to generate.