iostream.h
Go to the documentation of this file.
1 /*
2  pybind11/iostream.h -- Tools to assist with redirecting cout and cerr to Python
3 
4  Copyright (c) 2017 Henry F. Schreiner
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "pybind11.h"
13 
14 #include <streambuf>
15 #include <ostream>
16 #include <string>
17 #include <memory>
18 #include <iostream>
19 
22 
23 // Buffer that writes to Python instead of C++
24 class pythonbuf : public std::streambuf {
25 private:
26  using traits_type = std::streambuf::traits_type;
27 
28  const size_t buf_size;
29  std::unique_ptr<char[]> d_buffer;
30  object pywrite;
31  object pyflush;
32 
33  int overflow(int c) override {
34  if (!traits_type::eq_int_type(c, traits_type::eof())) {
35  *pptr() = traits_type::to_char_type(c);
36  pbump(1);
37  }
38  return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
39  }
40 
41  // This function must be non-virtual to be called in a destructor. If the
42  // rare MSVC test failure shows up with this version, then this should be
43  // simplified to a fully qualified call.
44  int _sync() {
45  if (pbase() != pptr()) {
46  // This subtraction cannot be negative, so dropping the sign
47  str line(pbase(), static_cast<size_t>(pptr() - pbase()));
48 
49  {
51  pywrite(line);
52  pyflush();
53  }
54 
55  setp(pbase(), epptr());
56  }
57  return 0;
58  }
59 
60  int sync() override {
61  return _sync();
62  }
63 
64 public:
65 
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);
72  }
73 
74  pythonbuf(pythonbuf&&) = default;
75 
77  ~pythonbuf() override {
78  _sync();
79  }
80 };
81 
83 
84 
85 
110 protected:
111  std::streambuf *old;
112  std::ostream &costream;
113  detail::pythonbuf buffer;
114 
115 public:
117  std::ostream &costream = std::cout,
118  object pyostream = module::import("sys").attr("stdout"))
119  : costream(costream), buffer(pyostream) {
120  old = costream.rdbuf(&buffer);
121  }
122 
124  costream.rdbuf(old);
125  }
126 
129  scoped_ostream_redirect &operator=(const scoped_ostream_redirect &) = delete;
130  scoped_ostream_redirect &operator=(scoped_ostream_redirect &&) = delete;
131 };
132 
133 
146 public:
148  std::ostream &costream = std::cerr,
149  object pyostream = module::import("sys").attr("stderr"))
150  : scoped_ostream_redirect(costream,pyostream) {}
151 };
152 
153 
155 
156 // Class to redirect output as a context manager. C++ backend.
160  std::unique_ptr<scoped_ostream_redirect> redirect_stdout;
161  std::unique_ptr<scoped_estream_redirect> redirect_stderr;
162 
163 public:
164  OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
165  : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
166 
167  void enter() {
168  if (do_stdout_)
169  redirect_stdout.reset(new scoped_ostream_redirect());
170  if (do_stderr_)
171  redirect_stderr.reset(new scoped_estream_redirect());
172  }
173 
174  void exit() {
175  redirect_stdout.reset();
176  redirect_stderr.reset();
177  }
178 };
179 
181 
182 
209 inline class_<detail::OstreamRedirect> add_ostream_redirect(module m, std::string name = "ostream_redirect") {
211  .def(init<bool,bool>(), arg("stdout")=true, arg("stderr")=true)
212  .def("__enter__", &detail::OstreamRedirect::enter)
213  .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); });
214 }
215 
object pywrite
Definition: iostream.h:30
Matrix3f m
scoped_estream_redirect(std::ostream &costream=std::cerr, object pyostream=module::import("sys").attr("stderr"))
Definition: iostream.h:147
std::streambuf::traits_type traits_type
Definition: iostream.h:26
~pythonbuf() override
Sync before destroy.
Definition: iostream.h:77
int sync() override
Definition: iostream.h:60
int _sync()
Definition: iostream.h:44
Definition: pytypes.h:1322
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
class_ & def(const char *name_, Func &&f, const Extra &...extra)
Definition: pybind11.h:1192
Wrapper for Python extension modules.
Definition: pybind11.h:855
int overflow(int c) override
Definition: iostream.h:33
Definition: Half.h:150
const size_t buf_size
Definition: iostream.h:28
detail::pythonbuf buffer
Definition: iostream.h:113
scoped_ostream_redirect(std::ostream &costream=std::cout, object pyostream=module::import("sys").attr("stdout"))
Definition: iostream.h:116
pythonbuf(object pyostream, size_t buffer_size=1024)
Definition: iostream.h:66
std::streambuf * old
Definition: iostream.h:111
Annotation that marks a class as local to the module:
Definition: attr.h:72
Definition: pytypes.h:928
std::unique_ptr< scoped_estream_redirect > redirect_stderr
Definition: iostream.h:161
std::unique_ptr< char[]> d_buffer
Definition: iostream.h:29
class_< detail::OstreamRedirect > add_ostream_redirect(module m, std::string name="ostream_redirect")
Definition: iostream.h:209
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
std::unique_ptr< scoped_ostream_redirect > redirect_stdout
Definition: iostream.h:160
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:914
OstreamRedirect(bool do_stdout=true, bool do_stderr=true)
Definition: iostream.h:164
Annotation for function names.
Definition: attr.h:36
object pyflush
Definition: iostream.h:31
void enter()
Definition: iostream.h:167
std::ostream & costream
Definition: iostream.h:112
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:16