Blame external/pybind11/tests/test_iostream.cpp

Packit 534379
/*
Packit 534379
    tests/test_iostream.cpp -- Usage of scoped_output_redirect
Packit 534379
Packit 534379
    Copyright (c) 2017 Henry F. Schreiner
Packit 534379
Packit 534379
    All rights reserved. Use of this source code is governed by a
Packit 534379
    BSD-style license that can be found in the LICENSE file.
Packit 534379
*/
Packit 534379
Packit 534379
Packit 534379
#include <pybind11/iostream.h>
Packit 534379
#include "pybind11_tests.h"
Packit 534379
#include <iostream>
Packit 534379
Packit 534379
Packit 534379
void noisy_function(std::string msg, bool flush) {
Packit 534379
Packit 534379
    std::cout << msg;
Packit 534379
    if (flush)
Packit 534379
        std::cout << std::flush;
Packit 534379
}
Packit 534379
Packit 534379
void noisy_funct_dual(std::string msg, std::string emsg) {
Packit 534379
    std::cout << msg;
Packit 534379
    std::cerr << emsg;
Packit 534379
}
Packit 534379
Packit 534379
TEST_SUBMODULE(iostream, m) {
Packit 534379
Packit 534379
    add_ostream_redirect(m);
Packit 534379
Packit 534379
    // test_evals
Packit 534379
Packit 534379
    m.def("captured_output_default", [](std::string msg) {
Packit 534379
        py::scoped_ostream_redirect redir;
Packit 534379
        std::cout << msg << std::flush;
Packit 534379
    });
Packit 534379
Packit 534379
    m.def("captured_output", [](std::string msg) {
Packit 534379
        py::scoped_ostream_redirect redir(std::cout, py::module::import("sys").attr("stdout"));
Packit 534379
        std::cout << msg << std::flush;
Packit 534379
    });
Packit 534379
Packit 534379
    m.def("guard_output", &noisy_function,
Packit 534379
            py::call_guard<py::scoped_ostream_redirect>(),
Packit 534379
            py::arg("msg"), py::arg("flush")=true);
Packit 534379
Packit 534379
    m.def("captured_err", [](std::string msg) {
Packit 534379
        py::scoped_ostream_redirect redir(std::cerr, py::module::import("sys").attr("stderr"));
Packit 534379
        std::cerr << msg << std::flush;
Packit 534379
    });
Packit 534379
Packit 534379
    m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);
Packit 534379
Packit 534379
    m.def("dual_guard", &noisy_funct_dual,
Packit 534379
            py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
Packit 534379
            py::arg("msg"), py::arg("emsg"));
Packit 534379
Packit 534379
    m.def("raw_output", [](std::string msg) {
Packit 534379
        std::cout << msg << std::flush;
Packit 534379
    });
Packit 534379
Packit 534379
    m.def("raw_err", [](std::string msg) {
Packit 534379
        std::cerr << msg << std::flush;
Packit 534379
    });
Packit 534379
Packit 534379
    m.def("captured_dual", [](std::string msg, std::string emsg) {
Packit 534379
        py::scoped_ostream_redirect redirout(std::cout, py::module::import("sys").attr("stdout"));
Packit 534379
        py::scoped_ostream_redirect redirerr(std::cerr, py::module::import("sys").attr("stderr"));
Packit 534379
        std::cout << msg << std::flush;
Packit 534379
        std::cerr << emsg << std::flush;
Packit 534379
    });
Packit 534379
}