io_buffered_writer.py: Improve failed flush test.

Try flushing 4 times.

The first two times, the underlying IO object fails.
The third time, the data is actually flushed.
The last time, write is not called as there was no data left.

Signed-off-by: Jeff Epler <jepler@gmail.com>
This commit is contained in:
Jeff Epler 2025-06-16 17:18:49 +02:00
parent 162bd023a4
commit 3e400cf433
2 changed files with 21 additions and 6 deletions

View file

@ -31,13 +31,24 @@ print(type(hash(buf)))
# Test failing flush()
class MyIO(io.IOBase):
def __init__(self):
self.count = 0
def write(self, buf):
return None
self.count += 1
if self.count < 3:
return None
print("writing", buf)
return len(buf)
buf = io.BufferedWriter(MyIO(), 8)
buf.write(b"foobar")
try:
buf.flush()
print("flushed")
except OSError:
print("OSError")
for _ in range(4):
try:
buf.flush()
print("flushed")
except OSError:
print("OSError")

View file

@ -5,3 +5,7 @@ b'foobarfoobar'
b'foo'
<class 'int'>
OSError
OSError
writing bytearray(b'foobar')
flushed
flushed