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 2 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
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ set(PYBIND11_TEST_FILES
test_stl_binders.cpp
test_tagbased_polymorphic.cpp
test_union.cpp
test_virtual_functions.cpp)
test_virtual_functions.cpp
test_thread.cpp)
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved

# Invoking cmake with something like:
# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" ..
Expand Down
53 changes: 53 additions & 0 deletions tests/test_thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
tests/test_tread.cpp -- thread handling

Copyright (c) 2020 Nick Bridge <nick.bridge.chess@gmail.com>

All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/

#include "pybind11_tests.h"
#include <thread>
#include <atomic>
#include <iostream>
#include <pybind11/iostream.h>


TEST_SUBMODULE(test_threading, m) {
// 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] {
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved
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_;
};

py::class_<TestThread>(m, "TestThread")
.def(py::init<>())
.def("stop", &TestThread::stop)
.def("join", &TestThread::join)
.def("sleep", &TestThread::sleep);

}

28 changes: 28 additions & 0 deletions tests/test_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
import pytest
from pybind11_tests import test_threading as m
from pybind11_tests import iostream
import time

def test_threading():
with iostream.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