mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
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:
1 parent
c5472e2a4e
commit
221dfc2e02
2 files changed
+21
-3
No files matched your search
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user