mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
pythonbuf fix (#2675)
* Added test_thread testing for ostream_redirect segfault recreation
* fix: scoped_ostream_redirect str created outside gil
* Moved threading tests into test_iostream. Cleaned up some formatting. Deleted test_thread.{cpp,py}
* CI: few formatting fixes
* CI: yet another formatting fix
* CI: more formatting fixes. Removed unecessary comment
* Ignore 'warning C4702: unreachable code' in MSVC 2015
Co-authored-by: Nick Bridge <nick.bridge.chess@gmail.com>
Co-authored-by: Nick Bridge <nbridge@jumptrading.com>
Co-authored-by: Yannick Jadoul <yannick.jadoul@belgacom.net>
This commit is contained in:
@@ -7,10 +7,15 @@
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC
|
||||
# pragma warning(disable: 4702) // unreachable code in system header (xatomic.h(382))
|
||||
#endif
|
||||
|
||||
#include <pybind11/iostream.h>
|
||||
#include "pybind11_tests.h"
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
|
||||
void noisy_function(std::string msg, bool flush) {
|
||||
@@ -25,6 +30,40 @@ void noisy_funct_dual(std::string msg, std::string emsg) {
|
||||
std::cerr << emsg;
|
||||
}
|
||||
|
||||
// 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 {
|
||||
TestThread() : t_{nullptr}, stop_{false} {
|
||||
auto thread_f = [this] {
|
||||
while (!stop_) {
|
||||
std::cout << "x" << std::flush;
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(50));
|
||||
} };
|
||||
t_ = new std::thread(std::move(thread_f));
|
||||
}
|
||||
|
||||
~TestThread() {
|
||||
delete t_;
|
||||
}
|
||||
|
||||
void stop() { stop_ = true; }
|
||||
|
||||
void join() {
|
||||
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));
|
||||
}
|
||||
|
||||
std::thread * t_;
|
||||
std::atomic<bool> stop_;
|
||||
};
|
||||
|
||||
|
||||
TEST_SUBMODULE(iostream, m) {
|
||||
|
||||
add_ostream_redirect(m);
|
||||
@@ -70,4 +109,10 @@ TEST_SUBMODULE(iostream, m) {
|
||||
std::cout << msg << std::flush;
|
||||
std::cerr << emsg << std::flush;
|
||||
});
|
||||
|
||||
py::class_<TestThread>(m, "TestThread")
|
||||
.def(py::init<>())
|
||||
.def("stop", &TestThread::stop)
|
||||
.def("join", &TestThread::join)
|
||||
.def("sleep", &TestThread::sleep);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user