mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 19:20:22 +01:00
py/modio: Fix the case where write fails in BufferedWriter.flush.
Previously, there was no test coverage of the "write failed" path. In fact, the assertion would fire instead of gracefully raising a Python exception. Slightly re-organize the code to place the assertion later. Add a test case which exercises all paths, and update the expected output. Signed-off-by: Jeff Epler <jepler@gmail.com>
This commit is contained in:
committed by
Damien George
parent
5ade8b7058
commit
b6b7d64bd9
@@ -28,3 +28,27 @@ print(bts.getvalue())
|
||||
|
||||
# hashing a BufferedWriter
|
||||
print(type(hash(buf)))
|
||||
|
||||
# Test failing flush()
|
||||
class MyIO(io.IOBase):
|
||||
def __init__(self):
|
||||
self.count = 0
|
||||
|
||||
def write(self, buf):
|
||||
self.count += 1
|
||||
if self.count < 3:
|
||||
return None
|
||||
print("writing", buf)
|
||||
return len(buf)
|
||||
|
||||
|
||||
buf = io.BufferedWriter(MyIO(), 8)
|
||||
|
||||
buf.write(b"foobar")
|
||||
|
||||
for _ in range(4):
|
||||
try:
|
||||
buf.flush()
|
||||
print("flushed")
|
||||
except OSError:
|
||||
print("OSError")
|
||||
|
||||
@@ -4,3 +4,8 @@ b'foobarfoobar'
|
||||
b'foobarfoobar'
|
||||
b'foo'
|
||||
<class 'int'>
|
||||
OSError
|
||||
OSError
|
||||
writing bytearray(b'foobar')
|
||||
flushed
|
||||
flushed
|
||||
|
||||
Reference in New Issue
Block a user