Skip to content

Commit 1519426

Browse files
committed
Support raising exceptions without call
1 parent 40c1a0f commit 1519426

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

tests/snippets/try_exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
# print(ex.__traceback__)
99
# print(type(ex.__traceback__))
1010

11+
try:
12+
raise ZeroDivisionError
13+
except ZeroDivisionError as ex:
14+
pass
15+
16+
class E(Exception):
17+
def __init__(self):
18+
asdf
19+
20+
try:
21+
raise E
22+
except NameError as ex:
23+
pass
1124

1225
l = []
1326
try:

vm/src/frame.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ impl Frame {
515515
if objtype::isinstance(&exception, &vm.ctx.exceptions.base_exception_type) {
516516
info!("Exception raised: {:?}", exception);
517517
Err(exception)
518+
} else if objtype::isinstance(&exception, &vm.ctx.type_type())
519+
&& objtype::issubclass(&exception, &vm.ctx.exceptions.base_exception_type)
520+
{
521+
let exception = vm.new_empty_exception(exception)?;
522+
info!("Exception raised: {:?}", exception);
523+
Err(exception)
518524
} else {
519525
let msg = format!(
520526
"Can only raise BaseException derived types, not {}",

vm/src/vm.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,17 @@ impl VirtualMachine {
107107
self.ctx.new_dict()
108108
}
109109

110+
pub fn new_empty_exception(&mut self, exc_type: PyObjectRef) -> PyResult {
111+
info!("New exception created: no msg");
112+
let args = PyFuncArgs {
113+
args: vec![],
114+
kwargs: vec![],
115+
};
116+
self.invoke(exc_type, args)
117+
}
118+
110119
pub fn new_exception(&mut self, exc_type: PyObjectRef, msg: String) -> PyObjectRef {
120+
// TODO: exc_type may be user-defined exception, so we should return PyResult
111121
// TODO: maybe there is a clearer way to create an instance:
112122
info!("New exception created: {}", msg);
113123
let pymsg = self.new_str(msg);

0 commit comments

Comments
 (0)