micropython/tests/extmod/ujson_dump_iobase.py
David Lechner 3dc324d3f1 tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
2020-03-30 13:21:58 +11:00

35 lines
688 B
Python

# test ujson.dump in combination with uio.IOBase
try:
import uio as io
import ujson as json
except ImportError:
try:
import io, json
except ImportError:
print("SKIP")
raise SystemExit
if not hasattr(io, "IOBase"):
print("SKIP")
raise SystemExit
# a user stream that only has the write method
class S(io.IOBase):
def __init__(self):
self.buf = ""
def write(self, buf):
if type(buf) == bytearray:
# uPy passes a bytearray, CPython passes a str
buf = str(buf, "ascii")
self.buf += buf
return len(buf)
# dump to the user stream
s = S()
json.dump([123, {}], s)
print(s.buf)