-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
ports/unix: Add full uos.dupterm support. #6080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Yes I had to add this just to allow compilation with MICROPY_PY_OS_DUPTERM
enabled.
ports/unix/unix_mphal.c
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be handling the error? Otherwise we end up returning -1 at the end of the function without setting *errorcode
ports/unix/unix_mphal.c
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
ports/unix/unix_mphal.c
Outdated
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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=1
is 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