How to capture rust stdout output from python #1960
Unanswered
RobertCraigie
asked this question in
Questions
Replies: 2 comments
-
See #1918 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I am in a more sophisticated situation, I use You can impl the use std::io::Write;
struct PySysStdout;
impl Write for PySysStdout {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let s = std::str::from_utf8(buf).unwrap();
Python::with_gil(|py| {
let locals = PyDict::new(py);
locals.set_item("s", s).unwrap();
py.run("print(s, end='')", None, Some(&locals)).unwrap();
});
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Python::with_gil(|py| {
py.run("import sys;sys.stdout.flush()", None, None).unwrap();
});
Ok(())
}
} And then, refer this StackOverflow answer. Hope it works for you! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've tried using pytest's capsys fixture and python's contextlib.redirect_stdout but neither seemed to capture any output.
Is capturing stdout output from python supported?
Thank you for this fantastic library!
Beta Was this translation helpful? Give feedback.
All reactions