Scarab  v2.9.0
Project 8 C++ Utility Library
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 
21 NAMESPACE_BEGIN(detail)
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) {
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  int sync() {
42  if (pbase() != pptr()) {
43  // This subtraction cannot be negative, so dropping the sign
44  str line(pbase(), static_cast<size_t>(pptr() - pbase()));
45 
46  {
48  pywrite(line);
49  pyflush();
50  }
51 
52  setp(pbase(), epptr());
53  }
54  return 0;
55  }
56 
57 public:
58 
59  pythonbuf(object pyostream, size_t buffer_size = 1024)
60  : buf_size(buffer_size),
61  d_buffer(new char[buf_size]),
62  pywrite(pyostream.attr("write")),
63  pyflush(pyostream.attr("flush")) {
64  setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
65  }
66 
67  pythonbuf(pythonbuf&&) = default;
68 
71  sync();
72  }
73 };
74 
75 NAMESPACE_END(detail)
76 
77 
78 
103 protected:
104  std::streambuf *old;
105  std::ostream &costream;
107 
108 public:
110  std::ostream &costream = std::cout,
111  object pyostream = module::import("sys").attr("stdout"))
112  : costream(costream), buffer(pyostream) {
113  old = costream.rdbuf(&buffer);
114  }
115 
117  costream.rdbuf(old);
118  }
119 
122  scoped_ostream_redirect &operator=(const scoped_ostream_redirect &) = delete;
123  scoped_ostream_redirect &operator=(scoped_ostream_redirect &&) = delete;
124 };
125 
126 
139 public:
141  std::ostream &costream = std::cerr,
142  object pyostream = module::import("sys").attr("stderr"))
143  : scoped_ostream_redirect(costream,pyostream) {}
144 };
145 
146 
147 NAMESPACE_BEGIN(detail)
148 
149 // Class to redirect output as a context manager. C++ backend.
153  std::unique_ptr<scoped_ostream_redirect> redirect_stdout;
154  std::unique_ptr<scoped_estream_redirect> redirect_stderr;
155 
156 public:
157  OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
158  : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
159 
160  void enter() {
161  if (do_stdout_)
162  redirect_stdout.reset(new scoped_ostream_redirect());
163  if (do_stderr_)
164  redirect_stderr.reset(new scoped_estream_redirect());
165  }
166 
167  void exit() {
168  redirect_stdout.reset();
169  redirect_stderr.reset();
170  }
171 };
172 
173 NAMESPACE_END(detail)
174 
175 
202 inline class_<detail::OstreamRedirect> add_ostream_redirect(module m, std::string name = "ostream_redirect") {
204  .def(init<bool,bool>(), arg("stdout")=true, arg("stderr")=true)
205  .def("__enter__", &detail::OstreamRedirect::enter)
206  .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); });
207 }
208 
OstreamRedirect(bool do_stdout=true, bool do_stderr=true)
Definition: iostream.h:157
#define PYBIND11_NAMESPACE
Definition: detail/common.h:26
std::streambuf::traits_type traits_type
Definition: iostream.h:26
pythonbuf(object pyostream, size_t buffer_size=1024)
Definition: iostream.h:59
class_< detail::OstreamRedirect > add_ostream_redirect(module m, std::string name="ostream_redirect")
Definition: iostream.h:202
STL namespace.
Wrapper for Python extension modules.
Definition: pybind11.h:789
scoped_estream_redirect(std::ostream &costream=std::cerr, object pyostream=module::import("sys").attr("stderr"))
Definition: iostream.h:140
std::unique_ptr< scoped_ostream_redirect > redirect_stdout
Definition: iostream.h:153
std::unique_ptr< char[]> d_buffer
Definition: iostream.h:29
std::unique_ptr< scoped_estream_redirect > redirect_stderr
Definition: iostream.h:154
#define NAMESPACE_END(name)
Definition: detail/common.h:16
~pythonbuf()
Sync before destroy.
Definition: iostream.h:70
Annotation for function names.
Definition: attr.h:33
#define NAMESPACE_BEGIN(name)
Definition: detail/common.h:13
scoped_ostream_redirect(std::ostream &costream=std::cout, object pyostream=module::import("sys").attr("stdout"))
Definition: iostream.h:109
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110
Annotation that marks a class as local to the module:
Definition: attr.h:68