mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
fix: add flush method to BytesIO (#7235)
This commit is contained in:
1 parent
4c2537010d
commit
09cf49204b
2 files changed
+34
No files matched your search
@@ -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)?;
|
||||
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user