summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-10-17 23:34:06 +0100
committerDamien George <damien.p.george@gmail.com>2014-10-17 23:34:06 +0100
commitb7a4b0f86f8f16394490441368adffce0404f75e (patch)
treee89cde197d3d8d57473563d38c3da3637923e18c
parent297d8469b8fcaa072630c9561695edb001d96232 (diff)
downloadmicropython-b7a4b0f86f8f16394490441368adffce0404f75e.tar.gz
micropython-b7a4b0f86f8f16394490441368adffce0404f75e.zip
py: Improve stream_read so it doesn't need to alloc 2 bits of heap.
-rw-r--r--py/stream.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/py/stream.c b/py/stream.c
index ca474ba83f..a0940599cd 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -166,7 +166,8 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
}
#endif
- byte *buf = m_new(byte, sz);
+ byte *buf;
+ mp_obj_t ret_obj = mp_obj_str_builder_start(STREAM_CONTENT_TYPE(o->type->stream_p), sz, &buf);
int error;
mp_uint_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
if (out_sz == MP_STREAM_ERROR) {
@@ -180,9 +181,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
}
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
} else {
- mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size
- m_free(buf, sz);
- return s;
+ return mp_obj_str_builder_end_with_len(ret_obj, out_sz);
}
}