Skip to content

Commit e5ca631

Browse files
authored
Add bare ExceptionGroup (#5259)
1 parent 3313fde commit e5ca631

File tree

4 files changed

+10
-17
lines changed

4 files changed

+10
-17
lines changed

Lib/test/test_exception_group.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ def test_bad_EG_construction__nested_non_exceptions(self):
7272

7373

7474
class InstanceCreation(unittest.TestCase):
75-
# TODO: RUSTPYTHON
76-
@unittest.expectedFailure
7775
def test_EG_wraps_Exceptions__creates_EG(self):
7876
excs = [ValueError(1), TypeError(2)]
7977
self.assertIs(
@@ -99,8 +97,6 @@ def test_BEG_wraps_BaseException__creates_BEG(self):
9997
beg = BaseExceptionGroup("beg", [ValueError(1), KeyboardInterrupt(2)])
10098
self.assertIs(type(beg), BaseExceptionGroup)
10199

102-
# TODO: RUSTPYTHON
103-
@unittest.expectedFailure
104100
def test_EG_subclass_wraps_non_base_exceptions(self):
105101
class MyEG(ExceptionGroup):
106102
pass
@@ -245,8 +241,6 @@ def create_simple_eg():
245241

246242

247243
class ExceptionGroupFields(unittest.TestCase):
248-
# TODO: RUSTPYTHON
249-
@unittest.expectedFailure
250244
def test_basics_ExceptionGroup_fields(self):
251245
eg = create_simple_eg()
252246

@@ -510,8 +504,6 @@ class LeafGeneratorTest(unittest.TestCase):
510504
# on how to iterate over leaf nodes of an EG. It is also
511505
# used below as a test utility. So we test it here.
512506

513-
# TODO: RUSTPYTHON
514-
@unittest.expectedFailure
515507
def test_leaf_generator(self):
516508
eg = create_simple_eg()
517509

@@ -549,25 +541,19 @@ def create_nested_eg():
549541

550542

551543
class NestedExceptionGroupBasicsTest(ExceptionGroupTestBase):
552-
# TODO: RUSTPYTHON
553-
@unittest.expectedFailure
554544
def test_nested_group_matches_template(self):
555545
eg = create_nested_eg()
556546
self.assertMatchesTemplate(
557547
eg,
558548
ExceptionGroup,
559549
[[TypeError(bytes)], ValueError(1)])
560550

561-
# TODO: RUSTPYTHON
562-
@unittest.expectedFailure
563551
def test_nested_group_chaining(self):
564552
eg = create_nested_eg()
565553
self.assertIsInstance(eg.exceptions[1].__context__, MemoryError)
566554
self.assertIsInstance(eg.exceptions[1].__cause__, MemoryError)
567555
self.assertIsInstance(eg.exceptions[0].__context__, TypeError)
568556

569-
# TODO: RUSTPYTHON
570-
@unittest.expectedFailure
571557
def test_nested_exception_group_tracebacks(self):
572558
eg = create_nested_eg()
573559

@@ -581,8 +567,6 @@ def test_nested_exception_group_tracebacks(self):
581567
self.assertEqual(tb.tb_lineno, expected)
582568
self.assertIsNone(tb.tb_next)
583569

584-
# TODO: RUSTPYTHON
585-
@unittest.expectedFailure
586570
def test_iteration_full_tracebacks(self):
587571
eg = create_nested_eg()
588572
# check that iteration over leaves

Lib/test/test_pickle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def test_exceptions(self):
633633
StopAsyncIteration,
634634
RecursionError,
635635
EncodingWarning,
636-
#ExceptionGroup, # TODO: RUSTPYTHON
636+
ExceptionGroup,
637637
BaseExceptionGroup):
638638
continue
639639
if exc is not OSError and issubclass(exc, OSError):

vm/src/exceptions.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ impl ExceptionCtor {
332332
pub struct ExceptionZoo {
333333
pub base_exception_type: &'static Py<PyType>,
334334
pub base_exception_group: &'static Py<PyType>,
335+
pub exception_group: &'static Py<PyType>,
335336
pub system_exit: &'static Py<PyType>,
336337
pub keyboard_interrupt: &'static Py<PyType>,
337338
pub generator_exit: &'static Py<PyType>,
@@ -560,6 +561,7 @@ impl ExceptionZoo {
560561

561562
// Sorted By Hierarchy then alphabetized.
562563
let base_exception_group = PyBaseExceptionGroup::init_builtin_type();
564+
let exception_group = PyExceptionGroup::init_builtin_type();
563565
let system_exit = PySystemExit::init_builtin_type();
564566
let keyboard_interrupt = PyKeyboardInterrupt::init_builtin_type();
565567
let generator_exit = PyGeneratorExit::init_builtin_type();
@@ -646,6 +648,7 @@ impl ExceptionZoo {
646648
Self {
647649
base_exception_type,
648650
base_exception_group,
651+
exception_group,
649652
system_exit,
650653
keyboard_interrupt,
651654
generator_exit,
@@ -731,6 +734,7 @@ impl ExceptionZoo {
731734
"message" => ctx.new_readonly_getset("message", excs.base_exception_group, make_arg_getter(0)),
732735
"exceptions" => ctx.new_readonly_getset("exceptions", excs.base_exception_group, make_arg_getter(1)),
733736
});
737+
extend_exception!(PyExceptionGroup, ctx, excs.exception_group);
734738
extend_exception!(PySystemExit, ctx, excs.system_exit, {
735739
"code" => ctx.new_readonly_getset("code", excs.system_exit, system_exit_code),
736740
});
@@ -1083,6 +1087,10 @@ pub(super) mod types {
10831087
#[derive(Debug)]
10841088
pub struct PyBaseExceptionGroup {}
10851089

1090+
#[pyexception(name, base = "PyBaseExceptionGroup", ctx = "exception_group", impl)]
1091+
#[derive(Debug)]
1092+
pub struct PyExceptionGroup {}
1093+
10861094
#[pyexception(name, base = "PyBaseException", ctx = "generator_exit", impl)]
10871095
#[derive(Debug)]
10881096
pub struct PyGeneratorExit {}

vm/src/stdlib/builtins.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ pub fn init_module(vm: &VirtualMachine, module: &Py<PyModule>) {
10061006
// Exceptions:
10071007
"BaseException" => ctx.exceptions.base_exception_type.to_owned(),
10081008
"BaseExceptionGroup" => ctx.exceptions.base_exception_group.to_owned(),
1009+
"ExceptionGroup" => ctx.exceptions.exception_group.to_owned(),
10091010
"SystemExit" => ctx.exceptions.system_exit.to_owned(),
10101011
"KeyboardInterrupt" => ctx.exceptions.keyboard_interrupt.to_owned(),
10111012
"GeneratorExit" => ctx.exceptions.generator_exit.to_owned(),

0 commit comments

Comments
 (0)