Skip to content

Commit 81654d4

Browse files
committed
More exception types
1 parent 155da36 commit 81654d4

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

vm/src/exceptions.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,20 @@ extends_exception! {
591591
"Common base class for all non-exit exceptions."
592592
}
593593

594+
extends_exception! {
595+
PyGeneratorExit,
596+
PyBaseException,
597+
generator_exit,
598+
"Request that a generator exit."
599+
}
600+
601+
extends_exception! {
602+
PySystemExit,
603+
PyBaseException,
604+
system_exit,
605+
"Request to exit from the interpreter."
606+
}
607+
594608
extends_exception! {
595609
PyKeyboardInterrupt,
596610
PyBaseException,
@@ -608,6 +622,20 @@ extends_exception! {
608622
"Inappropriate argument type."
609623
}
610624

625+
extends_exception! {
626+
PyStopAsyncIteration,
627+
PyException,
628+
stop_async_iteration,
629+
"Signal the end from iterator.__anext__()."
630+
}
631+
632+
extends_exception! {
633+
PyStopIteration,
634+
PyException,
635+
stop_iteration,
636+
"Signal the end from iterator.__next__()."
637+
}
638+
611639
extends_exception! {
612640
PyOSError,
613641
PyException,
@@ -617,16 +645,16 @@ extends_exception! {
617645

618646
impl ExceptionZoo {
619647
pub(crate) fn init() -> Self {
648+
// The same order as definitions:
620649
let base_exception_type = PyBaseException::init_bare_type().clone();
650+
let exception_type = PyException::init_bare_type().clone();
621651

622-
// Sorted By Hierarchy then alphabetized.
623-
let system_exit = create_exception_type("SystemExit", &base_exception_type);
652+
let system_exit = PySystemExit::init_bare_type().clone();
624653
let keyboard_interrupt = PyKeyboardInterrupt::init_bare_type().clone();
625-
let generator_exit = create_exception_type("GeneratorExit", &base_exception_type);
654+
let generator_exit = PyGeneratorExit::init_bare_type().clone();
626655

627-
let exception_type = PyException::init_bare_type().clone();
628-
let stop_iteration = create_exception_type("StopIteration", &exception_type);
629-
let stop_async_iteration = create_exception_type("StopAsyncIteration", &exception_type);
656+
let stop_iteration = PyStopIteration::init_bare_type().clone();
657+
let stop_async_iteration = PyStopAsyncIteration::init_bare_type().clone();
630658
let arithmetic_error = create_exception_type("ArithmeticError", &exception_type);
631659
let floating_point_error = create_exception_type("FloatingPointError", &arithmetic_error);
632660
let overflow_error = create_exception_type("OverflowError", &arithmetic_error);
@@ -776,9 +804,20 @@ impl ExceptionZoo {
776804

777805
PyBaseException::extend_class(ctx, &excs.base_exception_type);
778806
PyException::extend_class(ctx, &excs.exception_type);
807+
808+
PyGeneratorExit::extend_class(ctx, &excs.generator_exit);
809+
PySystemExit::extend_class(ctx, &excs.system_exit);
810+
extend_class!(ctx, &excs.system_exit, {
811+
"code" => ctx.new_readonly_getset("code", excs.system_exit.clone(), system_exit_code),
812+
});
779813
PyKeyboardInterrupt::extend_class(ctx, &excs.keyboard_interrupt);
780814

781815
PyTypeError::extend_class(ctx, &excs.type_error);
816+
PyStopAsyncIteration::extend_class(ctx, &excs.stop_async_iteration);
817+
PyStopIteration::extend_class(ctx, &excs.stop_iteration);
818+
extend_class!(ctx, &excs.stop_iteration, {
819+
"value" => ctx.new_readonly_getset("value", excs.stop_iteration.clone(), make_arg_getter(0)),
820+
});
782821

783822
extend_class!(ctx, &excs.syntax_error, {
784823
"msg" => ctx.new_readonly_getset("msg", excs.syntax_error.clone(), make_arg_getter(0)),
@@ -789,19 +828,11 @@ impl ExceptionZoo {
789828
"text" => ctx.none(),
790829
});
791830

792-
extend_class!(ctx, &excs.system_exit, {
793-
"code" => ctx.new_readonly_getset("code", excs.system_exit.clone(), system_exit_code),
794-
});
795-
796831
extend_class!(ctx, &excs.import_error, {
797832
"__init__" => ctx.new_method("__init__", excs.import_error.clone(), import_error_init),
798833
"msg" => ctx.new_readonly_getset("msg", excs.import_error.clone(), make_arg_getter(0)),
799834
});
800835

801-
extend_class!(ctx, &excs.stop_iteration, {
802-
"value" => ctx.new_readonly_getset("value", excs.stop_iteration.clone(), make_arg_getter(0)),
803-
});
804-
805836
extend_class!(ctx, &excs.key_error, {
806837
"__str__" => ctx.new_method("__str__", excs.key_error.clone(), key_error_str),
807838
});

0 commit comments

Comments
 (0)