2024-06-22 04:55:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
import sys
|
2022-02-11 02:28:08 +00:00
|
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
|
|
from io import StringIO
|
2017-08-25 00:12:43 +00:00
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-04-09 13:55:16 +00:00
|
|
|
import env # noqa: F401
|
2021-08-13 16:37:05 +00:00
|
|
|
from pybind11_tests import iostream as m
|
|
|
|
|
2024-04-09 13:55:16 +00:00
|
|
|
pytestmark = pytest.mark.skipif(
|
|
|
|
"env.GRAALPY",
|
|
|
|
reason="Delayed prints from finalizers from other tests can end up in the output",
|
|
|
|
)
|
|
|
|
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
def test_captured(capsys):
|
|
|
|
msg = "I've been redirected to Python, I hope!"
|
|
|
|
m.captured_output(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
m.captured_err(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stderr == msg
|
|
|
|
|
2018-08-29 10:10:48 +00:00
|
|
|
|
2018-08-29 09:48:30 +00:00
|
|
|
def test_captured_large_string(capsys):
|
|
|
|
# Make this bigger than the buffer used on the C++ side: 1024 chars
|
|
|
|
msg = "I've been redirected to Python, I hope!"
|
|
|
|
msg = msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2018-08-29 09:48:30 +00:00
|
|
|
|
2017-08-25 00:12:43 +00:00
|
|
|
|
2021-05-04 05:04:38 +00:00
|
|
|
def test_captured_utf8_2byte_offset0(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\u07ff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_2byte_offset1(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\u07ff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "1" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_3byte_offset0(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\uffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_3byte_offset1(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\uffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "1" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_3byte_offset2(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\uffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "12" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_4byte_offset0(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\U0010ffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_4byte_offset1(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\U0010ffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "1" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_4byte_offset2(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\U0010ffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "12" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_captured_utf8_4byte_offset3(capsys):
|
2024-04-02 15:14:08 +00:00
|
|
|
msg = "\U0010ffff"
|
2021-05-04 05:04:38 +00:00
|
|
|
msg = "123" + msg * (1024 // len(msg) + 1)
|
|
|
|
|
|
|
|
m.captured_output_default(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2021-05-04 05:04:38 +00:00
|
|
|
|
|
|
|
|
2017-08-25 00:12:43 +00:00
|
|
|
def test_guard_capture(capsys):
|
|
|
|
msg = "I've been redirected to Python, I hope!"
|
|
|
|
m.guard_output(msg)
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_series_captured(capture):
|
|
|
|
with capture:
|
|
|
|
m.captured_output("a")
|
|
|
|
m.captured_output("b")
|
|
|
|
assert capture == "ab"
|
|
|
|
|
|
|
|
|
|
|
|
def test_flush(capfd):
|
|
|
|
msg = "(not flushed)"
|
|
|
|
msg2 = "(flushed)"
|
|
|
|
|
|
|
|
with m.ostream_redirect():
|
|
|
|
m.noisy_function(msg, flush=False)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
m.noisy_function(msg2, flush=True)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
|
|
|
assert stdout == msg + msg2
|
|
|
|
|
|
|
|
m.noisy_function(msg, flush=False)
|
|
|
|
|
|
|
|
stdout, stderr = capfd.readouterr()
|
|
|
|
assert stdout == msg
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_captured(capfd):
|
|
|
|
msg = "Something that should not show up in log"
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stdout(stream):
|
|
|
|
m.raw_output(msg)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
|
|
|
assert not stream.getvalue()
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stdout(stream):
|
|
|
|
m.captured_output(msg)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stream.getvalue() == msg
|
|
|
|
|
|
|
|
|
|
|
|
def test_err(capfd):
|
|
|
|
msg = "Something that should not show up in log"
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stderr(stream):
|
|
|
|
m.raw_err(msg)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stderr == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stream.getvalue()
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stderr(stream):
|
|
|
|
m.captured_err(msg)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stream.getvalue() == msg
|
|
|
|
|
|
|
|
|
|
|
|
def test_multi_captured(capfd):
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stdout(stream):
|
|
|
|
m.captured_output("a")
|
|
|
|
m.raw_output("b")
|
|
|
|
m.captured_output("c")
|
|
|
|
m.raw_output("d")
|
|
|
|
stdout, stderr = capfd.readouterr()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert stdout == "bd"
|
|
|
|
assert stream.getvalue() == "ac"
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_dual(capsys):
|
|
|
|
m.captured_dual("a", "b")
|
|
|
|
stdout, stderr = capsys.readouterr()
|
|
|
|
assert stdout == "a"
|
|
|
|
assert stderr == "b"
|
|
|
|
|
|
|
|
|
|
|
|
def test_redirect(capfd):
|
|
|
|
msg = "Should not be in log!"
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stdout(stream):
|
|
|
|
m.raw_output(msg)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stream.getvalue()
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
stream = StringIO()
|
2023-02-22 14:18:55 +00:00
|
|
|
with redirect_stdout(stream), m.ostream_redirect():
|
|
|
|
m.raw_output(msg)
|
2017-08-25 00:12:43 +00:00
|
|
|
stdout, stderr = capfd.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stream.getvalue() == msg
|
|
|
|
|
|
|
|
stream = StringIO()
|
|
|
|
with redirect_stdout(stream):
|
|
|
|
m.raw_output(msg)
|
|
|
|
stdout, stderr = capfd.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stream.getvalue()
|
2017-08-25 00:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_redirect_err(capfd):
|
|
|
|
msg = "StdOut"
|
|
|
|
msg2 = "StdErr"
|
|
|
|
|
|
|
|
stream = StringIO()
|
2023-02-22 14:18:55 +00:00
|
|
|
with redirect_stderr(stream), m.ostream_redirect(stdout=False):
|
|
|
|
m.raw_output(msg)
|
|
|
|
m.raw_err(msg2)
|
2017-08-25 00:12:43 +00:00
|
|
|
stdout, stderr = capfd.readouterr()
|
|
|
|
assert stdout == msg
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stream.getvalue() == msg2
|
|
|
|
|
|
|
|
|
|
|
|
def test_redirect_both(capfd):
|
|
|
|
msg = "StdOut"
|
|
|
|
msg2 = "StdErr"
|
|
|
|
|
|
|
|
stream = StringIO()
|
|
|
|
stream2 = StringIO()
|
2023-02-22 14:18:55 +00:00
|
|
|
with redirect_stdout(stream), redirect_stderr(stream2), m.ostream_redirect():
|
|
|
|
m.raw_output(msg)
|
|
|
|
m.raw_err(msg2)
|
2017-08-25 00:12:43 +00:00
|
|
|
stdout, stderr = capfd.readouterr()
|
2023-04-28 18:32:32 +00:00
|
|
|
assert not stdout
|
|
|
|
assert not stderr
|
2017-08-25 00:12:43 +00:00
|
|
|
assert stream.getvalue() == msg
|
|
|
|
assert stream2.getvalue() == msg2
|
2020-11-19 11:09:33 +00:00
|
|
|
|
|
|
|
|
2024-07-18 18:50:38 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads")
|
2020-11-19 11:09:33 +00:00
|
|
|
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
|