Skip to content

Commit f76472c

Browse files
committed
Add some base exceptions
1 parent 0f22c1a commit f76472c

6 files changed

Lines changed: 39 additions & 0 deletions

File tree

tests/snippets/try_exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,18 @@
77
print(type(ex))
88
# print(ex.__traceback__)
99
# print(type(ex.__traceback__))
10+
11+
12+
try:
13+
assert 0
14+
except:
15+
print('boom')
16+
finally:
17+
print('kablam')
18+
19+
try:
20+
assert 0
21+
except AssertionError as ex:
22+
print('boom', type(ex))
23+
finally:
24+
print('kablam')

vm/src/builtins.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,26 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
354354
dict.insert(String::from("tuple"), ctx.tuple_type.clone());
355355
dict.insert(String::from("type"), ctx.type_type.clone());
356356
dict.insert(String::from("object"), ctx.object.clone());
357+
358+
// Exceptions:
357359
dict.insert(
358360
String::from("BaseException"),
359361
ctx.base_exception_type.clone(),
360362
);
363+
let exception_type = ctx.new_class(&String::from("Exception"), ctx.base_exception_type.clone());
364+
dict.insert(String::from("Exception"), exception_type.clone());
365+
let assertion_error = ctx.new_class(&String::from("AssertionError"), exception_type.clone());
366+
dict.insert(String::from("AssertionError"), assertion_error);
367+
let attribute_error = ctx.new_class(&String::from("AttributeError"), exception_type.clone());
368+
dict.insert(String::from("AttributeError"), attribute_error);
369+
let name_error = ctx.new_class(&String::from("NameError"), exception_type.clone());
370+
dict.insert(String::from("NameError"), name_error);
371+
let runtime_error = ctx.new_class(&String::from("RuntimeError"), exception_type.clone());
372+
dict.insert(String::from("RuntimeError"), runtime_error.clone());
373+
let not_implemented_error =
374+
ctx.new_class(&String::from("NotImplementedError"), runtime_error.clone());
375+
dict.insert(String::from("NotImplementedError"), not_implemented_error);
376+
361377
let d2 = PyObject::new(PyObjectKind::Dict { elements: dict }, ctx.type_type.clone());
362378
let scope = PyObject::new(
363379
PyObjectKind::Scope {

vm/src/compile.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ impl Compiler {
423423
self.emit(Instruction::CallFunction { count: 0 });
424424
}
425425
}
426+
self.emit(Instruction::Raise { argc: 1 });
426427
self.set_label(end_label);
427428
}
428429
ast::Statement::Break => {

vm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod objstr;
2323
mod objtype;
2424
pub mod pyobject;
2525
mod sysmodule;
26+
mod traceback;
2627
mod vm;
2728

2829
// pub use self::pyobject::Executor;

vm/src/pyobject.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ impl PyContext {
194194
)
195195
}
196196

197+
pub fn new_class(&self, name: &String, base: PyObjectRef) -> PyObjectRef {
198+
objtype::new(self.type_type.clone(), name, vec![base], self.new_dict()).unwrap()
199+
}
200+
197201
pub fn new_scope(&self, parent: Option<PyObjectRef>) -> PyObjectRef {
198202
let locals = self.new_dict();
199203
let scope = Scope {

vm/src/traceback.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// python tracebacks
2+
//

0 commit comments

Comments
 (0)