Visitar URL original
enhance error codec and exit code by youknowone · Pull Request #6602 · RustPython/RustPython · GitHub
Skip to content
Merged
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
enhance error codec and exit code
  • Loading branch information
youknowone committed Dec 31, 2025
commit 8cf8029a3487ff8af8f9a3925817e5b31a147be8
23 changes: 19 additions & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,17 @@ impl VirtualMachine {
let line_buffering = buffered_stdio && (isatty || fd == 2);

let newline = if cfg!(windows) { None } else { Some("\n") };
// stderr uses backslashreplace error handler
let errors: Option<&str> = if fd == 2 {
Some("backslashreplace")
} else {
None
};

let stdio = self.call_method(
&io,
"TextIOWrapper",
(buf, (), (), newline, line_buffering, write_through),
(buf, (), errors, newline, line_buffering, write_through),
)?;
let mode = if write { "w" } else { "r" };
stdio.set_attr("mode", self.ctx.new_str(mode), self)?;
Expand Down Expand Up @@ -853,7 +859,13 @@ impl VirtualMachine {
[arg] => match_class!(match arg {
ref i @ PyInt => {
use num_traits::cast::ToPrimitive;
return i.as_bigint().to_u32().unwrap_or(0);
// Try u32 first, then i32 (for negative values), else -1 for overflow
let code = i
.as_bigint()
.to_u32()
.or_else(|| i.as_bigint().to_i32().map(|v| v as u32))
.unwrap_or(-1i32 as u32);
return code;
}
arg => {
if self.is_none(arg) {
Expand All @@ -866,8 +878,11 @@ impl VirtualMachine {
_ => args.as_object().repr(self).ok(),
};
if let Some(msg) = msg {
let stderr = stdlib::sys::PyStderr(self);
writeln!(stderr, "{msg}");
// Write using Python's write() to use stderr's error handler (backslashreplace)
if let Ok(stderr) = stdlib::sys::get_stderr(self) {
let _ = self.call_method(&stderr, "write", (msg,));
let _ = self.call_method(&stderr, "write", ("\n",));
}
}
1
} else if exc.fast_isinstance(self.ctx.exceptions.keyboard_interrupt) {
Expand Down
Loading