utilities.rst 5.58 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Utilities
#########

Using Python's print function in C++
====================================

The usual way to write output in C++ is using ``std::cout`` while in Python one
would use ``print``. Since these methods use different buffers, mixing them can
lead to output order issues. To resolve this, pybind11 modules can use the
:func:`py::print` function which writes to Python's ``sys.stdout`` for consistency.

Python's ``print`` function is replicated in the C++ API including optional
keyword arguments ``sep``, ``end``, ``file``, ``flush``. Everything works as
expected in Python:

.. code-block:: cpp

    py::print(1, 2.0, "three"); // 1 2.0 three
    py::print(1, 2.0, "three", "sep"_a="-"); // 1-2.0-three

    auto args = py::make_tuple("unpacked", true);
    py::print("->", *args, "end"_a="<-"); // -> unpacked True <-

24
25
26
27
28
29
30
.. _ostream_redirect:

Capturing standard output from ostream
======================================

Often, a library will use the streams ``std::cout`` and ``std::cerr`` to print,
but this does not play well with Python's standard ``sys.stdout`` and ``sys.stderr``
31
redirection. Replacing a library's printing with ``py::print <print>`` may not
32
33
34
35
36
37
38
39
40
41
42
43
44
be feasible. This can be fixed using a guard around the library function that
redirects output to the corresponding Python streams:

.. code-block:: cpp

    #include <pybind11/iostream.h>

    ...

    // Add a scoped redirect for your noisy code
    m.def("noisy_func", []() {
        py::scoped_ostream_redirect stream(
            std::cout,                               // std::ostream&
45
            py::module_::import("sys").attr("stdout") // Python output
46
47
48
49
        );
        call_noisy_func();
    });

50
51
52
53
.. warning::

    The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple
    threads writing to a redirected ostream concurrently cause data races
54
    and potentially buffer overflows. Therefore it is currently a requirement
55
56
57
58
59
60
    that all (possibly) concurrent redirected ostream writes are protected by
    a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more
    background see the discussions under
    `PR #2982 <https://github.com/pybind/pybind11/pull/2982>`_ and
    `PR #2995 <https://github.com/pybind/pybind11/pull/2995>`_.

61
62
63
64
This method respects flushes on the output streams and will flush if needed
when the scoped guard is destroyed. This allows the output to be redirected in
real time, such as to a Jupyter notebook. The two arguments, the C++ stream and
the Python output, are optional, and default to standard output if not given. An
65
extra type, ``py::scoped_estream_redirect <scoped_estream_redirect>``, is identical
66
except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with
67
``py::call_guard``, which allows multiple items, but uses the default constructor:
68

69
.. code-block:: cpp
70
71
72
73
74
75
76

    // Alternative: Call single function using call guard
    m.def("noisy_func", &call_noisy_function,
          py::call_guard<py::scoped_ostream_redirect,
                         py::scoped_estream_redirect>());

The redirection can also be done in Python with the addition of a context
77
manager, using the ``py::add_ostream_redirect() <add_ostream_redirect>`` function:
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

.. code-block:: cpp

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

The name in Python defaults to ``ostream_redirect`` if no name is passed.  This
creates the following context manager in Python:

.. code-block:: python

    with ostream_redirect(stdout=True, stderr=True):
        noisy_function()

It defaults to redirecting both streams, though you can use the keyword
arguments to disable one of the streams if needed.

.. note::

    The above methods will not redirect C-level output to file descriptors, such
    as ``fprintf``. For those cases, you'll need to redirect the file
    descriptors either directly in C or with Python's ``os.dup2`` function
    in an operating-system dependent way.

101
102
.. _eval:

Dean Moldovan's avatar
Dean Moldovan committed
103
104
105
Evaluating Python expressions from strings and files
====================================================

106
pybind11 provides the ``eval``, ``exec`` and ``eval_file`` functions to evaluate
Dean Moldovan's avatar
Dean Moldovan committed
107
108
109
110
111
112
113
114
115
116
117
Python expressions and statements. The following example illustrates how they
can be used.

.. code-block:: cpp

    // At beginning of file
    #include <pybind11/eval.h>

    ...

    // Evaluate in scope of main module
118
    py::object scope = py::module_::import("__main__").attr("__dict__");
Dean Moldovan's avatar
Dean Moldovan committed
119
120
121
122
123

    // Evaluate an isolated expression
    int result = py::eval("my_variable + 10", scope).cast<int>();

    // Evaluate a sequence of statements
124
    py::exec(
Dean Moldovan's avatar
Dean Moldovan committed
125
126
127
128
129
130
        "print('Hello')\n"
        "print('world!');",
        scope);

    // Evaluate the statements in an separate Python file on disk
    py::eval_file("script.py", scope);
131
132
133
134
135
136
137

C++11 raw string literals are also supported and quite handy for this purpose.
The only requirement is that the first statement must be on a new line following
the raw string delimiter ``R"(``, ensuring all lines have common leading indent:

.. code-block:: cpp

138
    py::exec(R"(
139
140
141
142
143
144
145
        x = get_answer()
        if x == 42:
            print('Hello World!')
        else:
            print('Bye!')
        )", scope
    );
146
147
148
149
150
151
152
153
154
155

.. note::

    `eval` and `eval_file` accept a template parameter that describes how the
    string/file should be interpreted. Possible choices include ``eval_expr``
    (isolated expression), ``eval_single_statement`` (a single statement, return
    value is always ``none``), and ``eval_statements`` (sequence of statements,
    return value is always ``none``). `eval` defaults to  ``eval_expr``,
    `eval_file` defaults to ``eval_statements`` and `exec` is just a shortcut
    for ``eval<eval_statements>``.