Skip to content

Commit 09cf492

Browse files
authored
fix: add flush method to BytesIO (#7235)
1 parent 4c25370 commit 09cf492

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

crates/vm/src/stdlib/io.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4595,6 +4595,15 @@ mod _io {
45954595
true
45964596
}
45974597

4598+
#[pymethod]
4599+
fn flush(&self, vm: &VirtualMachine) -> PyResult<()> {
4600+
if self.closed.load() {
4601+
Err(io_closed_error(vm))
4602+
} else {
4603+
Ok(())
4604+
}
4605+
}
4606+
45984607
#[pymethod]
45994608
fn write(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<u64> {
46004609
let mut buffer = self.try_resizable(vm)?;

extra_tests/snippets/stdlib_io_bytesio.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,35 @@ def test_06():
7474
assert f.readline() == b""
7575

7676

77+
def test_07():
78+
"""
79+
Tests that flush() returns None when the file is open,
80+
and raises ValueError when the file is closed.
81+
CPython reference: Modules/_io/bytesio.c:325-335
82+
"""
83+
f = BytesIO(b"Test String 7")
84+
85+
# flush() on an open BytesIO returns None
86+
assert f.flush() is None
87+
88+
# flush() is defined directly on BytesIO (not just inherited)
89+
assert "flush" in BytesIO.__dict__
90+
91+
f.close()
92+
93+
# flush() on a closed BytesIO raises ValueError
94+
try:
95+
f.flush()
96+
assert False, "Expected ValueError not raised"
97+
except ValueError:
98+
pass
99+
100+
77101
if __name__ == "__main__":
78102
test_01()
79103
test_02()
80104
test_03()
81105
test_04()
82106
test_05()
83107
test_06()
108+
test_07()

0 commit comments

Comments
 (0)