From 536b3e0c26fbc93b867443db07911e8caf48f789 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sun, 23 Jan 2022 21:20:20 +0800 Subject: [PATCH] test: added test case for nested exceptions and try --- artiq/test/coredevice/test_portability.py | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/artiq/test/coredevice/test_portability.py b/artiq/test/coredevice/test_portability.py index a861b6833..21f73000a 100644 --- a/artiq/test/coredevice/test_portability.py +++ b/artiq/test/coredevice/test_portability.py @@ -125,6 +125,57 @@ class _Pulses(EnvExperiment): class _MyException(Exception): pass +class _NestedFinally(EnvExperiment): + def build(self, trace): + self.setattr_device("core") + self.trace = trace + + def _trace(self, i): + self.trace.append(i) + + @kernel + def run(self): + try: + try: + raise ValueError + finally: + try: + raise IndexError() + except ValueError: + self._trace(0) + except: + self._trace(1) + finally: + self._trace(2) + +class _NestedExceptions(EnvExperiment): + def build(self, trace): + self.setattr_device("core") + self.trace = trace + + def _trace(self, i): + self.trace.append(i) + + @kernel + def run(self): + try: + try: + raise ValueError + except _MyException: + self._trace(0) + raise + finally: + try: + raise IndexError() + except ValueError: + self._trace(1) + raise + except IndexError: + self._trace(2) + except: + self._trace(3) + finally: + self._trace(4) class _Exceptions(EnvExperiment): def build(self, trace): @@ -253,6 +304,18 @@ class HostVsDeviceCase(ExperimentCase): _run_on_host(_Exceptions, trace=t_host) self.assertEqual(t_device, t_host) + def test_nested_finally(self): + t_device, t_host = [], [] + self.execute(_NestedFinally, trace=t_device) + _run_on_host(_NestedFinally, trace=t_host) + self.assertEqual(t_device, t_host) + + def test_nested_exceptions(self): + t_device, t_host = [], [] + self.execute(_NestedExceptions, trace=t_device) + _run_on_host(_NestedExceptions, trace=t_host) + self.assertEqual(t_device, t_host) + def test_rpc_exceptions(self): for f in self.execute, _run_on_host: with self.assertRaises(_MyException):