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
This commit is contained in:
Jiseok CHOI authored and GitHub committed 2026-02-27 21:04:47 +09:00
1 parent c5472e2a4e
commit 221dfc2e02
2 files changed
+21 -3

No files matched your search

+3 -2
View File
@@ -477,8 +477,9 @@ mod _io {
}
#[pymethod]
fn isatty(_self: PyObjectRef) -> bool {
false
fn isatty(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
check_closed(&instance, vm)?;
Ok(false)
}
#[pygetset]
+18 -1
View File
@@ -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()