2017-08-25 00:12:43 +00:00
|
|
|
/*
|
|
|
|
pybind11/iostream.h -- Tools to assist with redirecting cout and cerr to Python
|
|
|
|
|
|
|
|
Copyright (c) 2017 Henry F. Schreiner
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
2021-07-12 20:39:06 +00:00
|
|
|
|
|
|
|
WARNING: The implementation in this file is NOT thread safe. Multiple
|
|
|
|
threads writing to a redirected ostream concurrently cause data races
|
2021-07-12 20:57:28 +00:00
|
|
|
and potentially buffer overflows. Therefore it is currently a requirement
|
2021-07-12 20:39:06 +00:00
|
|
|
that all (possibly) concurrent redirected ostream writes are protected by
|
|
|
|
a mutex.
|
|
|
|
#HelpAppreciated: Work on iostream.h thread safety.
|
|
|
|
For more background see the discussions under
|
|
|
|
https://github.com/pybind/pybind11/pull/2982 and
|
|
|
|
https://github.com/pybind/pybind11/pull/2995.
|
2017-08-25 00:12:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pybind11.h"
|
|
|
|
|
2021-06-19 17:53:27 +00:00
|
|
|
#include <algorithm>
|
2021-05-04 05:04:38 +00:00
|
|
|
#include <cstring>
|
2021-06-19 17:53:27 +00:00
|
|
|
#include <iostream>
|
2021-05-04 05:04:38 +00:00
|
|
|
#include <iterator>
|
2021-06-19 17:53:27 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <ostream>
|
|
|
|
#include <streambuf>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2017-08-25 00:12:43 +00:00
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
// Buffer that writes to Python instead of C++
|
|
|
|
class pythonbuf : public std::streambuf {
|
|
|
|
private:
|
|
|
|
using traits_type = std::streambuf::traits_type;
|
|
|
|
|
2019-06-11 20:11:38 +00:00
|
|
|
const size_t buf_size;
|
|
|
|
std::unique_ptr<char[]> d_buffer;
|
2017-08-25 00:12:43 +00:00
|
|
|
object pywrite;
|
|
|
|
object pyflush;
|
|
|
|
|
2020-09-11 02:43:53 +00:00
|
|
|
int overflow(int c) override {
|
2017-08-25 00:12:43 +00:00
|
|
|
if (!traits_type::eq_int_type(c, traits_type::eof())) {
|
|
|
|
*pptr() = traits_type::to_char_type(c);
|
|
|
|
pbump(1);
|
|
|
|
}
|
2018-08-29 09:48:30 +00:00
|
|
|
return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 05:04:38 +00:00
|
|
|
// Computes how many bytes at the end of the buffer are part of an
|
|
|
|
// incomplete sequence of UTF-8 bytes.
|
|
|
|
// Precondition: pbase() < pptr()
|
|
|
|
size_t utf8_remainder() const {
|
|
|
|
const auto rbase = std::reverse_iterator<char *>(pbase());
|
|
|
|
const auto rpptr = std::reverse_iterator<char *>(pptr());
|
2022-02-10 20:17:07 +00:00
|
|
|
auto is_ascii = [](char c) { return (static_cast<unsigned char>(c) & 0x80) == 0x00; };
|
|
|
|
auto is_leading = [](char c) { return (static_cast<unsigned char>(c) & 0xC0) == 0xC0; };
|
|
|
|
auto is_leading_2b = [](char c) { return static_cast<unsigned char>(c) <= 0xDF; };
|
|
|
|
auto is_leading_3b = [](char c) { return static_cast<unsigned char>(c) <= 0xEF; };
|
2021-05-04 05:04:38 +00:00
|
|
|
// If the last character is ASCII, there are no incomplete code points
|
2022-02-08 00:23:20 +00:00
|
|
|
if (is_ascii(*rpptr)) {
|
2021-05-04 05:04:38 +00:00
|
|
|
return 0;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2021-05-04 05:04:38 +00:00
|
|
|
// Otherwise, work back from the end of the buffer and find the first
|
|
|
|
// UTF-8 leading byte
|
2022-02-10 20:17:07 +00:00
|
|
|
const auto rpend = rbase - rpptr >= 3 ? rpptr + 3 : rbase;
|
2021-05-04 05:04:38 +00:00
|
|
|
const auto leading = std::find_if(rpptr, rpend, is_leading);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (leading == rbase) {
|
2021-05-04 05:04:38 +00:00
|
|
|
return 0;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
const auto dist = static_cast<size_t>(leading - rpptr);
|
|
|
|
size_t remainder = 0;
|
2021-05-04 05:04:38 +00:00
|
|
|
|
2022-02-08 00:23:20 +00:00
|
|
|
if (dist == 0) {
|
2021-05-04 05:04:38 +00:00
|
|
|
remainder = 1; // 1-byte code point is impossible
|
2022-02-08 00:23:20 +00:00
|
|
|
} else if (dist == 1) {
|
2021-05-04 05:04:38 +00:00
|
|
|
remainder = is_leading_2b(*leading) ? 0 : dist + 1;
|
2022-02-08 00:23:20 +00:00
|
|
|
} else if (dist == 2) {
|
2021-05-04 05:04:38 +00:00
|
|
|
remainder = is_leading_3b(*leading) ? 0 : dist + 1;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2021-05-04 05:04:38 +00:00
|
|
|
// else if (dist >= 3), at least 4 bytes before encountering an UTF-8
|
|
|
|
// leading byte, either no remainder or invalid UTF-8.
|
|
|
|
// Invalid UTF-8 will cause an exception later when converting
|
|
|
|
// to a Python string, so that's not handled here.
|
|
|
|
return remainder;
|
|
|
|
}
|
|
|
|
|
2021-07-12 20:39:06 +00:00
|
|
|
// This function must be non-virtual to be called in a destructor.
|
2020-09-12 12:26:35 +00:00
|
|
|
int _sync() {
|
2021-05-04 05:04:38 +00:00
|
|
|
if (pbase() != pptr()) { // If buffer is not empty
|
|
|
|
gil_scoped_acquire tmp;
|
2021-07-12 20:39:06 +00:00
|
|
|
// This subtraction cannot be negative, so dropping the sign.
|
2022-02-10 20:17:07 +00:00
|
|
|
auto size = static_cast<size_t>(pptr() - pbase());
|
2021-07-12 20:39:06 +00:00
|
|
|
size_t remainder = utf8_remainder();
|
|
|
|
|
|
|
|
if (size > remainder) {
|
|
|
|
str line(pbase(), size - remainder);
|
2022-05-27 18:32:57 +00:00
|
|
|
pywrite(std::move(line));
|
2021-07-12 20:39:06 +00:00
|
|
|
pyflush();
|
2019-06-11 20:17:50 +00:00
|
|
|
}
|
2021-07-12 20:39:06 +00:00
|
|
|
|
|
|
|
// Copy the remainder at the end of the buffer to the beginning:
|
2022-02-08 00:23:20 +00:00
|
|
|
if (remainder > 0) {
|
2021-07-12 20:39:06 +00:00
|
|
|
std::memmove(pbase(), pptr() - remainder, remainder);
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2021-07-12 20:39:06 +00:00
|
|
|
setp(pbase(), epptr());
|
|
|
|
pbump(static_cast<int>(remainder));
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
int sync() override { return _sync(); }
|
2020-09-12 12:26:35 +00:00
|
|
|
|
2017-08-25 00:12:43 +00:00
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit pythonbuf(const object &pyostream, size_t buffer_size = 1024)
|
2021-06-19 17:53:27 +00:00
|
|
|
: buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")),
|
2017-08-25 00:12:43 +00:00
|
|
|
pyflush(pyostream.attr("flush")) {
|
2019-06-11 20:11:38 +00:00
|
|
|
setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
pythonbuf(pythonbuf &&) = default;
|
2019-07-15 14:16:14 +00:00
|
|
|
|
2017-08-25 00:12:43 +00:00
|
|
|
/// Sync before destroy
|
2022-02-10 20:17:07 +00:00
|
|
|
~pythonbuf() override { _sync(); }
|
2017-08-25 00:12:43 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
/** \rst
|
|
|
|
This a move-only guard that redirects output.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
#include <pybind11/iostream.h>
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
{
|
|
|
|
py::scoped_ostream_redirect output;
|
|
|
|
std::cout << "Hello, World!"; // Python stdout
|
|
|
|
} // <-- return std::cout to normal
|
|
|
|
|
|
|
|
You can explicitly pass the c++ stream and the python object,
|
|
|
|
for example to guard stderr instead.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
{
|
2022-02-10 19:42:03 +00:00
|
|
|
py::scoped_ostream_redirect output{
|
|
|
|
std::cerr, py::module::import("sys").attr("stderr")};
|
2021-01-20 00:56:22 +00:00
|
|
|
std::cout << "Hello, World!";
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|
|
|
|
\endrst */
|
|
|
|
class scoped_ostream_redirect {
|
|
|
|
protected:
|
|
|
|
std::streambuf *old;
|
|
|
|
std::ostream &costream;
|
|
|
|
detail::pythonbuf buffer;
|
|
|
|
|
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit scoped_ostream_redirect(std::ostream &costream = std::cout,
|
|
|
|
const object &pyostream
|
|
|
|
= module_::import("sys").attr("stdout"))
|
2017-08-25 00:12:43 +00:00
|
|
|
: costream(costream), buffer(pyostream) {
|
|
|
|
old = costream.rdbuf(&buffer);
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
~scoped_ostream_redirect() { costream.rdbuf(old); }
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
scoped_ostream_redirect(const scoped_ostream_redirect &) = delete;
|
|
|
|
scoped_ostream_redirect(scoped_ostream_redirect &&other) = default;
|
|
|
|
scoped_ostream_redirect &operator=(const scoped_ostream_redirect &) = delete;
|
|
|
|
scoped_ostream_redirect &operator=(scoped_ostream_redirect &&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \rst
|
|
|
|
Like `scoped_ostream_redirect`, but redirects cerr by default. This class
|
|
|
|
is provided primary to make ``py::call_guard`` easier to make.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
m.def("noisy_func", &noisy_func,
|
|
|
|
py::call_guard<scoped_ostream_redirect,
|
|
|
|
scoped_estream_redirect>());
|
|
|
|
|
|
|
|
\endrst */
|
|
|
|
class scoped_estream_redirect : public scoped_ostream_redirect {
|
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit scoped_estream_redirect(std::ostream &costream = std::cerr,
|
|
|
|
const object &pyostream
|
|
|
|
= module_::import("sys").attr("stderr"))
|
2021-06-19 17:53:27 +00:00
|
|
|
: scoped_ostream_redirect(costream, pyostream) {}
|
2017-08-25 00:12:43 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
// Class to redirect output as a context manager. C++ backend.
|
|
|
|
class OstreamRedirect {
|
|
|
|
bool do_stdout_;
|
|
|
|
bool do_stderr_;
|
|
|
|
std::unique_ptr<scoped_ostream_redirect> redirect_stdout;
|
|
|
|
std::unique_ptr<scoped_estream_redirect> redirect_stderr;
|
|
|
|
|
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
|
2017-08-25 00:12:43 +00:00
|
|
|
: do_stdout_(do_stdout), do_stderr_(do_stderr) {}
|
|
|
|
|
|
|
|
void enter() {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (do_stdout_) {
|
2017-08-25 00:12:43 +00:00
|
|
|
redirect_stdout.reset(new scoped_ostream_redirect());
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
|
|
|
if (do_stderr_) {
|
2017-08-25 00:12:43 +00:00
|
|
|
redirect_stderr.reset(new scoped_estream_redirect());
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit() {
|
|
|
|
redirect_stdout.reset();
|
|
|
|
redirect_stderr.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
/** \rst
|
|
|
|
This is a helper function to add a C++ redirect context manager to Python
|
|
|
|
instead of using a C++ guard. To use it, add the following to your binding code:
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
#include <pybind11/iostream.h>
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
py::add_ostream_redirect(m, "ostream_redirect");
|
|
|
|
|
|
|
|
You now have a Python context manager that redirects your output:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
with m.ostream_redirect():
|
|
|
|
m.print_to_cout_function()
|
|
|
|
|
|
|
|
This manager can optionally be told which streams to operate on:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
with m.ostream_redirect(stdout=true, stderr=true):
|
|
|
|
m.noisy_function_with_error_printing()
|
|
|
|
|
|
|
|
\endrst */
|
2021-06-19 17:53:27 +00:00
|
|
|
inline class_<detail::OstreamRedirect>
|
|
|
|
add_ostream_redirect(module_ m, const std::string &name = "ostream_redirect") {
|
|
|
|
return class_<detail::OstreamRedirect>(std::move(m), name.c_str(), module_local())
|
|
|
|
.def(init<bool, bool>(), arg("stdout") = true, arg("stderr") = true)
|
2017-08-25 00:12:43 +00:00
|
|
|
.def("__enter__", &detail::OstreamRedirect::enter)
|
2021-06-22 16:11:54 +00:00
|
|
|
.def("__exit__", [](detail::OstreamRedirect &self_, const args &) { self_.exit(); });
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|