Skip to content

Commit 5bf6eba

Browse files
committed
tests/open_plus: Add tests for "r+", "w+" open modes.
1 parent a63a476 commit 5bf6eba

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tests/io/open_plus.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
try:
3+
import uos as os
4+
except ImportError:
5+
import os
6+
7+
if not hasattr(os, "unlink"):
8+
print("SKIP")
9+
sys.exit()
10+
11+
# cleanup in case testfile exists
12+
try:
13+
os.unlink("testfile")
14+
except OSError:
15+
pass
16+
17+
try:
18+
f = open("testfile", "r+b")
19+
print("Unexpectedly opened non-existing file")
20+
except OSError:
21+
print("Expected OSError")
22+
pass
23+
24+
f = open("testfile", "w+b")
25+
f.write(b"1234567890")
26+
f.seek(0)
27+
print(f.read())
28+
f.close()
29+
30+
# Open with truncation
31+
f = open("testfile", "w+b")
32+
f.write(b"abcdefg")
33+
f.seek(0)
34+
print(f.read())
35+
f.close()
36+
37+
# Open without truncation
38+
f = open("testfile", "r+b")
39+
f.write(b"1234")
40+
f.seek(0)
41+
print(f.read())
42+
f.close()

0 commit comments

Comments
 (0)