-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Redirect std::cout #1005
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
Comments
I'm assuming in such a case Python's I'm mostly just thinking out loud here, but I think it would be nifty if it adapted a RAII-type structure (like m.def("noisy_func", []() {
py::scoped_output_redirect redir(std::cout, py::module::import("sys").attr("stdout"));
call_noisy_func();
}); and have |
That might be pretty easy to implement with the system I wrote, let me give it a try. Being able to redirect globally is useful too, but maybe the latter is more in line with PyBind's design. In my case, I think only a few functions are noisy (not sure how often that's the case, but I expect it is somewhat common). |
You could do it globally (at least, as globally as the module loaded within the Python interpreter) with something like: m.attr("redirect_output") = py::capsule(new py::scoped_output_redirect(...),
[](void *sor) { delete static_cast<py::scoped_output_redirect *>(sor); }); or for an uglier but simpler version you could just leak the instance: new py::scoped_output_redirect(...); (though the latter would probably not work nicely if anything tried to print after the Python interpreter has gone away, i.e. during global destruction or after an embedded interpreter shutdown). |
Squashed commit of the following: commit 3f55f7c Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 09:35:47 2017 -0400 Fix short underline in docs commit bc8ac7d Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 08:53:15 2017 -0400 Typo fix in test commit e53f073 Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 08:43:12 2017 -0400 Linting fixes commit d8f47fb Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 00:13:43 2017 -0400 Linting fixes commit 84fe118 Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 00:11:25 2017 -0400 Docs typo fixed from lintig commit 3187674 Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 00:03:11 2017 -0400 Fixing Pypy tests commit 6a97ecc Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 23:26:16 2017 -0400 Minor cleanup from style lint commit c1c14c9 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:56:35 2017 -0400 Changelog update commit 57d1d88 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:53:37 2017 -0400 Adding tests for psudo-global iostream wrapping commit bcb6cc8 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:34:08 2017 -0400 Adding docs and triple-test commit 54993c9 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:21:08 2017 -0400 Adding method suggested in pybind#1005
Squashed commit of the following: commit 3f55f7c Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 09:35:47 2017 -0400 Fix short underline in docs commit bc8ac7d Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 08:53:15 2017 -0400 Typo fix in test commit e53f073 Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 08:43:12 2017 -0400 Linting fixes commit d8f47fb Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 00:13:43 2017 -0400 Linting fixes commit 84fe118 Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 00:11:25 2017 -0400 Docs typo fixed from lintig commit 3187674 Author: Henry Fredrick Schreiner <[email protected]> Date: Fri Aug 18 00:03:11 2017 -0400 Fixing Pypy tests commit 6a97ecc Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 23:26:16 2017 -0400 Minor cleanup from style lint commit c1c14c9 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:56:35 2017 -0400 Changelog update commit 57d1d88 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:53:37 2017 -0400 Adding tests for psudo-global iostream wrapping commit bcb6cc8 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:34:08 2017 -0400 Adding docs and triple-test commit 54993c9 Author: Henry Fredrick Schreiner <[email protected]> Date: Thu Aug 17 22:21:08 2017 -0400 Adding method suggested in pybind#1005
I'm interested in redirecting
std::cout
for my functions (some of which are long running library functions that report on progress). Is there an officially sanctioned way to redirectstd::cout
to python's stdout? (other than the custompybind11::print
function)? Running inside of a Jupyter notebook, for example, requires the output go to Python's stdout to be visible.After lots of googling and some playing around, I've come up with the following solution, which works well. I can make into a PR if this is useful and not already available. It's at https://github.com/GooFit/GooFit/blob/573044ea20eedc8444f38cc6a7ce66bef27d2524/python/Print.cpp .
The text was updated successfully, but these errors were encountered: