From: Simon Marchi Date: Wed, 5 Jun 2019 18:11:37 +0000 (-0400) Subject: bt2: change some bt2.CreationError usages to ValueError X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=4430bc809d78701c128ced642fb4e9597bc00ad0 bt2: change some bt2.CreationError usages to ValueError The bt2.CreationError exception type is reserved for cases where the creation functions return None/NULL, which is usually because of exhausted memory. Other exception types should be used for other errors. This patch fixes a few instances where bt2.CreationError is wrongly used. Change them for ValueError, since they are cases of wrong parameter value passed by the user. We have already used ValueError for cases like these, for example in _UserMessageIterator._create_packet_beginning_message. Change-Id: Ib66943f8dc1200f7b589764c171bf4764741a6bd Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/1383 Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/bindings/python/bt2/bt2/stream_class.py b/bindings/python/bt2/bt2/stream_class.py index f2869cb9..fa13cb3b 100644 --- a/bindings/python/bt2/bt2/stream_class.py +++ b/bindings/python/bt2/bt2/stream_class.py @@ -61,12 +61,12 @@ class StreamClass(object._SharedObject, collections.abc.Mapping): payload_field_class=None): if self.assigns_automatic_event_class_id: if id is not None: - raise bt2.CreationError('id provided, but stream class assigns automatic event class ids') + raise ValueError('id provided, but stream class assigns automatic event class ids') ec_ptr = native_bt.event_class_create(self._ptr) else: if id is None: - raise bt2.CreationError('id not provided, but stream class does not assign automatic event class ids') + raise ValueError('id not provided, but stream class does not assign automatic event class ids') utils._check_uint64(id) ec_ptr = native_bt.event_class_create_with_id(self._ptr, id) diff --git a/bindings/python/bt2/bt2/trace.py b/bindings/python/bt2/bt2/trace.py index aa2757c0..ba7b89d8 100644 --- a/bindings/python/bt2/bt2/trace.py +++ b/bindings/python/bt2/bt2/trace.py @@ -79,12 +79,12 @@ class Trace(object._SharedObject, collections.abc.Mapping): if stream_class.assigns_automatic_stream_id: if id is not None: - raise bt2.CreationError("id provided, but stream class assigns automatic stream ids") + raise ValueError("id provided, but stream class assigns automatic stream ids") stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) else: if id is None: - raise bt2.CreationError("id not provided, but stream class does not assign automatic stream ids") + raise ValueError("id not provided, but stream class does not assign automatic stream ids") utils._check_uint64(id) stream_ptr = native_bt.stream_create_with_id(stream_class._ptr, self._ptr, id) diff --git a/bindings/python/bt2/bt2/trace_class.py b/bindings/python/bt2/bt2/trace_class.py index 9ae57ade..b8eeb30f 100644 --- a/bindings/python/bt2/bt2/trace_class.py +++ b/bindings/python/bt2/bt2/trace_class.py @@ -176,12 +176,12 @@ class TraceClass(object._SharedObject, collections.abc.Mapping): if self.assigns_automatic_stream_class_id: if id is not None: - raise bt2.CreationError('id provided, but trace class assigns automatic stream class ids') + raise ValueError('id provided, but trace class assigns automatic stream class ids') sc_ptr = native_bt.stream_class_create(self._ptr) else: if id is None: - raise bt2.CreationError('id not provided, but trace class does not assign automatic stream class ids') + raise ValueError('id not provided, but trace class does not assign automatic stream class ids') utils._check_uint64(id) sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) diff --git a/tests/bindings/python/bt2/test_stream_class.py b/tests/bindings/python/bt2/test_stream_class.py index 3eb695b1..243563f5 100644 --- a/tests/bindings/python/bt2/test_stream_class.py +++ b/tests/bindings/python/bt2/test_stream_class.py @@ -70,7 +70,7 @@ class StreamClassTestCase(unittest.TestCase): sc = self._tc.create_stream_class(assigns_automatic_stream_id=True) self.assertTrue(sc.assigns_automatic_stream_id) - with self.assertRaises(bt2.CreationError): + with self.assertRaises(ValueError): self._trace.create_stream(sc, id=123) def test_no_automatic_stream_ids(self): @@ -84,7 +84,7 @@ class StreamClassTestCase(unittest.TestCase): sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) self.assertFalse(sc.assigns_automatic_stream_id) - with self.assertRaises(bt2.CreationError): + with self.assertRaises(ValueError): self._trace.create_stream(sc) def test_automatic_event_class_ids(self): @@ -98,7 +98,7 @@ class StreamClassTestCase(unittest.TestCase): sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True) self.assertTrue(sc.assigns_automatic_event_class_id) - with self.assertRaises(bt2.CreationError): + with self.assertRaises(ValueError): sc.create_event_class(id=123) def test_no_automatic_event_class_ids(self): @@ -112,7 +112,7 @@ class StreamClassTestCase(unittest.TestCase): sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) self.assertFalse(sc.assigns_automatic_event_class_id) - with self.assertRaises(bt2.CreationError): + with self.assertRaises(ValueError): sc.create_event_class() def test_packets_have_default_beginning_clock_snapshot(self): diff --git a/tests/bindings/python/bt2/test_trace_class.py b/tests/bindings/python/bt2/test_trace_class.py index 265b0de8..4f19b4a2 100644 --- a/tests/bindings/python/bt2/test_trace_class.py +++ b/tests/bindings/python/bt2/test_trace_class.py @@ -45,7 +45,7 @@ class TraceClassTestCase(unittest.TestCase): tc = run_in_component_init(f) self.assertTrue(tc.assigns_automatic_stream_class_id) - with self.assertRaises(bt2.CreationError): + with self.assertRaises(ValueError): sc1 = tc.create_stream_class(23) def test_no_assigns_automatic_stream_class_id(self): @@ -66,7 +66,7 @@ class TraceClassTestCase(unittest.TestCase): self.assertFalse(tc.assigns_automatic_stream_class_id) # In this mode, it is required to pass an explicit id. - with self.assertRaises(bt2.CreationError): + with self.assertRaises(ValueError): tc.create_stream_class() def test_env_get(self):