Fix: _EventConst.__getitem__(): check if event has a packet
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 7 Apr 2020 13:41:55 +0000 (09:41 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 8 Apr 2020 17:04:40 +0000 (13:04 -0400)
I'm also adding a test to check this. Without this patch and an invalid
key, __getitem__() throws `AttributeError` instead of the expected
`KeyError`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ie6258a2354ece8aee6c8530587c900ea08e45fe8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/3350
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/event.py
tests/bindings/python/bt2/test_event.py

index 08d6ae0949b108ccb9be366740ebb98fa4d6cb94..6a6c1185987517c8c930e32b0b545e19b0daf5a8 100644 (file)
@@ -123,10 +123,11 @@ class _EventConst(object._UniqueObject):
         if common_context_field is not None and key in common_context_field:
             return common_context_field[key]
 
-        packet_context_field = self.packet.context_field
+        if self.packet:
+            packet_context_field = self.packet.context_field
 
-        if packet_context_field is not None and key in packet_context_field:
-            return packet_context_field[key]
+            if packet_context_field is not None and key in packet_context_field:
+                return packet_context_field[key]
 
         raise KeyError(key)
 
index a770c8f5d58c4316b6f5de9346e509783415b7c6..e8effef34535ea1cb55953db817a0a2803a1d56b 100644 (file)
@@ -331,6 +331,20 @@ class EventTestCase(unittest.TestCase):
         with self.assertRaises(KeyError):
             ev['yes']
 
+    def test_const_getitem_no_packet(self):
+        def event_fields_config(event):
+            event.payload_field['giraffe'] = 1
+            event.payload_field['gnu'] = 23
+            event.payload_field['mosquito'] = 42
+
+        msg = self._create_test_const_event_message(
+            event_fields_config=event_fields_config, with_ep=True,
+        )
+        ev = msg.event
+
+        with self.assertRaises(KeyError):
+            ev['yes']
+
     def test_getitem(self):
         msg = utils.get_event_message()
         ev = msg.event
This page took 0.025523 seconds and 4 git commands to generate.