From d54bef76921ae719bcbaf4ad7f9915da10f25574 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 20 Jan 2014 18:35:32 +0200 Subject: [PATCH] stream: Add generic unbuffered iternext method. Uses stream_unbuffered_readline underline. --- py/stream.c | 11 ++++++++++- py/stream.h | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/py/stream.c b/py/stream.c index d3a11affbc..3a87d0444c 100644 --- a/py/stream.c +++ b/py/stream.c @@ -137,9 +137,18 @@ static mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) { // TODO: \0 vstr_add_byte(vstr, 0); vstr_shrink(vstr); - return mp_obj_new_str(qstr_from_str_take(vstr_str(vstr), vstr_len(vstr))); + return MP_OBJ_NEW_QSTR(qstr_from_str_take(vstr_str(vstr), vstr_len(vstr))); } +mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { + mp_obj_t l_in = stream_unbuffered_readline(1, &self); + const char *l = qstr_str(MP_OBJ_QSTR_VALUE(l_in)); + // TODO: \0 + if (*l != 0) { + return l_in; + } + return mp_const_stop_iteration; +} MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); diff --git a/py/stream.h b/py/stream.h index 58e8072549..a0cc34797b 100644 --- a/py/stream.h +++ b/py/stream.h @@ -2,3 +2,6 @@ extern const mp_obj_fun_native_t mp_stream_read_obj; extern const mp_obj_fun_native_t mp_stream_readall_obj; extern const mp_obj_fun_native_t mp_stream_unbuffered_readline_obj; extern const mp_obj_fun_native_t mp_stream_write_obj; + +// Iterator which uses mp_stream_unbuffered_readline_obj +mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);