Skip to content

Commit 48829cd

Browse files
committed
tests/extmod: Add test for ujson.dump writing to a user IOBase object.
1 parent 0ecce77 commit 48829cd

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/extmod/ujson_dump_iobase.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# test ujson.dump in combination with uio.IOBase
2+
3+
try:
4+
import uio as io
5+
import ujson as json
6+
except ImportError:
7+
try:
8+
import io, json
9+
except ImportError:
10+
print('SKIP')
11+
raise SystemExit
12+
13+
if not hasattr(io, 'IOBase'):
14+
print('SKIP')
15+
raise SystemExit
16+
17+
18+
# a user stream that only has the write method
19+
class S(io.IOBase):
20+
def __init__(self):
21+
self.buf = ''
22+
def write(self, buf):
23+
if type(buf) == bytearray:
24+
# uPy passes a bytearray, CPython passes a str
25+
buf = str(buf, 'ascii')
26+
self.buf += buf
27+
28+
29+
# dump to the user stream
30+
s = S()
31+
json.dump([123, {}], s)
32+
print(s.buf)

0 commit comments

Comments
 (0)