From 497660fcda77ae819f5ea1f6a92b8fb5006d8213 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 20 May 2016 21:16:58 +0300 Subject: [PATCH] py/stream: Add mp_stream_close() helper function. --- py/stream.c | 8 ++++++++ py/stream.h | 1 + 2 files changed, 9 insertions(+) diff --git a/py/stream.c b/py/stream.c index 9b1d5fd2de..ebdbe26b45 100644 --- a/py/stream.c +++ b/py/stream.c @@ -31,6 +31,7 @@ #include "py/nlr.h" #include "py/objstr.h" #include "py/stream.h" +#include "py/runtime.h" #if MICROPY_STREAMS_NON_BLOCK #include @@ -104,6 +105,13 @@ const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) { return stream_p; } +mp_obj_t mp_stream_close(mp_obj_t stream) { + // TODO: Still consider using ioctl for close + mp_obj_t dest[2]; + mp_load_method(stream, MP_QSTR_close, dest); + return mp_call_method_n_kw(0, 0, dest); +} + STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) { const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ); diff --git a/py/stream.h b/py/stream.h index 9202c64f31..b0f45e2f02 100644 --- a/py/stream.h +++ b/py/stream.h @@ -65,6 +65,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_ioctl_obj); #define MP_STREAM_OP_IOCTL (4) const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags); +mp_obj_t mp_stream_close(mp_obj_t stream); // Iterator which uses mp_stream_unbuffered_readline_obj mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);