Skip to content

Commit 372bcf8

Browse files
committed
Fix stream version and add basic readinto test
1 parent 734661e commit 372bcf8

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

extmod/modujson.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ STATIC mp_obj_t _mod_ujson_load(mp_obj_t stream_obj, bool return_first_json) {
127127
stream_p = mp_get_stream_raise(stream_obj, MP_STREAM_OP_READ);
128128
s.stream_obj = stream_obj;
129129
s.read = stream_p->read;
130+
s.errcode = 0;
131+
s.cur = 0;
130132
}
131133

132134
JSON_DEBUG("got JSON stream\n");

tests/extmod/ujson_load_readinto.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ujson as json
2+
3+
# Test that json can load from any object with readinto
4+
5+
class Buffer:
6+
def __init__(self, data):
7+
self._data = data
8+
self._i = 0
9+
10+
def readinto(self, buf):
11+
end = self._i + len(buf)
12+
remaining = len(self._data) - self._i
13+
end = min(end, len(self._data))
14+
l = min(len(buf), remaining)
15+
buf[:l] = self._data[self._i:end]
16+
self._i += l
17+
return l
18+
19+
print(json.load(Buffer(b'null')))
20+
print(json.load(Buffer(b'"abc\\u0064e"')))
21+
print(json.load(Buffer(b'[false, true, 1, -2]')))
22+
print(json.load(Buffer(b'{"a":true}')))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
None
2+
abcde
3+
[False, True, 1, -2]
4+
{'a': True}

0 commit comments

Comments
 (0)