48 if (!traits_type::eq_int_type(c, traits_type::eof())) {
49 *pptr() = traits_type::to_char_type(c);
52 return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
59 const auto rbase = std::reverse_iterator<char *>(pbase());
60 const auto rpptr = std::reverse_iterator<char *>(pptr());
61 auto is_ascii = [](
char c) {
return (
static_cast<unsigned char>(c) & 0x80) == 0x00; };
62 auto is_leading = [](
char c) {
return (
static_cast<unsigned char>(c) & 0xC0) == 0xC0; };
63 auto is_leading_2b = [](
char c) {
return static_cast<unsigned char>(c) <= 0xDF; };
64 auto is_leading_3b = [](
char c) {
return static_cast<unsigned char>(c) <= 0xEF; };
66 if (is_ascii(*rpptr)) {
71 const auto rpend = rbase - rpptr >= 3 ? rpptr + 3 : rbase;
72 const auto leading = std::find_if(rpptr, rpend, is_leading);
73 if (leading == rbase) {
76 const auto dist =
static_cast<size_t>(leading - rpptr);
81 }
else if (dist == 1) {
82 remainder = is_leading_2b(*leading) ? 0 : dist + 1;
83 }
else if (dist == 2) {
84 remainder = is_leading_3b(*leading) ? 0 : dist + 1;
95 if (pbase() != pptr()) {
98 auto size =
static_cast<size_t>(pptr() - pbase());
99 size_t remainder = utf8_remainder();
101 if (size > remainder) {
102 str line(pbase(), size - remainder);
109 std::memmove(pbase(), pptr() - remainder, remainder);
111 setp(pbase(), epptr());
112 pbump(
static_cast<int>(remainder));
117 int sync()
override {
return _sync(); }
120 explicit pythonbuf(
const object &pyostream,
size_t buffer_size = 1024)
121 : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr(
"write")),
122 pyflush(pyostream.attr(
"flush")) {
123 setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
167 const object &pyostream
169 : costream(costream),
buffer(pyostream) {
170 old = costream.rdbuf(&
buffer);
195 const object &pyostream
211 : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
223 redirect_stdout.reset();
224 redirect_stderr.reset();
260 .
def(init<bool, bool>(),
arg(
"stdout") =
true,
arg(
"stderr") =
true)
261 .
def(
"__enter__", &detail::OstreamRedirect::enter)
262 .
def(
"__exit__", [](detail::OstreamRedirect &self_,
const args &) { self_.exit(); });
std::unique_ptr< scoped_ostream_redirect > redirect_stdout
OstreamRedirect(bool do_stdout=true, bool do_stderr=true)
std::unique_ptr< scoped_estream_redirect > redirect_stderr
class_ & def(const char *name_, Func &&f, const Extra &...extra)
Wrapper for Python extension modules.
static module_ import(const char *name)
Import and return a module or throws error_already_set.
pythonbuf(const object &pyostream, size_t buffer_size=1024)
std::unique_ptr< char[]> d_buffer
size_t utf8_remainder() const
int overflow(int c) override
pythonbuf(pythonbuf &&)=default
~pythonbuf() override
Sync before destroy.
std::streambuf::traits_type traits_type
\rst Like scoped_ostream_redirect, but redirects cerr by default.
scoped_estream_redirect(std::ostream &costream=std::cerr, const object &pyostream=module_::import("sys").attr("stderr"))
\rst This a move-only guard that redirects output.
scoped_ostream_redirect(const scoped_ostream_redirect &)=delete
scoped_ostream_redirect(std::ostream &costream=std::cout, const object &pyostream=module_::import("sys").attr("stdout"))
scoped_ostream_redirect(scoped_ostream_redirect &&other)=default
scoped_ostream_redirect & operator=(const scoped_ostream_redirect &)=delete
~scoped_ostream_redirect()
scoped_ostream_redirect & operator=(scoped_ostream_redirect &&)=delete
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
class_< detail::OstreamRedirect > add_ostream_redirect(module_ m, const std::string &name="ostream_redirect")
\rst This is a helper function to add a C++ redirect context manager to Python instead of using a C++...
Annotation for arguments.
Annotation that marks a class as local to the module:
Annotation for function names.