Update Python bindings and tests to match the latest API
[babeltrace.git] / bindings / python / bt2 / notification_iterator.py
index e0d0b0c79f3e1ce520a42e61c207a3eadbec231a..39b6bf0e3b776dd8160342c2d49250141e25a2ec 100644 (file)
@@ -27,62 +27,54 @@ import bt2.component
 import bt2
 
 
-class NotificationIteratorSeekOrigin:
-    BEGIN = native_bt.NOTIFICATION_ITERATOR_SEEK_ORIGIN_BEGIN
-    CURRENT = native_bt.NOTIFICATION_ITERATOR_SEEK_ORIGIN_CURRENT
-    END = native_bt.NOTIFICATION_ITERATOR_SEEK_ORIGIN_END
-
-
-class _GenericNotificationIteratorMethods(collections.abc.Iterator):
-    @property
-    def notification(self):
-        notif_ptr = native_bt.notification_iterator_get_notification(self._ptr)
-        utils._handle_ptr(notif_ptr, "cannot get notification iterator object's current notification object")
-        return bt2.notification._create_from_ptr(notif_ptr)
-
+class _NotificationIterator(collections.abc.Iterator):
     def _handle_status(self, status, gen_error_msg):
-        if status == native_bt.NOTIFICATION_ITERATOR_STATUS_END:
+        if status == native_bt.NOTIFICATION_ITERATOR_STATUS_CANCELED:
+            raise bt2.NotificationIteratorCanceled
+        elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_AGAIN:
+            raise bt2.TryAgain
+        elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_END:
             raise bt2.Stop
         elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED:
             raise bt2.UnsupportedFeature
         elif status < 0:
             raise bt2.Error(gen_error_msg)
 
-    def next(self):
-        status = native_bt.notification_iterator_next(self._ptr)
-        self._handle_status(status,
-                            'unexpected error: cannot go to the next notification')
-
     def __next__(self):
-        self.next()
-        return self.notification
-
-    def seek_to_time(self, origin, time):
-        utils._check_int64(origin)
-        utils._check_int64(time)
-        status = native_bt.notification_iterator_seek_time(self._ptr, origin,
-                                                           time)
-        self._handle_status(status,
-                            'unexpected error: cannot seek notification iterator to time')
+        raise NotImplementedError
 
 
-class _GenericNotificationIterator(object._Object, _GenericNotificationIteratorMethods):
+class _GenericNotificationIterator(object._Object, _NotificationIterator):
     @property
     def component(self):
         comp_ptr = native_bt.notification_iterator_get_component(self._ptr)
-        utils._handle_ptr(comp_ptr, "cannot get notification iterator object's component object")
+        assert(comp_ptr)
         return bt2.component._create_generic_component_from_ptr(comp_ptr)
 
+    def _get_notif(self):
+        notif_ptr = native_bt.notification_iterator_get_notification(self._ptr)
+        utils._handle_ptr(notif_ptr, "cannot get notification iterator object's current notification object")
+        return bt2.notification._create_from_ptr(notif_ptr)
+
+    def _next(self):
+        status = native_bt.notification_iterator_next(self._ptr)
+        self._handle_status(status,
+                            'unexpected error: cannot advance the notification iterator')
+
+    def __next__(self):
+        self._next()
+        return self._get_notif()
 
-class UserNotificationIterator(_GenericNotificationIteratorMethods):
+
+class _UserNotificationIterator(_NotificationIterator):
     def __new__(cls, ptr):
-        # User iterator objects are always created by the BT system,
+        # User iterator objects are always created by the native side,
         # that is, never instantiated directly by Python code.
         #
-        # The system calls this, then manually calls self.__init__()
-        # without the `ptr` argument. The user has access to
-        # self.component during this call, thanks to this self._ptr
-        # argument being set.
+        # The native code calls this, then manually calls
+        # self.__init__() without the `ptr` argument. The user has
+        # access to self.component during this call, thanks to this
+        # self._ptr argument being set.
         #
         # self._ptr is NOT owned by this object here, so there's nothing
         # to do in __del__().
@@ -94,22 +86,30 @@ class UserNotificationIterator(_GenericNotificationIteratorMethods):
         pass
 
     @property
-    def component(self):
-        return native_bt.py3_get_component_from_notif_iter(self._ptr)
+    def _component(self):
+        return native_bt.py3_get_user_component_from_user_notif_iter(self._ptr)
 
     @property
     def addr(self):
         return int(self._ptr)
 
-    def _destroy(self):
+    def _finalize(self):
         pass
 
-    def _get_from_bt(self):
-        # this can raise anything: it's catched by the system
-        notif = self._get()
+    def __next__(self):
+        raise bt2.Stop
+
+    def _next_from_native(self):
+        # this can raise anything: it's catched by the native part
+        try:
+            notif = next(self)
+        except StopIteration:
+            raise bt2.Stop
+        except:
+            raise
+
         utils._check_type(notif, bt2.notification._Notification)
 
-        # steal the underlying native notification object for the caller
-        notif_ptr = notif._ptr
-        notif._ptr = None
-        return int(notif_ptr)
+        # take a new reference for the native part
+        notif._get()
+        return int(notif._ptr)
This page took 0.024657 seconds and 4 git commands to generate.