pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Enable writing to python stdio streams

Open goulart-paul opened this issue 11 months ago • 3 comments

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.

goulart-paul avatar Mar 01 '24 15:03 goulart-paul

Thanks for opening this PR! I will do my best to review tomorrow.

davidhewitt avatar Mar 01 '24 23:03 davidhewitt

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.

davidhewitt avatar Mar 02 '24 23:03 davidhewitt

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.

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.

goulart-paul avatar Mar 03 '24 10:03 goulart-paul