112391Sjason@lowepower.com/*
212391Sjason@lowepower.com    tests/test_iostream.cpp -- Usage of scoped_output_redirect
312391Sjason@lowepower.com
412391Sjason@lowepower.com    Copyright (c) 2017 Henry F. Schreiner
512391Sjason@lowepower.com
612391Sjason@lowepower.com    All rights reserved. Use of this source code is governed by a
712391Sjason@lowepower.com    BSD-style license that can be found in the LICENSE file.
812391Sjason@lowepower.com*/
912391Sjason@lowepower.com
1012391Sjason@lowepower.com
1112391Sjason@lowepower.com#include <pybind11/iostream.h>
1212391Sjason@lowepower.com#include "pybind11_tests.h"
1312391Sjason@lowepower.com#include <iostream>
1412391Sjason@lowepower.com
1512391Sjason@lowepower.com
1612391Sjason@lowepower.comvoid noisy_function(std::string msg, bool flush) {
1712391Sjason@lowepower.com
1812391Sjason@lowepower.com    std::cout << msg;
1912391Sjason@lowepower.com    if (flush)
2012391Sjason@lowepower.com        std::cout << std::flush;
2112391Sjason@lowepower.com}
2212391Sjason@lowepower.com
2312391Sjason@lowepower.comvoid noisy_funct_dual(std::string msg, std::string emsg) {
2412391Sjason@lowepower.com    std::cout << msg;
2512391Sjason@lowepower.com    std::cerr << emsg;
2612391Sjason@lowepower.com}
2712391Sjason@lowepower.com
2812391Sjason@lowepower.comTEST_SUBMODULE(iostream, m) {
2912391Sjason@lowepower.com
3012391Sjason@lowepower.com    add_ostream_redirect(m);
3112391Sjason@lowepower.com
3212391Sjason@lowepower.com    // test_evals
3312391Sjason@lowepower.com
3412391Sjason@lowepower.com    m.def("captured_output_default", [](std::string msg) {
3512391Sjason@lowepower.com        py::scoped_ostream_redirect redir;
3612391Sjason@lowepower.com        std::cout << msg << std::flush;
3712391Sjason@lowepower.com    });
3812391Sjason@lowepower.com
3912391Sjason@lowepower.com    m.def("captured_output", [](std::string msg) {
4012391Sjason@lowepower.com        py::scoped_ostream_redirect redir(std::cout, py::module::import("sys").attr("stdout"));
4112391Sjason@lowepower.com        std::cout << msg << std::flush;
4212391Sjason@lowepower.com    });
4312391Sjason@lowepower.com
4412391Sjason@lowepower.com    m.def("guard_output", &noisy_function,
4512391Sjason@lowepower.com            py::call_guard<py::scoped_ostream_redirect>(),
4612391Sjason@lowepower.com            py::arg("msg"), py::arg("flush")=true);
4712391Sjason@lowepower.com
4812391Sjason@lowepower.com    m.def("captured_err", [](std::string msg) {
4912391Sjason@lowepower.com        py::scoped_ostream_redirect redir(std::cerr, py::module::import("sys").attr("stderr"));
5012391Sjason@lowepower.com        std::cerr << msg << std::flush;
5112391Sjason@lowepower.com    });
5212391Sjason@lowepower.com
5312391Sjason@lowepower.com    m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);
5412391Sjason@lowepower.com
5512391Sjason@lowepower.com    m.def("dual_guard", &noisy_funct_dual,
5612391Sjason@lowepower.com            py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
5712391Sjason@lowepower.com            py::arg("msg"), py::arg("emsg"));
5812391Sjason@lowepower.com
5912391Sjason@lowepower.com    m.def("raw_output", [](std::string msg) {
6012391Sjason@lowepower.com        std::cout << msg << std::flush;
6112391Sjason@lowepower.com    });
6212391Sjason@lowepower.com
6312391Sjason@lowepower.com    m.def("raw_err", [](std::string msg) {
6412391Sjason@lowepower.com        std::cerr << msg << std::flush;
6512391Sjason@lowepower.com    });
6612391Sjason@lowepower.com
6712391Sjason@lowepower.com    m.def("captured_dual", [](std::string msg, std::string emsg) {
6812391Sjason@lowepower.com        py::scoped_ostream_redirect redirout(std::cout, py::module::import("sys").attr("stdout"));
6912391Sjason@lowepower.com        py::scoped_ostream_redirect redirerr(std::cerr, py::module::import("sys").attr("stderr"));
7012391Sjason@lowepower.com        std::cout << msg << std::flush;
7112391Sjason@lowepower.com        std::cerr << emsg << std::flush;
7212391Sjason@lowepower.com    });
7312391Sjason@lowepower.com}
74