From: Simon Marchi Date: Wed, 24 Jul 2019 02:38:20 +0000 (-0400) Subject: bt2: remove NonexistentClockSnapshot exception type X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=1153eccbbc9fcacffeffd3d39556f250f12bce00 bt2: remove NonexistentClockSnapshot exception type The NonexistentClockSnapshot exception type is used when trying to get a clock snapshot from an object that doesn't have clock snapshots (because of the way it's configured). It does not have much value over using a standard exception type. Replace it with ValueError. In the test add some validation about the error message, because it would be possible for the test to be a false positive if some ValueError is raised for another reason (a programming error on our part, for example). Change-Id: Iebf86bc53e578407bb60f2145246dbd110cafaac Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/1753 Reviewed-by: Philippe Proulx Tested-by: jenkins --- diff --git a/src/bindings/python/bt2/bt2/__init__.py.in b/src/bindings/python/bt2/bt2/__init__.py.in index 8de6ba80..4ceb37d1 100644 --- a/src/bindings/python/bt2/bt2/__init__.py.in +++ b/src/bindings/python/bt2/bt2/__init__.py.in @@ -105,10 +105,6 @@ class Canceled(Exception): pass -class NonexistentClockSnapshot(Exception): - pass - - class _LoadingError(_Error): pass diff --git a/src/bindings/python/bt2/bt2/message.py b/src/bindings/python/bt2/bt2/message.py index aeb9ebd8..5a1ca32d 100644 --- a/src/bindings/python/bt2/bt2/message.py +++ b/src/bindings/python/bt2/bt2/message.py @@ -40,7 +40,7 @@ class _Message(object._SharedObject): @staticmethod def _check_has_default_clock_class(clock_class): if clock_class is None: - raise bt2.NonexistentClockSnapshot( + raise ValueError( 'cannot get default clock snapshot: stream class has no default clock class' ) @@ -180,7 +180,7 @@ class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot): def _check_has_default_clock_snapshots(self): if not self._has_default_clock_snapshots: - raise bt2.NonexistentClockSnapshot( + raise ValueError( 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class' ) diff --git a/tests/bindings/python/bt2/test_event.py b/tests/bindings/python/bt2/test_event.py index 74fe7cff..ab65e0fa 100644 --- a/tests/bindings/python/bt2/test_event.py +++ b/tests/bindings/python/bt2/test_event.py @@ -229,7 +229,9 @@ class EventTestCase(unittest.TestCase): def test_no_clock_value(self): msg = self._create_test_event_message(with_clockclass=False) - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, 'stream class has no default clock class' + ): msg.default_clock_snapshot def test_stream(self): diff --git a/tests/bindings/python/bt2/test_message.py b/tests/bindings/python/bt2/test_message.py index 5b513361..3b89337a 100644 --- a/tests/bindings/python/bt2/test_message.py +++ b/tests/bindings/python/bt2/test_message.py @@ -214,7 +214,9 @@ class AllMessagesTestCase(unittest.TestCase): if i == 0: self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) self.assertEqual(msg.stream.addr, self._stream.addr) - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, 'stream class has no default clock class' + ): msg.default_clock_snapshot elif i == 1: self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) @@ -222,16 +224,24 @@ class AllMessagesTestCase(unittest.TestCase): elif i == 2: self.assertIsInstance(msg, bt2.message._EventMessage) self.assertEqual(msg.event.cls.addr, self._event_class.addr) - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, 'stream class has no default clock class' + ): msg.default_clock_snapshot elif i == 3: self.assertIsInstance(msg, bt2.message._DiscardedEventsMessage) self.assertEqual(msg.stream.addr, self._stream.addr) self.assertEqual(msg.count, 890) self.assertIsNone(msg.stream.cls.default_clock_class) - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, + 'such a message has no clock snapshots for this stream class', + ): msg.beginning_default_clock_snapshot - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, + 'such a message has no clock snapshots for this stream class', + ): msg.end_default_clock_snapshot elif i == 4: self.assertIsInstance(msg, bt2.message._PacketEndMessage) @@ -241,14 +251,22 @@ class AllMessagesTestCase(unittest.TestCase): self.assertEqual(msg.stream.addr, self._stream.addr) self.assertEqual(msg.count, 678) self.assertIsNone(msg.stream.cls.default_clock_class) - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, + 'such a message has no clock snapshots for this stream class', + ): msg.beginning_default_clock_snapshot - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, + 'such a message has no clock snapshots for this stream class', + ): msg.end_default_clock_snapshot elif i == 6: self.assertIsInstance(msg, bt2.message._StreamEndMessage) self.assertEqual(msg.stream.addr, self._stream.addr) - with self.assertRaises(bt2.NonexistentClockSnapshot): + with self.assertRaisesRegex( + ValueError, 'stream class has no default clock class' + ): msg.default_clock_snapshot else: raise Exception