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
2 changes: 0 additions & 2 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,6 @@ def test_unmached_quote(self):
self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
self.assertEqual(b'', out)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_stdout_flush_at_shutdown(self):
# Issue #5319: if stdout.flush() fails at shutdown, an error should
# be printed out.
Expand Down
17 changes: 14 additions & 3 deletions crates/vm/src/vm/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use core::sync::atomic::Ordering;

type InitFunc = Box<dyn FnOnce(&mut VirtualMachine)>;

/// Exit code used when stdout/stderr flush fails during interpreter shutdown.
/// Matches CPython's behavior (see cpython/Python/pylifecycle.c).
const EXITCODE_FLUSH_FAILURE: u32 = 120;

/// Configuration builder for constructing an Interpreter.
///
/// This is the preferred way to configure and create an interpreter with custom modules.
Expand Down Expand Up @@ -401,7 +405,7 @@ impl Interpreter {
/// Note that calling `finalize` is not necessary by purpose though.
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32 {
self.enter(|vm| {
vm.flush_std();
let mut flush_status = vm.flush_std();

// See if any exception leaked out:
let exit_code = if let Some(exc) = exc {
Expand Down Expand Up @@ -439,9 +443,16 @@ impl Interpreter {
// (while builtins is still available for __del__), then clear module dicts.
vm.finalize_modules();

vm.flush_std();
if vm.flush_std() < 0 && flush_status == 0 {
flush_status = -1;
}

exit_code
// Match CPython: if exit_code is 0 and stdout flush failed, exit 120
if exit_code == 0 && flush_status < 0 {
EXITCODE_FLUSH_FAILURE
} else {
exit_code
}
})
}
}
Expand Down
25 changes: 21 additions & 4 deletions crates/vm/src/vm/vm_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,31 @@ impl VirtualMachine {
}
}

pub(crate) fn flush_std(&self) {
/// Returns true if the file object's `closed` attribute is truthy.
fn file_is_closed(&self, file: &PyObject) -> bool {
file.get_attr("closed", self)
.ok()
.is_some_and(|v| v.try_to_bool(self).unwrap_or(false))
}

pub(crate) fn flush_std(&self) -> i32 {
let vm = self;
if let Ok(stdout) = sys::get_stdout(vm) {
let _ = vm.call_method(&stdout, identifier!(vm, flush).as_str(), ());
let mut status = 0;
if let Ok(stdout) = sys::get_stdout(vm)
&& !vm.is_none(&stdout)
&& !vm.file_is_closed(&stdout)
&& let Err(e) = vm.call_method(&stdout, identifier!(vm, flush).as_str(), ())
{
vm.run_unraisable(e, None, stdout);
status = -1;
}
if let Ok(stderr) = sys::get_stderr(vm) {
if let Ok(stderr) = sys::get_stderr(vm)
&& !vm.is_none(&stderr)
&& !vm.file_is_closed(&stderr)
{
let _ = vm.call_method(&stderr, identifier!(vm, flush).as_str(), ());
}
status
}

#[track_caller]
Expand Down
Loading