py, stream: Implement readlines for a stream.
This commit is contained in:
parent
5320bff32c
commit
d5f5b2f766
@ -314,6 +314,7 @@ Q(unpack)
|
|||||||
Q(io)
|
Q(io)
|
||||||
Q(readall)
|
Q(readall)
|
||||||
Q(readline)
|
Q(readline)
|
||||||
|
Q(readlines)
|
||||||
Q(StringIO)
|
Q(StringIO)
|
||||||
Q(BytesIO)
|
Q(BytesIO)
|
||||||
Q(getvalue)
|
Q(getvalue)
|
||||||
|
28
py/stream.c
28
py/stream.c
@ -123,8 +123,7 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
|
|||||||
while (max_size == -1 || max_size-- != 0) {
|
while (max_size == -1 || max_size-- != 0) {
|
||||||
char *p = vstr_add_len(vstr, 1);
|
char *p = vstr_add_len(vstr, 1);
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
// TODO
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
machine_int_t out_sz = o->type->stream_p->read(o, p, 1, &error);
|
machine_int_t out_sz = o->type->stream_p->read(o, p, 1, &error);
|
||||||
@ -143,16 +142,29 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO don't intern this string
|
// TODO need a string creation API that doesn't copy the given data
|
||||||
vstr_shrink(vstr);
|
mp_obj_t ret = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
|
||||||
return MP_OBJ_NEW_QSTR(qstr_from_strn_take(vstr_str(vstr), vstr->alloc, vstr_len(vstr)));
|
vstr_free(vstr);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO take an optional extra argument (what does it do exactly?)
|
||||||
|
STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
|
||||||
|
mp_obj_t lines = mp_obj_new_list(0, NULL);
|
||||||
|
for (;;) {
|
||||||
|
mp_obj_t line = stream_unbuffered_readline(1, &self);
|
||||||
|
if (mp_obj_str_get_len(line) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mp_obj_list_append(lines, line);
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
|
||||||
|
|
||||||
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
|
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
|
||||||
mp_obj_t l_in = stream_unbuffered_readline(1, &self);
|
mp_obj_t l_in = stream_unbuffered_readline(1, &self);
|
||||||
uint sz;
|
if (mp_obj_str_get_len(l_in) != 0) {
|
||||||
mp_obj_str_get_data(l_in, &sz);
|
|
||||||
if (sz != 0) {
|
|
||||||
return l_in;
|
return l_in;
|
||||||
}
|
}
|
||||||
return MP_OBJ_STOP_ITERATION;
|
return MP_OBJ_STOP_ITERATION;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
extern const mp_obj_fun_native_t mp_stream_read_obj;
|
MP_DECLARE_CONST_FUN_OBJ(mp_stream_read_obj);
|
||||||
extern const mp_obj_fun_native_t mp_stream_readall_obj;
|
MP_DECLARE_CONST_FUN_OBJ(mp_stream_readall_obj);
|
||||||
extern const mp_obj_fun_native_t mp_stream_unbuffered_readline_obj;
|
MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readline_obj);
|
||||||
extern const mp_obj_fun_native_t mp_stream_write_obj;
|
MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readlines_obj);
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
|
||||||
|
|
||||||
// Iterator which uses mp_stream_unbuffered_readline_obj
|
// Iterator which uses mp_stream_unbuffered_readline_obj
|
||||||
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
|
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
|
||||||
|
@ -51,6 +51,7 @@ STATIC const mp_map_elem_t file_locals_dict_table[] = {
|
|||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj},
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&file_obj_close_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&file_obj_close_obj },
|
||||||
|
@ -2,3 +2,5 @@ f = open("io/data/file1")
|
|||||||
print(f.read(5))
|
print(f.read(5))
|
||||||
print(f.readline())
|
print(f.readline())
|
||||||
print(f.read())
|
print(f.read())
|
||||||
|
f = open("io/data/file1")
|
||||||
|
print(f.readlines())
|
||||||
|
@ -132,6 +132,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
|
|||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj},
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&fdfile_close_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&fdfile_close_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
|
||||||
|
Loading…
Reference in New Issue
Block a user