Skip to content

Commit 117158f

Browse files
committed
tests: Add tests for stream IO errors.
1 parent 1c9210b commit 117158f

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

tests/io/file1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,35 @@
1212
print(f.readlines())
1313
f = open("io/data/file1",mode="rb")
1414
print(f.readlines())
15+
16+
# write() error
17+
f = open('io/data/file1', 'r')
18+
try:
19+
f.write('x')
20+
except OSError:
21+
print('OSError')
22+
f.close()
23+
24+
# read(n) error on binary file
25+
f = open('io/data/file1', 'ab')
26+
try:
27+
f.read(1)
28+
except OSError:
29+
print('OSError')
30+
f.close()
31+
32+
# read(n) error on text file
33+
f = open('io/data/file1', 'at')
34+
try:
35+
f.read(1)
36+
except OSError:
37+
print('OSError')
38+
f.close()
39+
40+
# readall() error (call read() for compat with CPy)
41+
f = open('io/data/file1', 'ab')
42+
try:
43+
f.read()
44+
except OSError:
45+
print('OSError')
46+
f.close()

tests/io/file_readinto.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
f = open("io/data/file2", "rb")
66
print(f.readinto(b))
77
print(b)
8+
9+
# readinto() on writable file
10+
f = open('io/data/file1', 'ab')
11+
try:
12+
f.readinto(bytearray(4))
13+
except OSError:
14+
print('OSError')

tests/io/file_readline.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@
44
print(f.readline(4))
55
print(f.readline(5))
66
print(f.readline())
7+
8+
# readline() on writable file
9+
f = open('io/data/file1', 'ab')
10+
try:
11+
f.readline()
12+
except OSError:
13+
print('OSError')
14+
f.close()

tests/io/file_seek.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@
2323
print(f.read(5))
2424
print(f.tell())
2525
f.close()
26+
27+
# seek closed file
28+
f = open('io/data/file1', 'r')
29+
f.close()
30+
try:
31+
f.seek(1)
32+
except (OSError, ValueError):
33+
# CPy raises ValueError, uPy raises OSError
34+
print('OSError or ValueError')

0 commit comments

Comments
 (0)