From 43d0a9135bfaf8c0439ee88fcf5508db05d45886 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Fri, 24 Nov 2023 11:28:40 +1300 Subject: [PATCH] Handle Python 3.12 behaviour. --- test/test_framework.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/test_framework.py b/test/test_framework.py index 00a075d01..145ecdf89 100644 --- a/test/test_framework.py +++ b/test/test_framework.py @@ -537,16 +537,26 @@ class MyEvents(ops.ObjectEvents): with self.assertRaises(RuntimeError) as cm: class OtherEvents(ops.ObjectEvents): # type: ignore foo = event + # Python 3.12+ raises the original exception with a note, but earlier + # Python chains the exceptions. + if hasattr(cm.exception, "__notes__"): + cause = str(cm.exception) + else: + cause = str(cm.exception.__cause__) self.assertEqual( - str(cm.exception.__cause__), + cause, "EventSource(MyEvent) reused as MyEvents.foo and OtherEvents.foo") with self.assertRaises(RuntimeError) as cm: class MyNotifier(ops.Object): # type: ignore on = MyEvents() # type: ignore bar = event + if hasattr(cm.exception, "__notes__"): + cause = str(cm.exception) + else: + cause = str(cm.exception.__cause__) self.assertEqual( - str(cm.exception.__cause__), + cause, "EventSource(MyEvent) reused as MyEvents.foo and MyNotifier.bar") def test_reemit_ignores_unknown_event_type(self):