test_iostream.cpp 4.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
    tests/test_iostream.cpp -- Usage of scoped_output_redirect

    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.
*/

10
11
#if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC
#    pragma warning(disable : 4702)      // unreachable code in system header (xatomic.h(382))
nickbridgechess's avatar
nickbridgechess committed
12
#endif
13
14

#include <pybind11/iostream.h>
15

16
#include "pybind11_tests.h"
17

nickbridgechess's avatar
nickbridgechess committed
18
#include <atomic>
19
#include <iostream>
20
21
#include <mutex>
#include <string>
nickbridgechess's avatar
nickbridgechess committed
22
#include <thread>
23

24
void noisy_function(const std::string &msg, bool flush) {
25
26

    std::cout << msg;
27
    if (flush) {
28
        std::cout << std::flush;
29
    }
30
31
}

32
void noisy_funct_dual(const std::string &msg, const std::string &emsg) {
33
34
35
36
    std::cout << msg;
    std::cerr << emsg;
}

nickbridgechess's avatar
nickbridgechess committed
37
38
39
40
// object to manage C++ thread
// simply repeatedly write to std::cerr until stopped
// redirect is called at some point to test the safety of scoped_estream_redirect
struct TestThread {
41
    TestThread() : stop_{false} {
nickbridgechess's avatar
nickbridgechess committed
42
        auto thread_f = [this] {
43
            static std::mutex cout_mutex;
nickbridgechess's avatar
nickbridgechess committed
44
            while (!stop_) {
45
46
47
48
49
50
51
52
53
54
                {
                    // #HelpAppreciated: Work on iostream.h thread safety.
                    // Without this lock, the clang ThreadSanitizer (tsan) reliably reports a
                    // data race, and this test is predictably flakey on Windows.
                    // For more background see the discussion under
                    // https://github.com/pybind/pybind11/pull/2982 and
                    // https://github.com/pybind/pybind11/pull/2995.
                    const std::lock_guard<std::mutex> lock(cout_mutex);
                    std::cout << "x" << std::flush;
                }
nickbridgechess's avatar
nickbridgechess committed
55
                std::this_thread::sleep_for(std::chrono::microseconds(50));
56
57
            }
        };
nickbridgechess's avatar
nickbridgechess committed
58
59
60
        t_ = new std::thread(std::move(thread_f));
    }

61
    ~TestThread() { delete t_; }
nickbridgechess's avatar
nickbridgechess committed
62
63
64

    void stop() { stop_ = true; }

65
    void join() const {
nickbridgechess's avatar
nickbridgechess committed
66
67
68
69
70
71
72
73
74
        py::gil_scoped_release gil_lock;
        t_->join();
    }

    void sleep() {
        py::gil_scoped_release gil_lock;
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }

75
    std::thread *t_{nullptr};
nickbridgechess's avatar
nickbridgechess committed
76
77
78
    std::atomic<bool> stop_;
};

79
80
81
82
83
84
TEST_SUBMODULE(iostream, m) {

    add_ostream_redirect(m);

    // test_evals

85
    m.def("captured_output_default", [](const std::string &msg) {
86
87
88
89
        py::scoped_ostream_redirect redir;
        std::cout << msg << std::flush;
    });

90
    m.def("captured_output", [](const std::string &msg) {
91
        py::scoped_ostream_redirect redir(std::cout, py::module_::import("sys").attr("stdout"));
92
93
94
        std::cout << msg << std::flush;
    });

95
96
97
98
99
    m.def("guard_output",
          &noisy_function,
          py::call_guard<py::scoped_ostream_redirect>(),
          py::arg("msg"),
          py::arg("flush") = true);
100

101
    m.def("captured_err", [](const std::string &msg) {
102
        py::scoped_ostream_redirect redir(std::cerr, py::module_::import("sys").attr("stderr"));
103
104
105
106
107
        std::cerr << msg << std::flush;
    });

    m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);

108
109
110
111
112
    m.def("dual_guard",
          &noisy_funct_dual,
          py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
          py::arg("msg"),
          py::arg("emsg"));
113

114
    m.def("raw_output", [](const std::string &msg) { std::cout << msg << std::flush; });
115

116
    m.def("raw_err", [](const std::string &msg) { std::cerr << msg << std::flush; });
117

118
    m.def("captured_dual", [](const std::string &msg, const std::string &emsg) {
119
120
        py::scoped_ostream_redirect redirout(std::cout, py::module_::import("sys").attr("stdout"));
        py::scoped_ostream_redirect redirerr(std::cerr, py::module_::import("sys").attr("stderr"));
121
122
123
        std::cout << msg << std::flush;
        std::cerr << emsg << std::flush;
    });
nickbridgechess's avatar
nickbridgechess committed
124
125
126
127
128
129

    py::class_<TestThread>(m, "TestThread")
        .def(py::init<>())
        .def("stop", &TestThread::stop)
        .def("join", &TestThread::join)
        .def("sleep", &TestThread::sleep);
130
}