python: run isort on everything
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message.py
index cdf18879672414f3f1902949c889a2ce0e05376b..c56f696190c3c7cb1ed2bf59ccb87b3fd9df9115 100644 (file)
@@ -1,30 +1,14 @@
-# The MIT License (MIT)
+# SPDX-License-Identifier: MIT
 #
 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-from bt2 import native_bt, object, utils
-from bt2 import clock_snapshot as bt2_clock_snapshot
+
+from bt2 import event as bt2_event
+from bt2 import utils as bt2_utils
+from bt2 import object as bt2_object
 from bt2 import packet as bt2_packet
 from bt2 import stream as bt2_stream
-from bt2 import event as bt2_event
+from bt2 import native_bt
+from bt2 import clock_snapshot as bt2_clock_snapshot
 
 
 def _create_from_ptr(ptr):
@@ -32,15 +16,20 @@ def _create_from_ptr(ptr):
     return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
 
 
-class _MessageConst(object._SharedObject):
-    _get_ref = staticmethod(native_bt.message_get_ref)
-    _put_ref = staticmethod(native_bt.message_put_ref)
+class _MessageConst(bt2_object._SharedObject):
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.message_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.message_put_ref(ptr)
 
     @staticmethod
     def _check_has_default_clock_class(clock_class):
         if clock_class is None:
             raise ValueError(
-                'cannot get default clock snapshot: stream class has no default clock class'
+                "cannot get default clock snapshot: stream class has no default clock class"
             )
 
 
@@ -152,7 +141,7 @@ class _StreamMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot):
 
 class _StreamMessage(_StreamMessageConst, _Message):
     def _default_clock_snapshot(self, raw_value):
-        utils._check_uint64(raw_value)
+        bt2_utils._check_uint64(raw_value)
         self._set_default_clock_snapshot(self._ptr, raw_value)
 
     _default_clock_snapshot = property(
@@ -195,15 +184,15 @@ class _StreamEndMessage(_StreamMessage):
 class _MessageIteratorInactivityMessageConst(
     _MessageConst, _MessageWithDefaultClockSnapshot
 ):
-    _borrow_default_clock_snapshot_ptr = staticmethod(
-        native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
+    _borrow_clock_snapshot_ptr = staticmethod(
+        native_bt.message_message_iterator_inactivity_borrow_clock_snapshot_const
     )
 
     @property
-    def default_clock_snapshot(self):
-        # This kind of message always has a default clock class: no
+    def clock_snapshot(self):
+        # This kind of message always has a clock class: no
         # need to call self._check_has_default_clock_class() here.
-        return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
+        return self._get_default_clock_snapshot(self._borrow_clock_snapshot_ptr)
 
 
 class _MessageIteratorInactivityMessage(
@@ -230,7 +219,7 @@ class _DiscardedMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot):
     def _check_has_default_clock_snapshots(self):
         if not self._has_default_clock_snapshots:
             raise ValueError(
-                'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
+                "cannot get default clock snapshot: such a message has no clock snapshots for this stream class"
             )
 
     @property
@@ -250,7 +239,11 @@ class _DiscardedMessage(_DiscardedMessageConst, _Message):
     _stream_pycls = property(lambda _: bt2_stream._Stream)
 
     def _set_count(self, count):
-        utils._check_uint64(count)
+        bt2_utils._check_uint64(count)
+
+        if count == 0:
+            raise ValueError("discarded {} count is 0".format(self._item_name))
+
         self._set_count(self._ptr, count)
 
     _count = property(fget=_DiscardedMessageConst.count.fget, fset=_set_count)
@@ -273,9 +266,10 @@ class _DiscardedEventsMessageConst(_DiscardedMessageConst):
         return self.stream.cls.discarded_events_have_default_clock_snapshots
 
 
-class _DiscardedEventsMessage(_DiscardedMessage):
+class _DiscardedEventsMessage(_DiscardedEventsMessageConst, _DiscardedMessage):
     _borrow_stream_ptr = staticmethod(native_bt.message_discarded_events_borrow_stream)
     _set_count = staticmethod(native_bt.message_discarded_events_set_count)
+    _item_name = "event"
 
 
 class _DiscardedPacketsMessageConst(_DiscardedMessageConst):
@@ -298,6 +292,7 @@ class _DiscardedPacketsMessageConst(_DiscardedMessageConst):
 class _DiscardedPacketsMessage(_DiscardedPacketsMessageConst, _DiscardedMessage):
     _borrow_stream_ptr = staticmethod(native_bt.message_discarded_packets_borrow_stream)
     _set_count = staticmethod(native_bt.message_discarded_packets_set_count)
+    _item_name = "packet"
 
 
 _MESSAGE_TYPE_TO_CLS = {
This page took 0.027064 seconds and 4 git commands to generate.