pyo3
pyo3 copied to clipboard
Enable writing to python stdio streams
Enables creation of handles for printing directly to python sys.stdout
and sys.stderr
. This is a (partial) fix for #2247.
Usually calling Rust's println!
result's in output appearing in the python interpreter. However, in some cases this fails (particularly in some, but not all, Jupyter notebooks and on Google Colab) because the Rust std::io::stdout
and std::io::stderr
streams are not redirected to match Python's sys.stdout
and sys.stderr
.
This does not directly fix the problem with println!
, but instead enables printing via
writeln!(pyo3::stdio::stdout(),...)
I have not written a unit test for this because it's unclear what such a test should do other than not crash. I don't see an obvious way of checking via a unit test whether text piped to a python stream actually appears.
Thanks for opening this PR! I will do my best to review tomorrow.
Thanks @adamreichold for picking up the review 👍
As well as the above, I think tests are definitely worth adding here. It would be possible to achieve that by temporarily assigning the Python sys.stdout
/ sys.stderr
streams to io.StringIO
objects (or similar), use these APIs to write to them, and validate the final contents.
Thanks @adamreichold for picking up the review 👍
As well as the above, I think tests are definitely worth adding here. It would be possible to achieve that by temporarily assigning the Python
sys.stdout
/sys.stderr
streams toio.StringIO
objects (or similar), use these APIs to write to them, and validate the final contents.
Good idea. I had not thought to redirect the python side streams someplace else for inspection. I will give this a try.
Edit : unit test added.