{
PyObject *py_status;
PyObject *py_return_tuple;
- PyObject *py_msg_list = Py_None;
+ PyObject *py_msg_list;
- py_status = SWIG_From_long_SS_long(status);
- if (status != __BT_FUNC_STATUS_OK) {
- goto end;
- }
-
- py_msg_list = create_pylist_from_messages(messages, message_count);
-
-end:
py_return_tuple = PyTuple_New(2);
BT_ASSERT(py_return_tuple);
+
+ /* Set tuple[0], status. */
+ py_status = SWIG_From_long_SS_long(status);
PyTuple_SET_ITEM(py_return_tuple, 0, py_status);
+
+ /* Set tuple[1], message list on success, None otherwise. */
+ if (status == __BT_FUNC_STATUS_OK) {
+ py_msg_list = create_pylist_from_messages(messages, message_count);
+ } else {
+ py_msg_list = Py_None;
+ Py_INCREF(py_msg_list);
+ }
+
PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list);
+
return py_return_tuple;
}
with self.assertRaises(bt2.Error):
it.seek_beginning()
+ # Try consuming many times from an iterator that always returns TryAgain.
+ # This verifies that we are not missing an incref of Py_None, making the
+ # refcount of Py_None reach 0.
+ def test_try_again_many_times(self):
+ class MyIter(bt2._UserMessageIterator):
+ def __next__(self):
+ raise bt2.TryAgain
+
+ class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter):
+ def __init__(self, params):
+ self._add_output_port('out')
+
+ graph = bt2.Graph()
+ src = graph.add_component(MySource, 'src')
+ it = graph.create_output_port_message_iterator(src.output_ports['out'])
+
+ # The initial refcount of Py_None was in the 7000, so 100000 iterations
+ # should be enough to catch the bug even if there are small differences
+ # between configurations.
+ for i in range(100000):
+ with self.assertRaises(bt2.TryAgain):
+ next(it)
+
class OutputPortMessageIteratorTestCase(unittest.TestCase):
def test_component(self):