μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Classes | Functions
iostream.h File Reference
#include "pybind11.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>
#include <memory>
#include <ostream>
#include <streambuf>
#include <string>
#include <utility>
Include dependency graph for iostream.h:

Go to the source code of this file.

Classes

class  pythonbuf
 
class  scoped_ostream_redirect
 \rst This a move-only guard that redirects output. More...
 
class  scoped_estream_redirect
 \rst Like scoped_ostream_redirect, but redirects cerr by default. More...
 
class  OstreamRedirect
 

Functions

class_< detail::OstreamRedirect > add_ostream_redirect (module_ m, const std::string &name="ostream_redirect")
 \rst This is a helper function to add a C++ redirect context manager to Python instead of using a C++ guard. More...
 

Function Documentation

◆ add_ostream_redirect()

class_< detail::OstreamRedirect > add_ostream_redirect ( module_  m,
const std::string &  name = "ostream_redirect" 
)
inline

\rst This is a helper function to add a C++ redirect context manager to Python instead of using a C++ guard.

To use it, add the following to your binding code:

.. code-block:: cpp

#include <pybind11/iostream.h>

...

py::add_ostream_redirect(m, "ostream_redirect");

You now have a Python context manager that redirects your output:

.. code-block:: python

with m.ostream_redirect():
    m.print_to_cout_function()

This manager can optionally be told which streams to operate on:

.. code-block:: python

with m.ostream_redirect(stdout=true, stderr=true):
    m.noisy_function_with_error_printing()

\endrst

Definition at line 258 of file iostream.h.

References class_< type_, options >::def().

Referenced by TEST_SUBMODULE().