Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pythonbuf fix #2675

Merged
merged 7 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/pybind11/iostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ class pythonbuf : public std::streambuf {
// simplified to a fully qualified call.
int _sync() {
if (pbase() != pptr()) {
// This subtraction cannot be negative, so dropping the sign
str line(pbase(), static_cast<size_t>(pptr() - pbase()));

{
gil_scoped_acquire tmp;

// This subtraction cannot be negative, so dropping the sign.
// This invokes python and therefore should be included in the
// GIL
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved
str line(pbase(), static_cast<size_t>(pptr() - pbase()));

pywrite(line);
pyflush();
}
Expand Down
45 changes: 45 additions & 0 deletions tests/test_iostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <pybind11/iostream.h>
#include "pybind11_tests.h"
#include <iostream>
#include <thread>
#include <atomic>
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved


void noisy_function(std::string msg, bool flush) {
Expand All @@ -25,6 +27,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(!this->stop_) {
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved
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_ = 1; }

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);
Expand Down Expand Up @@ -70,4 +106,13 @@ 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);
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved
}
23 changes: 23 additions & 0 deletions tests/test_iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,26 @@ def test_redirect_both(capfd):
assert stderr == ""
assert stream.getvalue() == msg
assert stream2.getvalue() == msg2


def test_threading():
with m.ostream_redirect(stdout=True, stderr=False):
# start some threads
threads = []

# start some threads
for _j in range(20):
threads.append(m.TestThread())

# give the threads some time to fail
threads[0].sleep()

# stop all the threads
for t in threads:
t.stop()

for t in threads:
t.join()

# if a thread segfaults, we don't get here
assert True