Skip to content

Commit 846e38e

Browse files
committed
Fix stderr usage in exit handling
1 parent abdb04f commit 846e38e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,30 @@ where
106106
Ok(()) => 0,
107107
Err(err) if err.fast_isinstance(&vm.ctx.exceptions.system_exit) => {
108108
let args = err.args();
109-
match args.as_slice() {
110-
[] => 0,
109+
let exitcode = match args.as_slice() {
110+
[] => Ok(0),
111111
[arg] => match_class!(match arg {
112112
ref i @ PyInt => {
113113
use num_traits::cast::ToPrimitive;
114-
i.as_bigint().to_i32().unwrap_or(0)
114+
Ok(i.as_bigint().to_i32().unwrap_or(0))
115115
}
116116
arg => {
117117
if vm.is_none(arg) {
118-
0
118+
Ok(0)
119119
} else {
120-
if let Ok(s) = arg.str(vm) {
121-
eprintln!("{}", s);
122-
}
123-
1
120+
Err(arg.str(vm).ok())
124121
}
125122
}
126123
}),
127-
_ => {
128-
if let Ok(r) = args.as_object().repr(vm) {
129-
eprintln!("{}", r);
130-
}
131-
1
124+
_ => Err(args.as_object().repr(vm).ok()),
125+
};
126+
exitcode.unwrap_or_else(|msg| {
127+
if let Some(msg) = msg {
128+
let stderr = sys::PyStderr(vm);
129+
writeln!(stderr, "{}", msg);
132130
}
133-
}
131+
1
132+
})
134133
}
135134
Err(exc) => {
136135
vm.print_exception(exc);

vm/src/stdlib/sys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ mod sys {
501501
#[pyfunction(name = "__unraisablehook__")]
502502
fn unraisablehook(unraisable: UnraisableHookArgs, vm: &VirtualMachine) {
503503
if let Err(e) = _unraisablehook(unraisable, vm) {
504-
println!("{}", e.as_object().repr(vm).unwrap().as_str());
504+
let stderr = super::PyStderr(vm);
505+
writeln!(stderr, "{}", e.as_object().repr(vm).unwrap().as_str());
505506
}
506507
}
507508

0 commit comments

Comments
 (0)