Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
076c7386
Commit
076c7386
authored
Apr 30, 2017
by
Dean Moldovan
Browse files
Add py::exec() as a shortcut for py::eval<py::eval_statements>()
parent
0c4e0372
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
12 deletions
+25
-12
docs/advanced/pycpp/utilities.rst
docs/advanced/pycpp/utilities.rst
+13
-9
include/pybind11/eval.h
include/pybind11/eval.h
+9
-0
tests/test_eval.cpp
tests/test_eval.cpp
+3
-3
No files found.
docs/advanced/pycpp/utilities.rst
View file @
076c7386
...
...
@@ -24,16 +24,10 @@ expected in Python:
Evaluating Python expressions from strings and files
====================================================
pybind11 provides the
:func:`eval` and :func:
`eval_file` functions to evaluate
pybind11 provides the
`eval`, `exec` and
`eval_file` functions to evaluate
Python expressions and statements. The following example illustrates how they
can be used.
Both functions accept a template parameter that describes how the argument
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``).
.. code-block:: cpp
// At beginning of file
...
...
@@ -48,7 +42,7 @@ is always ``none``).
int result = py::eval("my_variable + 10", scope).cast<int>();
// Evaluate a sequence of statements
py::e
val<py::eval_statements>
(
py::e
xec
(
"print('Hello')\n"
"print('world!');",
scope);
...
...
@@ -62,7 +56,7 @@ the raw string delimiter ``R"(``, ensuring all lines have common leading indent:
.. code-block:: cpp
py::e
val<py::eval_statements>
(R"(
py::e
xec
(R"(
x = get_answer()
if x == 42:
print('Hello World!')
...
...
@@ -70,3 +64,13 @@ the raw string delimiter ``R"(``, ensuring all lines have common leading indent:
print('Bye!')
)", scope
);
.. 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>``.
include/pybind11/eval.h
View file @
076c7386
...
...
@@ -62,6 +62,15 @@ object eval(const char (&s)[N], object global = object(), object local = object(
return
eval
<
mode
>
(
expr
,
global
,
local
);
}
inline
void
exec
(
str
expr
,
object
global
=
object
(),
object
local
=
object
())
{
eval
<
eval_statements
>
(
expr
,
global
,
local
);
}
template
<
size_t
N
>
void
exec
(
const
char
(
&
s
)[
N
],
object
global
=
object
(),
object
local
=
object
())
{
eval
<
eval_statements
>
(
s
,
global
,
local
);
}
template
<
eval_mode
mode
=
eval_statements
>
object
eval_file
(
str
fname
,
object
global
=
object
(),
object
local
=
object
())
{
if
(
!
global
)
{
...
...
tests/test_eval.cpp
View file @
076c7386
...
...
@@ -21,14 +21,14 @@ test_initializer eval([](py::module &m) {
});
// Regular string literal
py
::
e
val
<
py
::
eval_statements
>
(
py
::
e
xec
(
"message = 'Hello World!'
\n
"
"x = call_test()"
,
global
,
local
);
// Multi-line raw string literal
auto
result
=
py
::
eval
<
py
::
eval_statements
>
(
R"(
py
::
exec
(
R"(
if x == 42:
print(message)
else:
...
...
@@ -37,7 +37,7 @@ test_initializer eval([](py::module &m) {
);
auto
x
=
local
[
"x"
].
cast
<
int
>
();
return
result
.
is_none
()
&&
x
==
42
;
return
x
==
42
;
});
m
.
def
(
"test_eval"
,
[
global
]()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment