MCPcopy Create free account
hub / github.com/pybind/pybind11 / TEST_SUBMODULE

Function TEST_SUBMODULE

tests/test_iostream.cpp:75–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73};
74
75TEST_SUBMODULE(iostream, m) {
76
77 add_ostream_redirect(m);
78
79 // test_evals
80
81 m.def("captured_output_default", [](const std::string &msg) {
82 py::scoped_ostream_redirect redir;
83 std::cout << msg << std::flush;
84 });
85
86 m.def("captured_output", [](const std::string &msg) {
87 py::scoped_ostream_redirect redir(std::cout, py::module_::import("sys").attr("stdout"));
88 std::cout << msg << std::flush;
89 });
90
91 m.def("guard_output",
92 &noisy_function,
93 py::call_guard<py::scoped_ostream_redirect>(),
94 py::arg("msg"),
95 py::arg("flush") = true);
96
97 m.def("captured_err", [](const std::string &msg) {
98 py::scoped_ostream_redirect redir(std::cerr, py::module_::import("sys").attr("stderr"));
99 std::cerr << msg << std::flush;
100 });
101
102 m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);
103
104 m.def("dual_guard",
105 &noisy_funct_dual,
106 py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
107 py::arg("msg"),
108 py::arg("emsg"));
109
110 m.def("raw_output", [](const std::string &msg) { std::cout << msg << std::flush; });
111
112 m.def("raw_err", [](const std::string &msg) { std::cerr << msg << std::flush; });
113
114 m.def("captured_dual", [](const std::string &msg, const std::string &emsg) {
115 py::scoped_ostream_redirect redirout(std::cout, py::module_::import("sys").attr("stdout"));
116 py::scoped_ostream_redirect redirerr(std::cerr, py::module_::import("sys").attr("stderr"));
117 std::cout << msg << std::flush;
118 std::cerr << emsg << std::flush;
119 });
120
121 py::class_<TestThread>(m, "TestThread")
122 .def(py::init<>())
123 .def("stop", &TestThread::stop)
124 .def("join", &TestThread::join)
125 .def("sleep", &TestThread::sleep);
126
127 m.def("move_redirect_output", [](const std::string &msg_before, const std::string &msg_after) {
128 py::scoped_ostream_redirect redir1(std::cout, py::module_::import("sys").attr("stdout"));
129 std::cout << msg_before << std::flush;
130 py::scoped_ostream_redirect redir2(std::move(redir1));
131 std::cout << msg_after << std::flush;
132 });

Callers

nothing calls this directly

Calls 5

add_ostream_redirectFunction · 0.85
importFunction · 0.85
moveFunction · 0.85
attrMethod · 0.80
argClass · 0.50

Tested by

no test coverage detected