34 if (!traits_type::eq_int_type(c, traits_type::eof())) {
35 *pptr() = traits_type::to_char_type(c);
38 return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
45 if (pbase() != pptr()) {
47 str line(pbase(), static_cast<size_t>(pptr() - pbase()));
55 setp(pbase(), epptr());
66 pythonbuf(
object pyostream,
size_t buffer_size = 1024)
67 : buf_size(buffer_size),
68 d_buffer(new char[buf_size]),
69 pywrite(pyostream.attr(
"write")),
70 pyflush(pyostream.attr(
"flush")) {
71 setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
117 std::ostream &costream = std::cout,
119 : costream(costream),
buffer(pyostream) {
120 old = costream.rdbuf(&
buffer);
165 : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
175 redirect_stdout.reset();
176 redirect_stderr.reset();
212 .def(
"__enter__", &detail::OstreamRedirect::enter)
213 .
def(
"__exit__", [](detail::OstreamRedirect &self_,
args) { self_.exit(); });
scoped_estream_redirect(std::ostream &costream=std::cerr, object pyostream=module::import("sys").attr("stderr"))
std::streambuf::traits_type traits_type
~pythonbuf() override
Sync before destroy.
class_ & def(const char *name_, Func &&f, const Extra &...extra)
Wrapper for Python extension modules.
int overflow(int c) override
scoped_ostream_redirect(std::ostream &costream=std::cout, object pyostream=module::import("sys").attr("stdout"))
#define PYBIND11_NAMESPACE
pythonbuf(object pyostream, size_t buffer_size=1024)
Annotation that marks a class as local to the module:
std::unique_ptr< scoped_estream_redirect > redirect_stderr
std::unique_ptr< char[]> d_buffer
~scoped_ostream_redirect()
class_< detail::OstreamRedirect > add_ostream_redirect(module m, std::string name="ostream_redirect")
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
std::unique_ptr< scoped_ostream_redirect > redirect_stdout
static module_ import(const char *name)
Import and return a module or throws error_already_set.
OstreamRedirect(bool do_stdout=true, bool do_stderr=true)
Annotation for function names.
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)