Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ extern crate log;
use clap::{App, AppSettings, Arg, ArgMatches};
use rustpython_compiler::compile;
use rustpython_vm::{
exceptions::print_exception,
match_class,
obj::{objint::PyInt, objtuple::PyTuple, objtype},
print_exception,
obj::{objint::PyInt, objtype},
pyobject::{ItemProtocol, PyResult},
scope::Scope,
util, InitParameter, PySettings, VirtualMachine,
Expand Down Expand Up @@ -51,8 +51,7 @@ fn main() {
// See if any exception leaked out:
if let Err(err) = res {
if objtype::isinstance(&err, &vm.ctx.exceptions.system_exit) {
let args = vm.get_attribute(err.clone(), "args").unwrap();
let args = args.downcast::<PyTuple>().expect("'args' must be a tuple");
let args = err.args();
match args.elements.len() {
0 => return,
1 => match_class!(match args.elements[0].clone() {
Expand Down
11 changes: 5 additions & 6 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ mod rustyline_helper;
use rustpython_compiler::{compile, error::CompileError, error::CompileErrorType};
use rustpython_parser::error::ParseErrorType;
use rustpython_vm::{
exceptions::{print_exception, PyBaseExceptionRef},
obj::objtype,
print_exception,
pyobject::{ItemProtocol, PyObjectRef, PyResult},
pyobject::{ItemProtocol, PyResult},
scope::Scope,
VirtualMachine,
};
Expand All @@ -16,7 +16,7 @@ use readline::{Readline, ReadlineResult};

enum ShellExecResult {
Ok,
PyErr(PyObjectRef),
PyErr(PyBaseExceptionRef),
Continue,
}

Expand Down Expand Up @@ -118,9 +118,8 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
ReadlineResult::Interrupt => {
continuing = false;
full_input.clear();
let keyboard_interrupt = vm
.new_empty_exception(vm.ctx.exceptions.keyboard_interrupt.clone())
.unwrap();
let keyboard_interrupt =
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.clone());
Err(keyboard_interrupt)
}
ReadlineResult::EOF => {
Expand Down
18 changes: 17 additions & 1 deletion tests/snippets/try_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,23 @@ def y():
except ZeroDivisionError as ex:
raise NameError from ex
except NameError as ex2:
pass
assert isinstance(ex2.__cause__, ZeroDivisionError)
else:
assert False, "no raise"


try:
try:
try:
raise ZeroDivisionError
except ZeroDivisionError as ex:
raise NameError from ex
except NameError:
raise
except NameError as ex2:
assert isinstance(ex2.__cause__, ZeroDivisionError)
else:
assert False, "no raise"


# the else clause requires at least one except clause:
Expand Down
2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ chrono = { version = "=0.4.9", features = ["wasmbind"] }
unicode-xid = "0.2.0"
lazy_static = "^1.0.1"
lexical = "4"
itertools = "^0.8.0"
itertools = "0.8"
hex = "0.4.0"
hexf-parse = "0.1.0"
indexmap = "1.0.2"
Expand Down
5 changes: 3 additions & 2 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use num_traits::{Signed, ToPrimitive, Zero};
#[cfg(feature = "rustpython-compiler")]
use rustpython_compiler::compile;

use crate::exceptions::PyBaseExceptionRef;
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
use crate::obj::objbool::{self, IntoPyBool};
use crate::obj::objbyteinner::PyByteInner;
Expand Down Expand Up @@ -290,7 +291,7 @@ fn builtin_format(
})
}

fn catch_attr_exception<T>(ex: PyObjectRef, default: T, vm: &VirtualMachine) -> PyResult<T> {
fn catch_attr_exception<T>(ex: PyBaseExceptionRef, default: T, vm: &VirtualMachine) -> PyResult<T> {
if objtype::isinstance(&ex, &vm.ctx.exceptions.attribute_error) {
Ok(default)
} else {
Expand Down Expand Up @@ -628,7 +629,7 @@ impl Printer for std::io::StdoutLock<'_> {

pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
Err(vm.new_exception_obj(vm.ctx.exceptions.system_exit.clone(), vec![code])?)
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
}

pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
Expand Down
Loading