μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_iostream.cpp
Go to the documentation of this file.
1/*
2 tests/test_iostream.cpp -- Usage of scoped_output_redirect
3
4 Copyright (c) 2017 Henry F. Schreiner
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC
11# pragma warning(disable : 4702) // unreachable code in system header (xatomic.h(382))
12#endif
13
14#include <pybind11/iostream.h>
15
16#include "pybind11_tests.h"
17
18#include <atomic>
19#include <iostream>
20#include <mutex>
21#include <string>
22#include <thread>
23
24void noisy_function(const std::string &msg, bool flush) {
25
26 std::cout << msg;
27 if (flush) {
28 std::cout << std::flush;
29 }
30}
31
32void noisy_funct_dual(const std::string &msg, const std::string &emsg) {
33 std::cout << msg;
34 std::cerr << emsg;
35}
36
37// object to manage C++ thread
38// simply repeatedly write to std::cerr until stopped
39// redirect is called at some point to test the safety of scoped_estream_redirect
40struct TestThread {
41 TestThread() : stop_{false} {
42 auto thread_f = [this] {
43 static std::mutex cout_mutex;
44 while (!stop_) {
45 {
46 // #HelpAppreciated: Work on iostream.h thread safety.
47 // Without this lock, the clang ThreadSanitizer (tsan) reliably reports a
48 // data race, and this test is predictably flakey on Windows.
49 // For more background see the discussion under
50 // https://github.com/pybind/pybind11/pull/2982 and
51 // https://github.com/pybind/pybind11/pull/2995.
52 const std::lock_guard<std::mutex> lock(cout_mutex);
53 std::cout << "x" << std::flush;
54 }
55 std::this_thread::sleep_for(std::chrono::microseconds(50));
56 }
57 };
58 t_ = new std::thread(std::move(thread_f));
59 }
60
61 ~TestThread() { delete t_; }
62
63 void stop() { stop_ = true; }
64
65 void join() const {
66 py::gil_scoped_release gil_lock;
67 t_->join();
68 }
69
70 void sleep() {
71 py::gil_scoped_release gil_lock;
72 std::this_thread::sleep_for(std::chrono::milliseconds(50));
73 }
74
75 std::thread *t_{nullptr};
76 std::atomic<bool> stop_;
77};
78
79TEST_SUBMODULE(iostream, m) {
80
82
83 // test_evals
84
85 m.def("captured_output_default", [](const std::string &msg) {
86 py::scoped_ostream_redirect redir;
87 std::cout << msg << std::flush;
88 });
89
90 m.def("captured_output", [](const std::string &msg) {
91 py::scoped_ostream_redirect redir(std::cout, py::module_::import("sys").attr("stdout"));
92 std::cout << msg << std::flush;
93 });
94
95 m.def("guard_output",
97 py::call_guard<py::scoped_ostream_redirect>(),
98 py::arg("msg"),
99 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 std::cerr << msg << std::flush;
104 });
105
106 m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);
107
108 m.def("dual_guard",
110 py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
111 py::arg("msg"),
112 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 py::scoped_ostream_redirect redirout(std::cout, py::module_::import("sys").attr("stdout"));
120 py::scoped_ostream_redirect redirerr(std::cerr, py::module_::import("sys").attr("stderr"));
121 std::cout << msg << std::flush;
122 std::cerr << emsg << std::flush;
123 });
124
125 py::class_<TestThread>(m, "TestThread")
126 .def(py::init<>())
127 .def("stop", &TestThread::stop)
128 .def("join", &TestThread::join)
129 .def("sleep", &TestThread::sleep);
130}
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++...
Definition: iostream.h:258
#define TEST_SUBMODULE(name, variable)
void noisy_funct_dual(const std::string &msg, const std::string &emsg)
void noisy_function(const std::string &msg, bool flush)
void join() const
std::thread * t_
std::atomic< bool > stop_