bt2: Add `_Clock*Const` classes and adapt tests
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 30 Aug 2019 16:20:05 +0000 (12:20 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 10 Sep 2019 15:20:25 +0000 (11:20 -0400)
Split Python Clock Class related classes to mimic the type safety
offered by the C api. Const classes offer a read-only view of the data.
Non-Const classes subclass their respective Const classes.

Makes all `_ClockSnapshot` object Const

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I81e4643c80e6e94445ae0e4948279891416a38ca
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1996
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/bindings/python/bt2/bt2/__init__.py
src/bindings/python/bt2/bt2/clock_class.py
src/bindings/python/bt2/bt2/clock_snapshot.py
src/bindings/python/bt2/bt2/message.py
tests/bindings/python/bt2/test_clock_class.py
tests/bindings/python/bt2/test_package.py

index 9ea77fb8f8e6121ed4f100a719dbebdeb977f9d0..3ae303bf599a9699033f34afb2aff08bc3c3f228 100644 (file)
@@ -22,7 +22,7 @@
 
 # import all public names
 from bt2.clock_class import ClockClassOffset
-from bt2.clock_snapshot import _ClockSnapshot
+from bt2.clock_snapshot import _ClockSnapshotConst
 from bt2.clock_snapshot import _UnknownClockSnapshot
 from bt2.component import _SourceComponentClass
 from bt2.component import _FilterComponentClass
index 953a2185b2d8c3926d1b57873c959237379d4b2c..3e5297bcc359375ab93a5ba79def235ac70c4657 100644 (file)
@@ -48,15 +48,66 @@ class ClockClassOffset:
         return (self.seconds, self.cycles) == (other.seconds, other.cycles)
 
 
-class _ClockClass(object._SharedObject):
+class _ClockClassConst(object._SharedObject):
     _get_ref = staticmethod(native_bt.clock_class_get_ref)
     _put_ref = staticmethod(native_bt.clock_class_put_ref)
+    _create_value_from_ptr_and_get_ref = staticmethod(
+        bt2_value._create_from_const_ptr_and_get_ref
+    )
 
     @property
     def user_attributes(self):
         ptr = native_bt.clock_class_borrow_user_attributes(self._ptr)
         assert ptr is not None
-        return bt2_value._create_from_ptr_and_get_ref(ptr)
+        return self._create_value_from_ptr_and_get_ref(ptr)
+
+    @property
+    def name(self):
+        return native_bt.clock_class_get_name(self._ptr)
+
+    @property
+    def description(self):
+        return native_bt.clock_class_get_description(self._ptr)
+
+    @property
+    def frequency(self):
+        return native_bt.clock_class_get_frequency(self._ptr)
+
+    @property
+    def precision(self):
+        precision = native_bt.clock_class_get_precision(self._ptr)
+        return precision
+
+    @property
+    def offset(self):
+        offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr)
+        return ClockClassOffset(offset_s, offset_cycles)
+
+    @property
+    def origin_is_unix_epoch(self):
+        return native_bt.clock_class_origin_is_unix_epoch(self._ptr)
+
+    @property
+    def uuid(self):
+        uuid_bytes = native_bt.clock_class_get_uuid(self._ptr)
+
+        if uuid_bytes is None:
+            return
+
+        return uuidp.UUID(bytes=uuid_bytes)
+
+    def cycles_to_ns_from_origin(self, cycles):
+        utils._check_uint64(cycles)
+        status, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles)
+        error_msg = "cannot convert clock value to nanoseconds from origin for given clock class"
+        utils._handle_func_status(status, error_msg)
+        return ns
+
+
+class _ClockClass(_ClockClassConst):
+    _create_value_from_ptr_and_get_ref = staticmethod(
+        bt2_value._create_from_ptr_and_get_ref
+    )
 
     def _user_attributes(self, user_attributes):
         value = bt2_value.create_value(user_attributes)
@@ -65,10 +116,6 @@ class _ClockClass(object._SharedObject):
 
     _user_attributes = property(fset=_user_attributes)
 
-    @property
-    def name(self):
-        return native_bt.clock_class_get_name(self._ptr)
-
     def _name(self, name):
         utils._check_str(name)
         status = native_bt.clock_class_set_name(self._ptr, name)
@@ -76,10 +123,6 @@ class _ClockClass(object._SharedObject):
 
     _name = property(fset=_name)
 
-    @property
-    def description(self):
-        return native_bt.clock_class_get_description(self._ptr)
-
     def _description(self, description):
         utils._check_str(description)
         status = native_bt.clock_class_set_description(self._ptr, description)
@@ -87,42 +130,24 @@ class _ClockClass(object._SharedObject):
 
     _description = property(fset=_description)
 
-    @property
-    def frequency(self):
-        return native_bt.clock_class_get_frequency(self._ptr)
-
     def _frequency(self, frequency):
         utils._check_uint64(frequency)
         native_bt.clock_class_set_frequency(self._ptr, frequency)
 
     _frequency = property(fset=_frequency)
 
-    @property
-    def precision(self):
-        precision = native_bt.clock_class_get_precision(self._ptr)
-        return precision
-
     def _precision(self, precision):
         utils._check_uint64(precision)
         native_bt.clock_class_set_precision(self._ptr, precision)
 
     _precision = property(fset=_precision)
 
