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 1 commit
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
4 changes: 2 additions & 2 deletions include/pybind11/iostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class pythonbuf : public std::streambuf {
{
gil_scoped_acquire tmp;

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

Expand Down
3 changes: 1 addition & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ set(PYBIND11_TEST_FILES
test_stl_binders.cpp
test_tagbased_polymorphic.cpp
test_union.cpp
test_virtual_functions.cpp
test_thread.cpp)
test_virtual_functions.cpp)

# Invoking cmake with something like:
# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" ..
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_ ) {
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
53 changes: 0 additions & 53 deletions tests/test_thread.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions tests/test_thread.py

This file was deleted.