← index #11099Issue #18091
Off-topic · high · value 2.842
QUERY · ISSUE

Binary stdin and stdout buffers for Unix port

openby smurfixopened 2023-03-22updated 2023-03-30
enhancement
$ micropython
>>> import sys
>>> sys.stdout.buffer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TextIOWrapper' object has no attribute 'buffer'
>>>

Well, other ports have it … CPython has it … and I'd really like to not have to fall back to open("/dev/stdout","wb") in my unit tests.

Same for stdin of course.

CANDIDATE · ISSUE

print output redirection not working as in Python.

openby beetlegiggopened 2025-09-17updated 2025-09-19
bug

Port, board and/or hardware

raspberry pico2w

MicroPython version

MicroPython v1.26.0 on 2025-08-09; Raspberry Pi Pico 2 W with RP2350

Reproduction

# print redirection test
import sys
proper_stdout = sys.stdout       # save original
log = open('out.txt', 'w')
sys.stdout = log                 # redirect stdout            

print('hello')
print('wheres my print')

log.close()
sys.stdout = proper_stdout      # revert redirect

print('back to the future')
print('so whats in the out.txt file?')

read_log = open('out.txt', 'r')
for line in read_log:
    print(line)
read_log.close()    

Expected behaviour

The sys.stdout would be redirected to the 'log' file (out.txt) and the following print two print statement contents would be directed to the out.txt file.

Observed behaviour

Traceback (most recent call last):
File "<stdin>", line 5, in <module>
AttributeError: 'module' object has no attribute 'stdout'

line 5 is:
sys.stdout = log

Additional Information

This code snippet works as expected in Python.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied