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);
103 pywrite(std::move(line));
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();
261 .def(
"__enter__", &detail::OstreamRedirect::enter)
262 .
def(
"__exit__", [](detail::OstreamRedirect &self_,
const args &) { self_.exit(); });
std::streambuf::traits_type traits_type
~pythonbuf() override
Sync before destroy.
scoped_estream_redirect(std::ostream &costream=std::cerr, const object &pyostream=module_::import("sys").attr("stderr"))
class_ & def(const char *name_, Func &&f, const Extra &...extra)
Wrapper for Python extension modules.
class_< detail::OstreamRedirect > add_ostream_redirect(module_ m, const std::string &name="ostream_redirect")
int overflow(int c) override
pythonbuf(const object &pyostream, size_t buffer_size=1024)
#define PYBIND11_NAMESPACE
Annotation that marks a class as local to the module:
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
std::unique_ptr< scoped_estream_redirect > redirect_stderr
scoped_ostream_redirect(std::ostream &costream=std::cout, const object &pyostream=module_::import("sys").attr("stdout"))
std::unique_ptr< char[]> d_buffer
~scoped_ostream_redirect()
std::unique_ptr< scoped_ostream_redirect > redirect_stdout
static module_ import(const char *name)
Import and return a module or throws error_already_set.
OstreamRedirect(bool do_stdout=true, bool do_stderr=true)
Annotation for function names.
size_t utf8_remainder() const
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)