Skip to content

Commit

Permalink
add Bound variant
Browse files Browse the repository at this point in the history
  • Loading branch information
goulart-paul committed Mar 4, 2024
1 parent 41d600a commit e90f220
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,72 @@ use crate::intern;
use crate::prelude::*;
use std::io::{LineWriter, Write};

/// Implements `std::io::Write` for a `PyAny` object. The underlying
/// Python object must provide both `write` and `flush` methods.
pub struct PyWriter(Py<PyAny>);

fn get_stdio_writer(stream: &str) -> PyWriter {
impl PyWriter {
/// Construct a new `PyWriter` from a `PyAny` object.
pub fn buffered(self) -> LineWriter<PyWriter> {
LineWriter::new(self)
}
}

/// A GIL-attached equivalent to PyWriter.
pub struct PyWriterBound<'a, 'py>(&'a Bound<'py, PyAny>);

fn get_stdio_pywriter(stream: &str) -> PyWriter {
Python::with_gil(|py| {
let module = PyModule::import_bound(py, "sys").unwrap();
module.getattr(stream).unwrap();
PyWriter(module.into())
let stream = module.getattr(stream).unwrap();
PyWriter(stream.into())
})
}

/// Construct a new handle to Python's `sys.stdout` stream.
/// Construct a new `PyWriter` for Python's `sys.stdout` stream.
pub fn stdout() -> PyWriter {
get_stdio_writer("stdout")
get_stdio_pywriter("stdout")
}

/// Construct a new handle to Python's `sys.stderr` stream.
/// Construct a new `PyWriter` for Python's `sys.stderr` stream.
pub fn stderr() -> PyWriter {
get_stdio_writer("stderr")
get_stdio_pywriter("stderr")
}

/// Construct a new handle to Python's `sys.__stdout__` stream.
/// Construct a new `PyWriter` for Python's `sys.__stdout__` stream.
pub fn __stdout__() -> PyWriter {
get_stdio_writer("__stdout__")
get_stdio_pywriter("__stdout__")
}

/// Construct a new handle to Python's `sys.__stderr__` stream.
/// Construct a new `PyWriter` for Python's `sys.__stderr__` stream.
pub fn __stderr__() -> PyWriter {
get_stdio_writer("__stderr__")
get_stdio_pywriter("__stderr__")
}


impl Write for PyWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
Python::with_gil(|py| -> std::io::Result<usize> {
let str = PyString::new_bound(py,&String::from_utf8_lossy(buf));
self.0
.call_method1(py,intern!(py, "write"), (str,))
Python::with_gil(|py| {
PyWriterBound(self.0.bind(py)).write(buf)
})
}
fn flush(&mut self) -> std::io::Result<()> {
Python::with_gil(|py| {
PyWriterBound(self.0.bind(py)).flush()
})
}
}

impl Write for PyWriterBound<'_, '_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let str = PyString::new_bound(self.0.py(),&String::from_utf8_lossy(buf));
self.0.call_method1(intern!(self.0.py(),"write"), (str,))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(buf.len())
})
}
fn flush(&mut self) -> std::io::Result<()> {
Python::with_gil(|py| -> std::io::Result<()> {
self.0
.call_method0(py, intern!(py, "flush"))
self.0.call_method0(intern!(self.0.py(),"flush"))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
})
Ok(())
}
}
}

0 comments on commit e90f220

Please sign in to comment.