iostream.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. pybind11/iostream.h -- Tools to assist with redirecting cout and cerr to Python
  3. Copyright (c) 2017 Henry F. Schreiner
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. WARNING: The implementation in this file is NOT thread safe. Multiple
  7. threads writing to a redirected ostream concurrently cause data races
  8. and potentially buffer overflows. Therefore it is currently a requirement
  9. that all (possibly) concurrent redirected ostream writes are protected by
  10. a mutex.
  11. #HelpAppreciated: Work on iostream.h thread safety.
  12. For more background see the discussions under
  13. https://github.com/pybind/pybind11/pull/2982 and
  14. https://github.com/pybind/pybind11/pull/2995.
  15. */
  16. #pragma once
  17. #include "pybind11.h"
  18. #include <algorithm>
  19. #include <cstring>
  20. #include <iostream>
  21. #include <iterator>
  22. #include <memory>
  23. #include <ostream>
  24. #include <streambuf>
  25. #include <string>
  26. #include <utility>
  27. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  28. PYBIND11_NAMESPACE_BEGIN(detail)
  29. // Buffer that writes to Python instead of C++
  30. class pythonbuf : public std::streambuf {
  31. private:
  32. using traits_type = std::streambuf::traits_type;
  33. const size_t buf_size;
  34. std::unique_ptr<char[]> d_buffer;
  35. object pywrite;
  36. object pyflush;
  37. int overflow(int c) override {
  38. if (!traits_type::eq_int_type(c, traits_type::eof())) {
  39. *pptr() = traits_type::to_char_type(c);
  40. pbump(1);
  41. }
  42. return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
  43. }
  44. // Computes how many bytes at the end of the buffer are part of an
  45. // incomplete sequence of UTF-8 bytes.
  46. // Precondition: pbase() < pptr()
  47. size_t utf8_remainder() const {
  48. const auto rbase = std::reverse_iterator<char *>(pbase());
  49. const auto rpptr = std::reverse_iterator<char *>(pptr());
  50. auto is_ascii = [](char c) { return (static_cast<unsigned char>(c) & 0x80) == 0x00; };
  51. auto is_leading = [](char c) { return (static_cast<unsigned char>(c) & 0xC0) == 0xC0; };
  52. auto is_leading_2b = [](char c) { return static_cast<unsigned char>(c) <= 0xDF; };
  53. auto is_leading_3b = [](char c) { return static_cast<unsigned char>(c) <= 0xEF; };
  54. // If the last character is ASCII, there are no incomplete code points
  55. if (is_ascii(*rpptr)) {
  56. return 0;
  57. }
  58. // Otherwise, work back from the end of the buffer and find the first
  59. // UTF-8 leading byte
  60. const auto rpend = rbase - rpptr >= 3 ? rpptr + 3 : rbase;
  61. const auto leading = std::find_if(rpptr, rpend, is_leading);
  62. if (leading == rbase) {
  63. return 0;
  64. }
  65. const auto dist = static_cast<size_t>(leading - rpptr);
  66. size_t remainder = 0;
  67. if (dist == 0) {
  68. remainder = 1; // 1-byte code point is impossible
  69. } else if (dist == 1) {
  70. remainder = is_leading_2b(*leading) ? 0 : dist + 1;
  71. } else if (dist == 2) {
  72. remainder = is_leading_3b(*leading) ? 0 : dist + 1;
  73. }
  74. // else if (dist >= 3), at least 4 bytes before encountering an UTF-8
  75. // leading byte, either no remainder or invalid UTF-8.
  76. // Invalid UTF-8 will cause an exception later when converting
  77. // to a Python string, so that's not handled here.
  78. return remainder;
  79. }
  80. // This function must be non-virtual to be called in a destructor.
  81. int _sync() {
  82. if (pbase() != pptr()) { // If buffer is not empty
  83. gil_scoped_acquire tmp;
  84. // This subtraction cannot be negative, so dropping the sign.
  85. auto size = static_cast<size_t>(pptr() - pbase());
  86. size_t remainder = utf8_remainder();
  87. if (size > remainder) {
  88. str line(pbase(), size - remainder);
  89. pywrite(std::move(line));
  90. pyflush();
  91. }
  92. // Copy the remainder at the end of the buffer to the beginning:
  93. if (remainder > 0) {
  94. std::memmove(pbase(), pptr() - remainder, remainder);
  95. }
  96. setp(pbase(), epptr());
  97. pbump(static_cast<int>(remainder));
  98. }
  99. return 0;
  100. }
  101. int sync() override { return _sync(); }
  102. public:
  103. explicit pythonbuf(const object &pyostream, size_t buffer_size = 1024)
  104. : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")),
  105. pyflush(pyostream.attr("flush")) {
  106. setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
  107. }
  108. pythonbuf(pythonbuf &&) = default;
  109. /// Sync before destroy
  110. ~pythonbuf() override { _sync(); }
  111. };
  112. PYBIND11_NAMESPACE_END(detail)
  113. /** \rst
  114. This a move-only guard that redirects output.
  115. .. code-block:: cpp
  116. #include <pybind11/iostream.h>
  117. ...
  118. {
  119. py::scoped_ostream_redirect output;
  120. std::cout << "Hello, World!"; // Python stdout
  121. } // <-- return std::cout to normal
  122. You can explicitly pass the c++ stream and the python object,
  123. for example to guard stderr instead.
  124. .. code-block:: cpp
  125. {
  126. py::scoped_ostream_redirect output{
  127. std::cerr, py::module::import("sys").attr("stderr")};
  128. std::cout << "Hello, World!";
  129. }
  130. \endrst */
  131. class scoped_ostream_redirect {
  132. protected:
  133. std::streambuf *old;
  134. std::ostream &costream;
  135. detail::pythonbuf buffer;
  136. public:
  137. explicit scoped_ostream_redirect(std::ostream &costream = std::cout,
  138. const object &pyostream
  139. = module_::import("sys").attr("stdout"))
  140. : costream(costream), buffer(pyostream) {
  141. old = costream.rdbuf(&buffer);
  142. }
  143. ~scoped_ostream_redirect() { costream.rdbuf(old); }
  144. scoped_ostream_redirect(const scoped_ostream_redirect &) = delete;
  145. scoped_ostream_redirect(scoped_ostream_redirect &&other) = default;
  146. scoped_ostream_redirect &operator=(const scoped_ostream_redirect &) = delete;
  147. scoped_ostream_redirect &operator=(scoped_ostream_redirect &&) = delete;
  148. };
  149. /** \rst
  150. Like `scoped_ostream_redirect`, but redirects cerr by default. This class
  151. is provided primary to make ``py::call_guard`` easier to make.
  152. .. code-block:: cpp
  153. m.def("noisy_func", &noisy_func,
  154. py::call_guard<scoped_ostream_redirect,
  155. scoped_estream_redirect>());
  156. \endrst */
  157. class scoped_estream_redirect : public scoped_ostream_redirect {
  158. public:
  159. explicit scoped_estream_redirect(std::ostream &costream = std::cerr,
  160. const object &pyostream
  161. = module_::import("sys").attr("stderr"))
  162. : scoped_ostream_redirect(costream, pyostream) {}
  163. };
  164. PYBIND11_NAMESPACE_BEGIN(detail)
  165. // Class to redirect output as a context manager. C++ backend.
  166. class OstreamRedirect {
  167. bool do_stdout_;
  168. bool do_stderr_;
  169. std::unique_ptr<scoped_ostream_redirect> redirect_stdout;
  170. std::unique_ptr<scoped_estream_redirect> redirect_stderr;
  171. public:
  172. explicit OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
  173. : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
  174. void enter() {
  175. if (do_stdout_) {
  176. redirect_stdout.reset(new scoped_ostream_redirect());
  177. }
  178. if (do_stderr_) {
  179. redirect_stderr.reset(new scoped_estream_redirect());
  180. }
  181. }
  182. void exit() {
  183. redirect_stdout.reset();
  184. redirect_stderr.reset();
  185. }
  186. };
  187. PYBIND11_NAMESPACE_END(detail)
  188. /** \rst
  189. This is a helper function to add a C++ redirect context manager to Python
  190. instead of using a C++ guard. To use it, add the following to your binding code:
  191. .. code-block:: cpp
  192. #include <pybind11/iostream.h>
  193. ...
  194. py::add_ostream_redirect(m, "ostream_redirect");
  195. You now have a Python context manager that redirects your output:
  196. .. code-block:: python
  197. with m.ostream_redirect():
  198. m.print_to_cout_function()
  199. This manager can optionally be told which streams to operate on:
  200. .. code-block:: python
  201. with m.ostream_redirect(stdout=true, stderr=true):
  202. m.noisy_function_with_error_printing()
  203. \endrst */
  204. inline class_<detail::OstreamRedirect>
  205. add_ostream_redirect(module_ m, const std::string &name = "ostream_redirect") {
  206. return class_<detail::OstreamRedirect>(std::move(m), name.c_str(), module_local())
  207. .def(init<bool, bool>(), arg("stdout") = true, arg("stderr") = true)
  208. .def("__enter__", &detail::OstreamRedirect::enter)
  209. .def("__exit__", [](detail::OstreamRedirect &self_, const args &) { self_.exit(); });
  210. }
  211. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)