Skip to content

Commit deee1f7

Browse files
authored
Merge pull request RustPython#1644 from RustPython/coolreader18/exc-cleanup
Use PyBaseExceptionRef in places where we expect exceptions
2 parents bd13671 + 181dff8 commit deee1f7

37 files changed

+506
-444
lines changed

Cargo.lock

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ extern crate log;
77
use clap::{App, AppSettings, Arg, ArgMatches};
88
use rustpython_compiler::compile;
99
use rustpython_vm::{
10+
exceptions::print_exception,
1011
match_class,
11-
obj::{objint::PyInt, objtuple::PyTuple, objtype},
12-
print_exception,
12+
obj::{objint::PyInt, objtype},
1313
pyobject::{ItemProtocol, PyResult},
1414
scope::Scope,
1515
util, InitParameter, PySettings, VirtualMachine,
@@ -51,8 +51,7 @@ fn main() {
5151
// See if any exception leaked out:
5252
if let Err(err) = res {
5353
if objtype::isinstance(&err, &vm.ctx.exceptions.system_exit) {
54-
let args = vm.get_attribute(err.clone(), "args").unwrap();
55-
let args = args.downcast::<PyTuple>().expect("'args' must be a tuple");
54+
let args = err.args();
5655
match args.elements.len() {
5756
0 => return,
5857
1 => match_class!(match args.elements[0].clone() {

src/shell.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ mod rustyline_helper;
55
use rustpython_compiler::{compile, error::CompileError, error::CompileErrorType};
66
use rustpython_parser::error::ParseErrorType;
77
use rustpython_vm::{
8+
exceptions::{print_exception, PyBaseExceptionRef},
89
obj::objtype,
9-
print_exception,
10-
pyobject::{ItemProtocol, PyObjectRef, PyResult},
10+
pyobject::{ItemProtocol, PyResult},
1111
scope::Scope,
1212
VirtualMachine,
1313
};
@@ -16,7 +16,7 @@ use readline::{Readline, ReadlineResult};
1616

1717
enum ShellExecResult {
1818
Ok,
19-
PyErr(PyObjectRef),
19+
PyErr(PyBaseExceptionRef),
2020
Continue,
2121
}
2222

@@ -118,9 +118,8 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
118118
ReadlineResult::Interrupt => {
119119
continuing = false;
120120
full_input.clear();
121-
let keyboard_interrupt = vm
122-
.new_empty_exception(vm.ctx.exceptions.keyboard_interrupt.clone())
123-
.unwrap();
121+
let keyboard_interrupt =
122+
vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.clone());
124123
Err(keyboard_interrupt)
125124
}
126125
ReadlineResult::EOF => {

tests/snippets/try_exceptions.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,23 @@ def y():
250250
except ZeroDivisionError as ex:
251251
raise NameError from ex
252252
except NameError as ex2:
253-
pass
253+
assert isinstance(ex2.__cause__, ZeroDivisionError)
254+
else:
255+
assert False, "no raise"
256+
257+
258+
try:
259+
try:
260+
try:
261+
raise ZeroDivisionError
262+
except ZeroDivisionError as ex:
263+
raise NameError from ex
264+
except NameError:
265+
raise
266+
except NameError as ex2:
267+
assert isinstance(ex2.__cause__, ZeroDivisionError)
268+
else:
269+
assert False, "no raise"
254270

255271

256272
# the else clause requires at least one except clause:

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ chrono = { version = "=0.4.9", features = ["wasmbind"] }
4949
unicode-xid = "0.2.0"
5050
lazy_static = "^1.0.1"
5151
lexical = "4"
52-
itertools = "^0.8.0"
52+
itertools = "0.8"
5353
hex = "0.4.0"
5454
hexf-parse = "0.1.0"
5555
indexmap = "1.0.2"

vm/src/builtins.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use num_traits::{Signed, ToPrimitive, Zero};
1212
#[cfg(feature = "rustpython-compiler")]
1313
use rustpython_compiler::compile;
1414

15+
use crate::exceptions::PyBaseExceptionRef;
1516
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
1617
use crate::obj::objbool::{self, IntoPyBool};
1718
use crate::obj::objbyteinner::PyByteInner;
@@ -290,7 +291,7 @@ fn builtin_format(
290291
})
291292
}
292293

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

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

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

0 commit comments

Comments
 (0)