QUERY · ISSUE
Enabling DUPTERM on Unix kills output to stderr
bugport-unix
Port, board and/or hardware
Unix
MicroPython version
current master, v1.27.0-175-ga8b71559cf-dirty
Reproduction
Adding
#define MICROPY_PY_OS_DUPTERM (2)
to ports/unix/mpconfigport.h disables writing to standard error. Output is sent to stdout instead.
Expected behaviour
$ ports/unix/build-standard/micropython -c "import sys;print('XXX',file=sys.stderr)" >/dev/null
XXX
$
Observed behaviour
$ ports/unix/build-standard/micropython -c "import sys;print('XXX',file=sys.stderr)" >/dev/null
[ no output ]
$
Additional Information
>>> import sys
>>> sys.stdout
<io.TextIOWrapper 1>
>>> sys.stderr
<io.TextIOWrapper 2>
but print or write don't seem to care.
Code of Conduct
Yes, I agree
CANDIDATE · PULL REQUEST
ports/unix: Add full uos.dupterm support.
port-unix
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
stdio = None
class logger:
def write(self, buf):
global stdio
stdio.write(str(utime.time()))
stdio.write(" | ")
stdio.write(buf)
stdio = uos.dupterm(logger(), 0)