-    @property
-    def offset(self):
-        offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr)
-        return ClockClassOffset(offset_s, offset_cycles)
-
     def _offset(self, offset):
         utils._check_type(offset, ClockClassOffset)
         native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles)
 
     _offset = property(fset=_offset)
 
-    @property
-    def origin_is_unix_epoch(self):
-        return native_bt.clock_class_origin_is_unix_epoch(self._ptr)
-
     def _origin_is_unix_epoch(self, origin_is_unix_epoch):
         utils._check_bool(origin_is_unix_epoch)
         native_bt.clock_class_set_origin_is_unix_epoch(
@@ -131,24 +156,8 @@ class _ClockClass(object._SharedObject):
 
     _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch)
 
-    @property
-    def uuid(self):
-        uuid_bytes = native_bt.clock_class_get_uuid(self._ptr)
-
-        if uuid_bytes is None:
-            return
-
-        return uuidp.UUID(bytes=uuid_bytes)
-
     def _uuid(self, uuid):
         utils._check_type(uuid, uuidp.UUID)
         native_bt.clock_class_set_uuid(self._ptr, uuid.bytes)
 
     _uuid = property(fset=_uuid)
-
-    def cycles_to_ns_from_origin(self, cycles):
-        utils._check_uint64(cycles)
-        status, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles)
-        error_msg = "cannot convert clock value to nanoseconds from origin for given clock class"
-        utils._handle_func_status(status, error_msg)
-        return ns
index 1ec41299e524eabf365637ec2539b639dee465b5..d2da481b9136f10abfd1d89e6779fbafa2b7a972 100644 (file)
@@ -27,12 +27,12 @@ import functools
 
 
 @functools.total_ordering
-class _ClockSnapshot(object._UniqueObject):
+class _ClockSnapshotConst(object._UniqueObject):
     @property
     def clock_class(self):
         cc_ptr = native_bt.clock_snapshot_borrow_clock_class_const(self._ptr)
         assert cc_ptr is not None
-        return bt2_clock_class._ClockClass._create_from_ptr_and_get_ref(cc_ptr)
+        return bt2_clock_class._ClockClassConst._create_from_ptr_and_get_ref(cc_ptr)
 
     @property
     def value(self):
index bf6cf84d8f4a1f275dbade2c2505081218e3b9a2..6e3ab761513e054e8caa23f0ebd4e0cd48aad501 100644 (file)
@@ -48,7 +48,7 @@ class _MessageWithDefaultClockSnapshot:
     def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr):
         snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr)
 
-        return bt2_clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
+        return bt2_clock_snapshot._ClockSnapshotConst._create_from_ptr_and_get_ref(
             snapshot_ptr, self._ptr, self._get_ref, self._put_ref
         )
 
@@ -115,7 +115,7 @@ class _StreamMessage(_Message, _MessageWithDefaultClockSnapshot):
         if status == native_bt.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN:
             return bt2_clock_snapshot._UnknownClockSnapshot()
 
-        return bt2_clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
+        return bt2_clock_snapshot._ClockSnapshotConst._create_from_ptr_and_get_ref(
             snapshot_ptr, self._ptr, self._get_ref, self._put_ref
         )
 
index 5788e80cb1cc77a46fb159d22563843b2f551093..208e8b29f253eec99797e31bf6727b664c1316ef 100644 (file)
@@ -20,6 +20,8 @@ import unittest
 import uuid
 import bt2
 from utils import run_in_component_init, TestOutputPortMessageIterator
+from bt2 import value as bt2_value
+from bt2 import clock_class as bt2_clock_class
 
 
 class ClockClassOffsetTestCase(unittest.TestCase):
@@ -205,6 +207,7 @@ class ClockClassTestCase(unittest.TestCase):
 
         cc = run_in_component_init(f)
         self.assertEqual(cc.user_attributes, {'salut': 23})
+        self.assertIs(type(cc.user_attributes), bt2_value.MapValue)
 
     def test_create_invalid_user_attributes(self):
         def f(comp_self):
@@ -285,9 +288,9 @@ class ClockSnapshotTestCase(unittest.TestCase):
         self.assertEqual(self._msg.default_clock_snapshot.value, 123)
 
     def test_clock_class(self):
-        self.assertEqual(
-            self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr
-        )
+        cc = self._msg.default_clock_snapshot.clock_class
+        self.assertEqual(cc.addr, self._cc.addr)
+        self.assertIs(type(cc), bt2_clock_class._ClockClassConst)
 
     def test_ns_from_origin(self):
         s_from_origin = 45 + ((354 + 123) / 1000)
index 21ffe08446098a6e1fb3675c3eff5c6d4615166e..52d5dc67bbcb9bd355010a85f3fa50507c2a5035 100644 (file)
@@ -27,8 +27,8 @@ class PackageTestCase(unittest.TestCase):
     def test_has_ClockClassOffset(self):
         self._assert_in_bt2('ClockClassOffset')
 
-    def test_has__ClockSnapshot(self):
-        self._assert_in_bt2('_ClockSnapshot')
+    def test_has__ClockSnapshotConst(self):
+        self._assert_in_bt2('_ClockSnapshotConst')
 
     def test_has__UnknownClockSnapshot(self):
         self._assert_in_bt2('_UnknownClockSnapshot')
This page took 0.029793 seconds and 4 git commands to generate.