summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2025-08-16 14:43:37 +0200
committerDamien George <damien@micropython.org>2025-09-11 12:42:24 +1000
commit66fb82e44e0beecb64225189ec8fb788b2076fb4 (patch)
treecfdddadd7b697385c0343b0c5b30a4efa3d50206 /py
parentc50f9cbb4244a4aef2a77098939af3b7eb7410b3 (diff)
py/stream: Add a stream.readinto1() method for machine.UART.
Avoiding the double timeout when used with the UART class. `stream.readinto1()` returns after the first timeout. Fixes issue #17611. Signed-off-by: robert-hh <robert@hammelrath.com>
Diffstat (limited to 'py')
-rw-r--r--py/stream.c13
-rw-r--r--py/stream.h1
2 files changed, 12 insertions, 2 deletions
diff --git a/py/stream.c b/py/stream.c
index d7a8881e1..e41fbdd5d 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -287,7 +287,7 @@ static mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
-static mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
+static mp_obj_t stream_readinto_generic(size_t n_args, const mp_obj_t *args, byte flags) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
@@ -303,7 +303,7 @@ static mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
}
int error;
- mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
+ mp_uint_t out_sz = mp_stream_rw(args[0], bufinfo.buf, len, &error, flags);
if (error != 0) {
if (mp_is_nonblocking_error(error)) {
return mp_const_none;
@@ -313,8 +313,17 @@ static mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
return MP_OBJ_NEW_SMALL_INT(out_sz);
}
}
+
+static mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
+ return stream_readinto_generic(n_args, args, MP_STREAM_RW_READ);
+}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
+static mp_obj_t stream_readinto1(size_t n_args, const mp_obj_t *args) {
+ return stream_readinto_generic(n_args, args, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto1_obj, 2, 3, stream_readinto1);
+
static mp_obj_t stream_readall(mp_obj_t self_in) {
const mp_stream_p_t *stream_p = mp_get_stream(self_in);
diff --git a/py/stream.h b/py/stream.h
index 7c4d38afa..c3606e8ec 100644
--- a/py/stream.h
+++ b/py/stream.h
@@ -79,6 +79,7 @@ typedef struct _mp_stream_p_t {
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj);
+MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto1_obj);
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);