Skip to content

Commit 58a45b5

Browse files
committed
test: add unflushed ostream redirect regression
Cover the buffered-before-move case for `scoped_ostream_redirect`, which still crashes despite the current move fix. This gives the PR a direct reproducer for the remaining bug path. Made-with: Cursor
1 parent 5b0a441 commit 58a45b5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

tests/test_iostream.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ TEST_SUBMODULE(iostream, m) {
131131
std::cout << msg_after << std::flush;
132132
});
133133

134+
m.def("move_redirect_output_unflushed",
135+
[](const std::string &msg_before, const std::string &msg_after) {
136+
py::scoped_ostream_redirect redir1(std::cout,
137+
py::module_::import("sys").attr("stdout"));
138+
std::cout << msg_before;
139+
py::scoped_ostream_redirect redir2(std::move(redir1));
140+
std::cout << msg_after << std::flush;
141+
});
142+
134143
// Redirect a stream whose original rdbuf is nullptr, then move the redirect.
135144
// Verifies that nullptr is correctly restored (not confused with a moved-from sentinel).
136145
m.def("move_redirect_null_rdbuf", [](const std::string &msg) {

tests/test_iostream.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ def test_move_redirect(capsys):
291291
assert not stderr
292292

293293

294+
def test_move_redirect_unflushed(capsys):
295+
m.move_redirect_output_unflushed("before_move", "after_move")
296+
stdout, stderr = capsys.readouterr()
297+
assert stdout == "before_moveafter_move"
298+
assert not stderr
299+
300+
294301
def test_move_redirect_null_rdbuf(capsys):
295302
m.move_redirect_null_rdbuf("hello")
296303
stdout, stderr = capsys.readouterr()

0 commit comments

Comments
 (0)