From beb7a8dcfdaec2dbf391d70002c2b81058659687 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 23 Apr 2024 15:32:18 +0900 Subject: [PATCH] Add bare ExceptionGroup --- Lib/test/test_exception_group.py | 16 ---------------- Lib/test/test_pickle.py | 2 +- vm/src/exceptions.rs | 8 ++++++++ vm/src/stdlib/builtins.rs | 1 + 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py index c867ed861d..03c721d56f 100644 --- a/Lib/test/test_exception_group.py +++ b/Lib/test/test_exception_group.py @@ -72,8 +72,6 @@ def test_bad_EG_construction__nested_non_exceptions(self): class InstanceCreation(unittest.TestCase): - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_EG_wraps_Exceptions__creates_EG(self): excs = [ValueError(1), TypeError(2)] self.assertIs( @@ -99,8 +97,6 @@ def test_BEG_wraps_BaseException__creates_BEG(self): beg = BaseExceptionGroup("beg", [ValueError(1), KeyboardInterrupt(2)]) self.assertIs(type(beg), BaseExceptionGroup) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_EG_subclass_wraps_non_base_exceptions(self): class MyEG(ExceptionGroup): pass @@ -245,8 +241,6 @@ def create_simple_eg(): class ExceptionGroupFields(unittest.TestCase): - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_basics_ExceptionGroup_fields(self): eg = create_simple_eg() @@ -510,8 +504,6 @@ class LeafGeneratorTest(unittest.TestCase): # on how to iterate over leaf nodes of an EG. It is also # used below as a test utility. So we test it here. - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_leaf_generator(self): eg = create_simple_eg() @@ -549,8 +541,6 @@ def create_nested_eg(): class NestedExceptionGroupBasicsTest(ExceptionGroupTestBase): - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_nested_group_matches_template(self): eg = create_nested_eg() self.assertMatchesTemplate( @@ -558,16 +548,12 @@ def test_nested_group_matches_template(self): ExceptionGroup, [[TypeError(bytes)], ValueError(1)]) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_nested_group_chaining(self): eg = create_nested_eg() self.assertIsInstance(eg.exceptions[1].__context__, MemoryError) self.assertIsInstance(eg.exceptions[1].__cause__, MemoryError) self.assertIsInstance(eg.exceptions[0].__context__, TypeError) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_nested_exception_group_tracebacks(self): eg = create_nested_eg() @@ -581,8 +567,6 @@ def test_nested_exception_group_tracebacks(self): self.assertEqual(tb.tb_lineno, expected) self.assertIsNone(tb.tb_next) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_iteration_full_tracebacks(self): eg = create_nested_eg() # check that iteration over leaves diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py index 9450d8c08e..769677f242 100644 --- a/Lib/test/test_pickle.py +++ b/Lib/test/test_pickle.py @@ -633,7 +633,7 @@ def test_exceptions(self): StopAsyncIteration, RecursionError, EncodingWarning, - #ExceptionGroup, # TODO: RUSTPYTHON + ExceptionGroup, BaseExceptionGroup): continue if exc is not OSError and issubclass(exc, OSError): diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index ffa714195d..c726a073ea 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -332,6 +332,7 @@ impl ExceptionCtor { pub struct ExceptionZoo { pub base_exception_type: &'static Py, pub base_exception_group: &'static Py, + pub exception_group: &'static Py, pub system_exit: &'static Py, pub keyboard_interrupt: &'static Py, pub generator_exit: &'static Py, @@ -560,6 +561,7 @@ impl ExceptionZoo { // Sorted By Hierarchy then alphabetized. let base_exception_group = PyBaseExceptionGroup::init_builtin_type(); + let exception_group = PyExceptionGroup::init_builtin_type(); let system_exit = PySystemExit::init_builtin_type(); let keyboard_interrupt = PyKeyboardInterrupt::init_builtin_type(); let generator_exit = PyGeneratorExit::init_builtin_type(); @@ -646,6 +648,7 @@ impl ExceptionZoo { Self { base_exception_type, base_exception_group, + exception_group, system_exit, keyboard_interrupt, generator_exit, @@ -731,6 +734,7 @@ impl ExceptionZoo { "message" => ctx.new_readonly_getset("message", excs.base_exception_group, make_arg_getter(0)), "exceptions" => ctx.new_readonly_getset("exceptions", excs.base_exception_group, make_arg_getter(1)), }); + extend_exception!(PyExceptionGroup, ctx, excs.exception_group); extend_exception!(PySystemExit, ctx, excs.system_exit, { "code" => ctx.new_readonly_getset("code", excs.system_exit, system_exit_code), }); @@ -1083,6 +1087,10 @@ pub(super) mod types { #[derive(Debug)] pub struct PyBaseExceptionGroup {} + #[pyexception(name, base = "PyBaseExceptionGroup", ctx = "exception_group", impl)] + #[derive(Debug)] + pub struct PyExceptionGroup {} + #[pyexception(name, base = "PyBaseException", ctx = "generator_exit", impl)] #[derive(Debug)] pub struct PyGeneratorExit {} diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index bdbdc8b5ed..27b55bc6c9 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -1006,6 +1006,7 @@ pub fn init_module(vm: &VirtualMachine, module: &Py) { // Exceptions: "BaseException" => ctx.exceptions.base_exception_type.to_owned(), "BaseExceptionGroup" => ctx.exceptions.base_exception_group.to_owned(), + "ExceptionGroup" => ctx.exceptions.exception_group.to_owned(), "SystemExit" => ctx.exceptions.system_exit.to_owned(), "KeyboardInterrupt" => ctx.exceptions.keyboard_interrupt.to_owned(), "GeneratorExit" => ctx.exceptions.generator_exit.to_owned(),