Fix stream version and add basic readinto test

This commit is contained in:
Scott Shawcroft 2020-07-10 17:33:17 -07:00
parent 734661e79c
commit 372bcf8a95
No known key found for this signature in database
GPG Key ID: 9349BC7E64B1921E
3 changed files with 28 additions and 0 deletions

View File

@ -127,6 +127,8 @@ STATIC mp_obj_t _mod_ujson_load(mp_obj_t stream_obj, bool return_first_json) {
stream_p = mp_get_stream_raise(stream_obj, MP_STREAM_OP_READ);
s.stream_obj = stream_obj;
s.read = stream_p->read;
s.errcode = 0;
s.cur = 0;
}
JSON_DEBUG("got JSON stream\n");

View File

@ -0,0 +1,22 @@
import ujson as json
# Test that json can load from any object with readinto
class Buffer:
def __init__(self, data):
self._data = data
self._i = 0
def readinto(self, buf):
end = self._i + len(buf)
remaining = len(self._data) - self._i
end = min(end, len(self._data))
l = min(len(buf), remaining)
buf[:l] = self._data[self._i:end]
self._i += l
return l
print(json.load(Buffer(b'null')))
print(json.load(Buffer(b'"abc\\u0064e"')))
print(json.load(Buffer(b'[false, true, 1, -2]')))
print(json.load(Buffer(b'{"a":true}')))

View File

@ -0,0 +1,4 @@
None
abcde
[False, True, 1, -2]
{'a': True}