Skip to content

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

Closed
henryiii opened this issue Aug 17, 2017 · 3 comments · Fixed by #1009
Closed

Redirect std::cout #1005

henryiii opened this issue Aug 17, 2017 · 3 comments · Fixed by #1009

Comments

@henryiii
Copy link
Collaborator

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 redirect std::cout to python's stdout? (other than the custom pybind11::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 .

@jagerman
Copy link
Member

I'm assuming in such a case Python's stdout has been redirected somewhere else, but only at the Python level, not the C level.

I'm mostly just thinking out loud here, but I think it would be nifty if it adapted a RAII-type structure (like py::scoped_interpreter_guard, py::gil_scoped_acquire, etc.), so that you could do something like:

m.def("noisy_func", []() {
    py::scoped_output_redirect redir(std::cout, py::module::import("sys").attr("stdout"));
    call_noisy_func();
});

and have std::cout restored at the end of the call.

@henryiii
Copy link
Collaborator Author

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).

@jagerman
Copy link
Member

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).

henryiii added a commit to henryiii/pybind11 that referenced this issue Aug 18, 2017
henryiii added a commit to henryiii/pybind11 that referenced this issue Aug 23, 2017
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
dean0x7d pushed a commit to henryiii/pybind11 that referenced this issue Aug 24, 2017
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants