Skip to content

Commit 91ee8ac

Browse files
mvds00dpgeorge
authored andcommitted
extmod/os_dupterm: Let mp_os_dupterm_tx_strn() return num bytes written.
In case of multiple outputs, the minimum successful write length is returned. In line with this, in case any output has a write error, zero is returned. In case of no outputs, -1 is returned. The return value can be used to assess whether writes were attempted, and if so, whether they succeeded. Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
1 parent 5d28bb4 commit 91ee8ac

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

extmod/misc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream);
3939
void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t stream_attached);
4040
uintptr_t mp_os_dupterm_poll(uintptr_t poll_flags);
4141
int mp_os_dupterm_rx_chr(void);
42-
void mp_os_dupterm_tx_strn(const char *str, size_t len);
42+
int mp_os_dupterm_tx_strn(const char *str, size_t len);
4343
void mp_os_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc);
4444
#else
45-
#define mp_os_dupterm_tx_strn(s, l)
45+
static inline int mp_os_dupterm_tx_strn(const char *s, size_t l) {
46+
return -1;
47+
}
4648
#endif
4749

4850
#endif // MICROPY_INCLUDED_EXTMOD_MISC_H

extmod/os_dupterm.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,43 @@ int mp_os_dupterm_rx_chr(void) {
169169
return ret;
170170
}
171171

172-
void mp_os_dupterm_tx_strn(const char *str, size_t len) {
172+
int mp_os_dupterm_tx_strn(const char *str, size_t len) {
173+
// Returns the minimum successful write length, or -1 if no write is attempted.
174+
int ret = len;
175+
bool did_write = false;
173176
for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
174177
if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
175178
continue;
176179
}
180+
did_write = true;
177181

178182
#if MICROPY_PY_OS_DUPTERM_BUILTIN_STREAM
179183
if (mp_os_dupterm_is_builtin_stream(MP_STATE_VM(dupterm_objs[idx]))) {
180184
int errcode = 0;
181185
const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_VM(dupterm_objs[idx]));
182-
stream_p->write(MP_STATE_VM(dupterm_objs[idx]), str, len, &errcode);
186+
mp_uint_t written = stream_p->write(MP_STATE_VM(dupterm_objs[idx]), str, len, &errcode);
187+
int write_res = MAX(0, written);
188+
ret = MIN(write_res, ret);
183189
continue;
184190
}
185191
#endif
186192

187193
nlr_buf_t nlr;
188194
if (nlr_push(&nlr) == 0) {
189-
mp_stream_write(MP_STATE_VM(dupterm_objs[idx]), str, len, MP_STREAM_RW_WRITE);
195+
mp_obj_t written = mp_stream_write(MP_STATE_VM(dupterm_objs[idx]), str, len, MP_STREAM_RW_WRITE);
196+
if (written == mp_const_none) {
197+
ret = 0;
198+
} else if (mp_obj_is_small_int(written)) {
199+
int written_int = MAX(0, MP_OBJ_SMALL_INT_VALUE(written));
200+
ret = MIN(written_int, ret);
201+
}
190202
nlr_pop();
191203
} else {
192204
mp_os_deactivate(idx, "dupterm: Exception in write() method, deactivating: ", MP_OBJ_FROM_PTR(nlr.ret_val));
205+
ret = 0;
193206
}
194207
}
208+
return did_write ? ret : -1;
195209
}
196210

197211
STATIC mp_obj_t mp_os_dupterm(size_t n_args, const mp_obj_t *args) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy