Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
stdio_errors, stdio_encodings
  • Loading branch information
youknowone committed Jan 1, 2026
commit d69583bedba392463fe2ea5029be1ca7489a952e
16 changes: 12 additions & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,25 @@ 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 {
let encoding = self.state.config.settings.stdio_encoding.as_deref();
// stderr always uses backslashreplace (ignores stdio_errors)
let errors = if fd == 2 {
Some("backslashreplace")
} else {
None
self.state.config.settings.stdio_errors.as_deref()
};

let stdio = self.call_method(
&io,
"TextIOWrapper",
(buf, (), errors, newline, line_buffering, write_through),
(
buf,
encoding,
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
8 changes: 6 additions & 2 deletions crates/vm/src/vm/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ pub struct Settings {
/// -u, PYTHONUNBUFFERED=x
pub buffered_stdio: bool,

// wchar_t *stdio_encoding;
/// PYTHONIOENCODING - stdio encoding
pub stdio_encoding: Option<String>,
/// PYTHONIOENCODING - stdio error handler
pub stdio_errors: Option<String>,
pub utf8_mode: u8,
// wchar_t *stdio_errors;
/// --check-hash-based-pycs
pub check_hash_pycs_mode: CheckHashPycsMode,

Expand Down Expand Up @@ -197,6 +199,8 @@ impl Default for Settings {
buffered_stdio: true,
check_hash_pycs_mode: CheckHashPycsMode::Default,
allow_external_library: cfg!(feature = "importlib"),
stdio_encoding: None,
stdio_errors: None,
utf8_mode: 1,
int_max_str_digits: 4300,
#[cfg(feature = "flame-it")]
Expand Down
17 changes: 17 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@ pub fn parse_opts() -> Result<(Settings, RunMode), lexopt::Error> {
settings.code_debug_ranges = false;
}

// Parse PYTHONIOENCODING=encoding[:errors]
if let Some(val) = get_env("PYTHONIOENCODING")
&& let Some(val_str) = val.to_str()
&& !val_str.is_empty()
{
if let Some((enc, err)) = val_str.split_once(':') {
if !enc.is_empty() {
settings.stdio_encoding = Some(enc.to_owned());
}
if !err.is_empty() {
settings.stdio_errors = Some(err.to_owned());
}
} else {
settings.stdio_encoding = Some(val_str.to_owned());
}
}

if settings.dev_mode {
settings.warnoptions.push("default".to_owned());
settings.faulthandler = true;
Expand Down