2022-08-18 02:57:45 -04:00
|
|
|
# test json.dump in combination with io.IOBase
|
2018-06-12 23:13:36 -04:00
|
|
|
|
|
|
|
try:
|
2022-08-18 02:57:45 -04:00
|
|
|
import io, json
|
2018-06-12 23:13:36 -04:00
|
|
|
except ImportError:
|
2022-08-18 02:57:45 -04:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2018-06-12 23:13:36 -04:00
|
|
|
|
|
|
|
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 = ""
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-12 23:13:36 -04:00
|
|
|
def write(self, buf):
|
|
|
|
if type(buf) == bytearray:
|
|
|
|
# uPy passes a bytearray, CPython passes a str
|
|
|
|
buf = str(buf, "ascii")
|
|
|
|
self.buf += buf
|
2018-07-29 22:05:48 -04:00
|
|
|
return len(buf)
|
2018-06-12 23:13:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
# dump to the user stream
|
|
|
|
s = S()
|
|
|
|
json.dump([123, {}], s)
|
|
|
|
print(s.buf)
|