fix: add flush method to BytesIO (#7235)

This commit is contained in:
Jiseok CHOI authored and GitHub committed 2026-02-27 16:53:17 +09:00
1 parent 4c2537010d
commit 09cf49204b
2 files changed
+34

No files matched your search

+9
View File
@@ -4595,6 +4595,15 @@ mod _io {
true
}
#[pymethod]
fn flush(&self, vm: &VirtualMachine) -> PyResult<()> {
if self.closed.load() {
Err(io_closed_error(vm))
} else {
Ok(())
}
}
#[pymethod]
fn write(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<u64> {
let mut buffer = self.try_resizable(vm)?;
+25
View File
@@ -74,6 +74,30 @@ def test_06():
assert f.readline() == b""
def test_07():
"""
Tests that flush() returns None when the file is open,
and raises ValueError when the file is closed.
CPython reference: Modules/_io/bytesio.c:325-335
"""
f = BytesIO(b"Test String 7")
# flush() on an open BytesIO returns None
assert f.flush() is None
# flush() is defined directly on BytesIO (not just inherited)
assert "flush" in BytesIO.__dict__
f.close()
# flush() on a closed BytesIO raises ValueError
try:
f.flush()
assert False, "Expected ValueError not raised"
except ValueError:
pass
if __name__ == "__main__":
test_01()
test_02()
@@ -81,3 +105,4 @@ if __name__ == "__main__":
test_04()
test_05()
test_06()
test_07()