Repository navigation
ports/unix: Add full uos.dupterm support. - #6080
andrewleech wants to merge 2 commits into
Conversation
| @@ -103,3 +103,7 @@ enum { | |||
|
|
|||
| void mp_hal_get_mac(int idx, uint8_t buf[6]); | |||
| #endif | |||
|
|
|||
| #if MICROPY_PY_OS_DUPTERM | |||
| void init_dupterm_stdio(); | |||
There was a problem hiding this comment.
This should have mp_ prefix since it is in a header file. Or even better, mp_unix_ since it is specific to the unix port.
There was a problem hiding this comment.
I was loath to change the name of an existing file, but I too thought it was oddly inconsistent in name.
| @@ -64,8 +66,11 @@ STATIC void sighandler(int signum) { | |||
| } | |||
| #endif | |||
|
|
|||
| int mp_interrupt_char = -1; | |||
There was a problem hiding this comment.
might be nice to have a comment that explains this is the mp_interrupt_char from lib/utils/interupt_char.h
There was a problem hiding this comment.
Good point. Yes I had to add this just to allow compilation with MICROPY_PY_OS_DUPTERM enabled.
| MP_STATE_VM(dupterm_objs[idx]) = save_term; | ||
| STATIC mp_uint_t unix_stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { | ||
| ssize_t ret; | ||
| MP_HAL_RETRY_SYSCALL(ret, read(STDIN_FILENO, (byte *)buf, size), {}); |
There was a problem hiding this comment.
should this be handling the error? Otherwise we end up returning -1 at the end of the function without setting *errorcode
| STATIC mp_uint_t unix_stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { | ||
| ssize_t ret; | ||
| MP_HAL_RETRY_SYSCALL(ret, read(STDIN_FILENO, (byte *)buf, size), {}); | ||
| if (ret == 0) { |
There was a problem hiding this comment.
I thought a return value of 0 meant that we have reached EOF rather than non-blocking. Is this a special case for if stdin is closed but other slots are still open?
|
|
||
| STATIC mp_uint_t unix_stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { | ||
| int ret; | ||
| MP_HAL_RETRY_SYSCALL(ret, write(STDOUT_FILENO, (const byte *)buf, size), {}); |
There was a problem hiding this comment.
same here with not handling the error
|
On CPython I guess you can hook into stdout by redefining Anyway, the way it is now It just seems like there's a lot of complexity here with two different kinds of stdout/stdin objects (a dupterm one and a raw one). But I don't really see a way around that. Would it be possible to reuse the |
|
Thanks for the pointer towards const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_textio}, STDIN_FILENO};
const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_textio}, STDOUT_FILENO};
const mp_obj_vfs_posix_file_t mp_sys_stderr_obj = {{&mp_type_textio}, STDERR_FILENO};I should be able to clean up this patch reusing them. |
|
@dpgeorge there's a catch with using the I've resolved this by adding a relatively small amount of extra logic to make a new It feels like a bit of a tacked-on-the-side addition, but it does make for less duplication than I had previously. Now I'm also getting a lot of travis fails, for one the minimal port doesn't have the new file handle created, presumably due to a lack of one/both of MICROPY_VFS_POSIX / MICROPY_VFS_POSIX_FILE |
372e90b to
da3f655
Compare
da3f655 to
779e38b
Compare
|
Code size report: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6080 +/- ##
==========================================
- Coverage 98.46% 98.15% -0.32%
==========================================
Files 176 176
Lines 22784 22890 +106
==========================================
+ Hits 22435 22467 +32
- Misses 349 423 +74 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…path. The MAX(0, written) comparison where written is mp_uint_t (unsigned) triggers a -Wtype-limits warning because unsigned >= 0 is always true. Replace with an explicit MP_STREAM_ERROR check which is the actual intended guard. Signed-off-by: Andrew Leech <[email protected]>
Implements proper dupterm support for the unix port using the existing MICROPY_PY_OS_DUPTERM_BUILTIN_STREAM infrastructure. A lightweight mp_unix_stdio_obj stream type wraps STDIN/STDOUT and is registered as dupterm slot 0 at init. This allows os.dupterm() to manage the default console alongside user-provided streams (e.g. for remote REPL over sockets). The mp_hal_stdin_rx_chr and mp_hal_stdout_tx_strn functions are simplified to delegate to mp_os_dupterm_rx_chr/mp_os_dupterm_tx_strn. Also provides mp_interrupt_char for dupterm keyboard interrupt detection on non-builtin streams, and removes the stderr->dupterm echo to avoid double output on the unix port where stderr and stdout are separate. Signed-off-by: Andrew Leech <[email protected]>
779e38b to
66a4bc4
Compare
The unix port currently supports a very limited uos.dupterm implementation.
It can only replace stdin and/or duplicate stdout.
When using uos.dupterm to replace slot 0 it does not return a handle to the existing (posix stdio) stream.
This PR resolves these issues, preferring to use dupterm for all stdio on unix when
MICROPY_PY_OS_DUPTERM=1is configured in the build.With this change simple modification of the output stream is possible, for example to format logging messaging with a basic timestamp