summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-07 17:48:53 +1100
committerDamien George <damien.p.george@gmail.com>2018-04-10 13:41:32 +1000
commitcf31d384f1d2ca09f817f154892eaa6860c10144 (patch)
tree27b6dacefc471a394636e86ac787f6dd29f4a809 /py
parent8f11d0b532bd0203c875ad9e3e4efd8a7014ca15 (diff)
py/stream: Switch stream close operation from method to ioctl.
This patch moves the implementation of stream closure from a dedicated method to the ioctl of the stream protocol, for each type that implements closing. The benefits of this are: 1. Rounds out the stream ioctl function, which already includes flush, seek and poll (among other things). 2. Makes calling mp_stream_close() on an object slightly more efficient because it now no longer needs to lookup the close method and call it, rather it just delegates straight to the ioctl function (if it exists). 3. Reduces code size and allows future types that implement the stream protocol to be smaller because they don't need a dedicated close method. Code size reduction is around 200 bytes smaller for x86 archs and around 30 bytes smaller for the bare-metal archs.
Diffstat (limited to 'py')
-rw-r--r--py/objstringio.c30
-rw-r--r--py/stream.c18
-rw-r--r--py/stream.h3
3 files changed, 26 insertions, 25 deletions
diff --git a/py/objstringio.c b/py/objstringio.c
index 5c50aa317..b405ee21e 100644
--- a/py/objstringio.c
+++ b/py/objstringio.c
@@ -143,6 +143,17 @@ STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
}
case MP_STREAM_FLUSH:
return 0;
+ case MP_STREAM_CLOSE:
+ #if MICROPY_CPYTHON_COMPAT
+ vstr_free(o->vstr);
+ o->vstr = NULL;
+ #else
+ vstr_clear(o->vstr);
+ o->vstr->alloc = 0;
+ o->vstr->len = 0;
+ o->pos = 0;
+ #endif
+ return 0;
default:
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;
@@ -159,24 +170,9 @@ STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
-STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
- mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
-#if MICROPY_CPYTHON_COMPAT
- vstr_free(self->vstr);
- self->vstr = NULL;
-#else
- vstr_clear(self->vstr);
- self->vstr->alloc = 0;
- self->vstr->len = 0;
- self->pos = 0;
-#endif
- return mp_const_none;
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
-
STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
- return stringio_close(args[0]);
+ return mp_stream_close(args[0]);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
@@ -233,7 +229,7 @@ STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
- { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) },
+ { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
diff --git a/py/stream.c b/py/stream.c
index 453dee769..f51f634b6 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -105,13 +105,6 @@ 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);
@@ -434,6 +427,17 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
return MP_OBJ_STOP_ITERATION;
}
+mp_obj_t mp_stream_close(mp_obj_t stream) {
+ const mp_stream_p_t *stream_p = mp_get_stream_raise(stream, MP_STREAM_OP_IOCTL);
+ int error;
+ mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
+ if (res == MP_STREAM_ERROR) {
+ mp_raise_OSError(error);
+ }
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
+
STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
diff --git a/py/stream.h b/py/stream.h
index fbe3d7d85..a7d8d08f1 100644
--- a/py/stream.h
+++ b/py/stream.h
@@ -35,7 +35,7 @@
#define MP_STREAM_FLUSH (1)
#define MP_STREAM_SEEK (2)
#define MP_STREAM_POLL (3)
-//#define MP_STREAM_CLOSE (4) // Not yet implemented
+#define MP_STREAM_CLOSE (4)
#define MP_STREAM_TIMEOUT (5) // Get/set timeout (single op)
#define MP_STREAM_GET_OPTS (6) // Get stream options
#define MP_STREAM_SET_OPTS (7) // Set stream options
@@ -69,6 +69,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_write_obj);
MP_DECLARE_CONST_FUN_OBJ_2(mp_stream_write1_obj);
+MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_close_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_tell_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_flush_obj);