2017-08-25 00:12:43 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-11-19 11:09:33 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC
|
|
|
|
# pragma warning(disable: 4702) // unreachable code in system header (xatomic.h(382))
|
|
|
|
#endif
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
#include <pybind11/iostream.h>
|
|
|
|
#include "pybind11_tests.h"
|
2020-11-19 11:09:33 +00:00
|
|
|
#include <atomic>
|
2017-08-25 00:12:43 +00:00
|
|
|
#include <iostream>
|
2021-07-12 20:39:06 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2020-11-19 11:09:33 +00:00
|
|
|
#include <thread>
|
2017-08-25 00:12:43 +00:00
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
void noisy_function(const std::string &msg, bool flush) {
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
std::cout << msg;
|
|
|
|
if (flush)
|
|
|
|
std::cout << std::flush;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
void noisy_funct_dual(const std::string &msg, const std::string &emsg) {
|
2017-08-25 00:12:43 +00:00
|
|
|
std::cout << msg;
|
|
|
|
std::cerr << emsg;
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:09:33 +00:00
|
|
|
// 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 {
|
2021-06-21 14:37:48 +00:00
|
|
|
TestThread() : stop_{false} {
|
2020-11-19 11:09:33 +00:00
|
|
|
auto thread_f = [this] {
|
2021-07-12 20:39:06 +00:00
|
|
|
static std::mutex cout_mutex;
|
2020-11-19 11:09:33 +00:00
|
|
|
while (!stop_) {
|
2021-07-12 20:39:06 +00:00
|
|
|
{
|
|
|
|
// #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;
|
|
|
|
}
|
2020-11-19 11:09:33 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(50));
|
|
|
|
} };
|
|
|
|
t_ = new std::thread(std::move(thread_f));
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestThread() {
|
|
|
|
delete t_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop() { stop_ = true; }
|
|
|
|
|
2021-06-21 14:37:48 +00:00
|
|
|
void join() const {
|
2020-11-19 11:09:33 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:37:48 +00:00
|
|
|
std::thread *t_{nullptr};
|
2020-11-19 11:09:33 +00:00
|
|
|
std::atomic<bool> stop_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-08-25 00:12:43 +00:00
|
|
|
TEST_SUBMODULE(iostream, m) {
|
|
|
|
|
|
|
|
add_ostream_redirect(m);
|
|
|
|
|
|
|
|
// test_evals
|
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("captured_output_default", [](const std::string &msg) {
|
2017-08-25 00:12:43 +00:00
|
|
|
py::scoped_ostream_redirect redir;
|
|
|
|
std::cout << msg << std::flush;
|
|
|
|
});
|
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("captured_output", [](const std::string &msg) {
|
2020-10-03 17:38:03 +00:00
|
|
|
py::scoped_ostream_redirect redir(std::cout, py::module_::import("sys").attr("stdout"));
|
2017-08-25 00:12:43 +00:00
|
|
|
std::cout << msg << std::flush;
|
|
|
|
});
|
|
|
|
|
|
|
|
m.def("guard_output", &noisy_function,
|
|
|
|
py::call_guard<py::scoped_ostream_redirect>(),
|
|
|
|
py::arg("msg"), py::arg("flush")=true);
|
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("captured_err", [](const std::string &msg) {
|
2020-10-03 17:38:03 +00:00
|
|
|
py::scoped_ostream_redirect redir(std::cerr, py::module_::import("sys").attr("stderr"));
|
2017-08-25 00:12:43 +00:00
|
|
|
std::cerr << msg << std::flush;
|
|
|
|
});
|
|
|
|
|
|
|
|
m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);
|
|
|
|
|
|
|
|
m.def("dual_guard", &noisy_funct_dual,
|
|
|
|
py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
|
|
|
|
py::arg("msg"), py::arg("emsg"));
|
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("raw_output", [](const std::string &msg) { std::cout << msg << std::flush; });
|
2017-08-25 00:12:43 +00:00
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("raw_err", [](const std::string &msg) { std::cerr << msg << std::flush; });
|
2017-08-25 00:12:43 +00:00
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("captured_dual", [](const std::string &msg, const std::string &emsg) {
|
2020-10-03 17:38:03 +00:00
|
|
|
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"));
|
2017-08-25 00:12:43 +00:00
|
|
|
std::cout << msg << std::flush;
|
|
|
|
std::cerr << emsg << std::flush;
|
|
|
|
});
|
2020-11-19 11:09:33 +00:00
|
|
|
|
|
|
|
py::class_<TestThread>(m, "TestThread")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def("stop", &TestThread::stop)
|
|
|
|
.def("join", &TestThread::join)
|
|
|
|
.def("sleep", &TestThread::sleep);
|
2017-08-25 00:12:43 +00:00
|
|
|
}
|