From 221dfc2e0208e193398d099e9dfd17565af18f55 Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Fri, 27 Feb 2026 21:04:47 +0900 Subject: [PATCH] Fix IOBase.isatty() to raise ValueError on closed files (#7233) * Fix IOBase.isatty() to raise ValueError on closed files * Add snippet test for IOBase.isatty() closed check --- crates/vm/src/stdlib/io.rs | 5 +++-- extra_tests/snippets/stdlib_io.py | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/vm/src/stdlib/io.rs b/crates/vm/src/stdlib/io.rs index 11ad6d101..808dfec1e 100644 --- a/crates/vm/src/stdlib/io.rs +++ b/crates/vm/src/stdlib/io.rs @@ -477,8 +477,9 @@ mod _io { } #[pymethod] - fn isatty(_self: PyObjectRef) -> bool { - false + fn isatty(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult { + check_closed(&instance, vm)?; + Ok(false) } #[pygetset] diff --git a/extra_tests/snippets/stdlib_io.py b/extra_tests/snippets/stdlib_io.py index 7c4739082..f1e682b5d 100644 --- a/extra_tests/snippets/stdlib_io.py +++ b/extra_tests/snippets/stdlib_io.py @@ -1,5 +1,5 @@ import os -from io import BufferedReader, BytesIO, FileIO, StringIO +from io import BufferedReader, BytesIO, FileIO, RawIOBase, StringIO from testutils import assert_raises @@ -40,3 +40,20 @@ with FileIO("README.md") as fio: assert len(nres) == 1 nres = fio.read(2) assert len(nres) == 2 + +# Test that IOBase.isatty() raises ValueError when called on a closed file. +# Minimal subclass that inherits IOBase.isatty() without overriding it. +class MinimalRaw(RawIOBase): + def readinto(self, b): + return 0 + + +f = MinimalRaw() +assert not f.closed +assert not f.isatty() # open file: should return False + +f.close() +assert f.closed + +with assert_raises(ValueError): + f.isatty()