mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-29 08:32:02 +00:00
complete rewrite of eval/exec patch
This commit is contained in:
parent
c6ad2c4993
commit
0d3fc3566a
@ -1612,32 +1612,38 @@ work, it is important that all lines are indented consistently, i.e.:
|
|||||||
.. [#f4] http://www.sphinx-doc.org
|
.. [#f4] http://www.sphinx-doc.org
|
||||||
.. [#f5] http://github.com/pybind/python_example
|
.. [#f5] http://github.com/pybind/python_example
|
||||||
|
|
||||||
Calling Python from C++
|
Evaluating Python expressions from strings and files
|
||||||
=======================
|
====================================================
|
||||||
|
|
||||||
Pybind11 also allows to call python code from C++. Note that this code assumes, that the intepreter is already initialized.
|
pybind11 provides the :func:`eval` and :func:`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
|
.. code-block:: cpp
|
||||||
|
|
||||||
// get the main module, so we can access and declare stuff
|
// At beginning of file
|
||||||
py::module main_module = py::module::import("__main__");
|
#include <pybind11/eval.h>
|
||||||
|
|
||||||
//get the main namespace, so I can declare variables
|
...
|
||||||
py::object main_namespace = main_module.attr("__dict__");
|
|
||||||
|
|
||||||
//now execute code
|
// Evaluate in scope of main module
|
||||||
py::exec(
|
py::object scope = py::module::import("__main__").attr("__dict__");
|
||||||
"print('Hello World1!')\n"
|
|
||||||
"print('Other Data');",
|
|
||||||
main_namespace);
|
|
||||||
|
|
||||||
//execute a single statement
|
// Evaluate an isolated expression
|
||||||
py::exec_statement("x=42", main_namespace);
|
int result = py::eval("my_variable + 10", scope).cast<int>();
|
||||||
|
|
||||||
//ok, now I want to get the result of a statement, we'll use x in this example
|
// Evaluate a sequence of statements
|
||||||
py::object res = py::eval("x");
|
py::eval<py::eval_statements>(
|
||||||
std:cout << "Yielded: " << res.cast<int>() << std::endl;
|
"print('Hello')\n"
|
||||||
|
"print('world!');",
|
||||||
|
scope);
|
||||||
|
|
||||||
//or we can execute a file within the same content
|
// Evaluate the statements in an separate Python file on disk
|
||||||
py::exec_file("my_script.py", main_namespace);
|
py::eval_file("script.py", scope);
|
||||||
|
|
||||||
|
@ -245,25 +245,3 @@ Passing extra arguments to the def function
|
|||||||
|
|
||||||
Used to specify the function name
|
Used to specify the function name
|
||||||
|
|
||||||
Calling Python from C++
|
|
||||||
=======================
|
|
||||||
|
|
||||||
.. function:: eval(str string, object global = object(), object local = object())
|
|
||||||
|
|
||||||
Evaluate a statement, i.e. one that does not yield None.
|
|
||||||
The return value the result of the expression. It throws pybind11::error_already_set if the commands are invalid.
|
|
||||||
|
|
||||||
.. function:: exec(str string, object global = object(), object local = object())
|
|
||||||
|
|
||||||
Execute a set of statements. The return value the result of the code. It throws pybind11::error_already_set if the commands are invalid.
|
|
||||||
|
|
||||||
.. function:: exec_statement(str string, object global = object(), object local = object())
|
|
||||||
|
|
||||||
Execute a single statement. The return value the result of the code. It throws pybind11::error_already_set if the commands are invalid.
|
|
||||||
|
|
||||||
.. function:: exec_file(str filename, object global = object(), object local = object())
|
|
||||||
|
|
||||||
Execute a file. The function exec_file will throw std::invalid_argument if the file cannot be opened.
|
|
||||||
The return value the result of the code. It throws pybind11::error_already_set if the commands are invalid and
|
|
||||||
std::invalid_argument if the file cannot be opened.
|
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
example/example18.cpp -- Usage of exec, eval etc.
|
example/example18.cpp -- Usage of eval() and eval_file()
|
||||||
|
|
||||||
Copyright (c) 2016 Klemens D. Morgenstern
|
Copyright (c) 2016 Klemens D. Morgenstern
|
||||||
|
|
||||||
@ -8,29 +8,30 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <pybind11/exec.h>
|
#include <pybind11/eval.h>
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
|
|
||||||
void example18() {
|
void example18() {
|
||||||
py::module main_module = py::module::import("__main__");
|
py::module main_module = py::module::import("__main__");
|
||||||
py::object main_namespace = main_module.attr("__dict__");
|
py::object main_namespace = main_module.attr("__dict__");
|
||||||
|
|
||||||
bool executed = false;
|
bool ok = false;
|
||||||
|
|
||||||
main_module.def("call_test", [&]()-> int {executed = true; return 42;});
|
main_module.def("call_test", [&]() -> int {
|
||||||
|
ok = true;
|
||||||
|
return 42;
|
||||||
|
});
|
||||||
|
|
||||||
cout << "exec test" << endl;
|
cout << "eval_statements test" << endl;
|
||||||
|
|
||||||
py::exec(
|
auto result = py::eval<py::eval_statements>(
|
||||||
"print('Hello World!');\n"
|
"print('Hello World!');\n"
|
||||||
"x = call_test();",
|
"x = call_test();", main_namespace);
|
||||||
main_namespace);
|
|
||||||
|
|
||||||
if (executed)
|
if (ok && result == py::none())
|
||||||
cout << "exec passed" << endl;
|
cout << "eval_statements passed" << endl;
|
||||||
else {
|
else
|
||||||
cout << "exec failed" << endl;
|
cout << "eval_statements failed" << endl;
|
||||||
}
|
|
||||||
|
|
||||||
cout << "eval test" << endl;
|
cout << "eval test" << endl;
|
||||||
|
|
||||||
@ -38,83 +39,64 @@ void example18() {
|
|||||||
|
|
||||||
if (val.cast<int>() == 42)
|
if (val.cast<int>() == 42)
|
||||||
cout << "eval passed" << endl;
|
cout << "eval passed" << endl;
|
||||||
else {
|
else
|
||||||
cout << "eval failed" << endl;
|
cout << "eval failed" << endl;
|
||||||
}
|
|
||||||
|
|
||||||
|
ok = false;
|
||||||
|
cout << "eval_single_statement test" << endl;
|
||||||
|
|
||||||
executed = false;
|
py::eval<py::eval_single_statement>(
|
||||||
cout << "exec_statement test" << endl;
|
"y = call_test();", main_namespace);
|
||||||
|
|
||||||
py::exec_statement("y = call_test();", main_namespace);
|
if (ok)
|
||||||
|
cout << "eval_single_statement passed" << endl;
|
||||||
|
else
|
||||||
|
cout << "eval_single_statement failed" << endl;
|
||||||
|
|
||||||
|
cout << "eval_file test" << endl;
|
||||||
if (executed)
|
|
||||||
cout << "exec_statement passed" << endl;
|
|
||||||
else {
|
|
||||||
cout << "exec_statement failed" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "exec_file test" << endl;
|
|
||||||
|
|
||||||
int val_out;
|
int val_out;
|
||||||
main_module.def("call_test2", [&](int value) {val_out = value;});
|
main_module.def("call_test2", [&](int value) {val_out = value;});
|
||||||
|
|
||||||
|
|
||||||
py::exec_file("example18_call.py", main_namespace);
|
|
||||||
|
|
||||||
if (val_out == 42)
|
|
||||||
cout << "exec_file passed" << endl;
|
|
||||||
else {
|
|
||||||
cout << "exec_file failed" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
executed = false;
|
|
||||||
cout << "exec failure test" << endl;
|
|
||||||
try {
|
try {
|
||||||
py::exec("non-sense code ...");
|
result = py::eval_file("example18_call.py", main_namespace);
|
||||||
}
|
} catch (...) {
|
||||||
catch (py::error_already_set & err) {
|
result = py::eval_file("example/example18_call.py", main_namespace);
|
||||||
executed = true;
|
|
||||||
}
|
|
||||||
if (executed)
|
|
||||||
cout << "exec failure test passed" << endl;
|
|
||||||
else {
|
|
||||||
cout << "exec failure test failed" << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (val_out == 42 && result == py::none())
|
||||||
|
cout << "eval_file passed" << endl;
|
||||||
|
else
|
||||||
|
cout << "eval_file failed" << endl;
|
||||||
|
|
||||||
executed = false;
|
ok = false;
|
||||||
cout << "exec_file failure test" << endl;
|
|
||||||
try {
|
|
||||||
py::exec_file("none-existing file");
|
|
||||||
}
|
|
||||||
catch (std::invalid_argument & err) {
|
|
||||||
executed = true;
|
|
||||||
}
|
|
||||||
if (executed)
|
|
||||||
cout << "exec_file failure test passed" << endl;
|
|
||||||
else {
|
|
||||||
cout << "exec_file failure test failed" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
executed = false;
|
|
||||||
cout << "eval failure test" << endl;
|
cout << "eval failure test" << endl;
|
||||||
try {
|
try {
|
||||||
py::eval("print('dummy')");
|
py::eval("nonsense code ...");
|
||||||
|
} catch (py::error_already_set &) {
|
||||||
|
PyErr_Clear();
|
||||||
|
ok = true;
|
||||||
}
|
}
|
||||||
catch (py::error_already_set & err) {
|
|
||||||
executed = true;
|
if (ok)
|
||||||
}
|
|
||||||
if (executed)
|
|
||||||
cout << "eval failure test passed" << endl;
|
cout << "eval failure test passed" << endl;
|
||||||
else {
|
else
|
||||||
cout << "eval failure test failed" << endl;
|
cout << "eval failure test failed" << endl;
|
||||||
|
|
||||||
|
ok = false;
|
||||||
|
cout << "eval_file failure test" << endl;
|
||||||
|
try {
|
||||||
|
py::eval_file("nonexisting file");
|
||||||
|
} catch (std::exception &) {
|
||||||
|
ok = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ok)
|
||||||
|
cout << "eval_file failure test passed" << endl;
|
||||||
|
else
|
||||||
|
cout << "eval_file failure test failed" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_ex18(py::module & m) {
|
void init_ex18(py::module & m) {
|
||||||
m.def("example18", &example18);
|
m.def("example18", &example18);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
exec test
|
eval_statements test
|
||||||
Hello World!
|
eval_statements passed
|
||||||
exec passed
|
|
||||||
eval test
|
eval test
|
||||||
eval passed
|
eval passed
|
||||||
exec_statement test
|
eval_single_statement test
|
||||||
exec_statement passed
|
eval_single_statement passed
|
||||||
exec_file test
|
eval_file test
|
||||||
exec_file passed
|
eval_file passed
|
||||||
exec failure test
|
|
||||||
exec failure test passed
|
|
||||||
exec_file failure test
|
|
||||||
exec_file failure test passed
|
|
||||||
eval failure test
|
eval failure test
|
||||||
eval failure test passed
|
eval failure test passed
|
||||||
|
eval_file failure test
|
||||||
|
eval_file failure test passed
|
||||||
|
Hello World!
|
||||||
|
88
include/pybind11/eval.h
Normal file
88
include/pybind11/eval.h
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
pybind11/exec.h: Support for evaluating Python expressions and statements
|
||||||
|
from strings and files
|
||||||
|
|
||||||
|
Copyright (c) 2016 Klemens Morgenstern <klemens.morgenstern@ed-chemnitz.de> and
|
||||||
|
Wenzel Jakob <wenzel.jakob@epfl.ch>
|
||||||
|
|
||||||
|
All rights reserved. Use of this source code is governed by a
|
||||||
|
BSD-style license that can be found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "pytypes.h"
|
||||||
|
|
||||||
|
NAMESPACE_BEGIN(pybind11)
|
||||||
|
|
||||||
|
enum eval_mode {
|
||||||
|
/// Evaluate a string containing an isolated expression
|
||||||
|
eval_expr,
|
||||||
|
|
||||||
|
/// Evaluate a string containing a single statement. Returns \c none
|
||||||
|
eval_single_statement,
|
||||||
|
|
||||||
|
/// Evaluate a string containing a sequence of statement. Returns \c none
|
||||||
|
eval_statements
|
||||||
|
};
|
||||||
|
|
||||||
|
template <eval_mode mode = eval_expr>
|
||||||
|
object eval(const std::string& str, object global = object(), object local = object()) {
|
||||||
|
if (!global) {
|
||||||
|
global = object(PyEval_GetGlobals(), true);
|
||||||
|
if (!global)
|
||||||
|
global = dict();
|
||||||
|
}
|
||||||
|
if (!local)
|
||||||
|
local = global;
|
||||||
|
|
||||||
|
int start;
|
||||||
|
switch (mode) {
|
||||||
|
case eval_expr: start = Py_eval_input; break;
|
||||||
|
case eval_single_statement: start = Py_single_input; break;
|
||||||
|
case eval_statements: start = Py_file_input; break;
|
||||||
|
default: pybind11_fail("invalid evaluation mode");
|
||||||
|
}
|
||||||
|
|
||||||
|
object result(PyRun_String(str.c_str(), start, global.ptr(), local.ptr()), false);
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
throw error_already_set();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <eval_mode mode = eval_statements>
|
||||||
|
object eval_file(const std::string& fname, object global = object(), object local = object()) {
|
||||||
|
if (!global) {
|
||||||
|
global = object(PyEval_GetGlobals(), true);
|
||||||
|
if (!global)
|
||||||
|
global = dict();
|
||||||
|
}
|
||||||
|
if (!local)
|
||||||
|
local = global;
|
||||||
|
|
||||||
|
int start;
|
||||||
|
switch (mode) {
|
||||||
|
case eval_expr: start = Py_eval_input; break;
|
||||||
|
case eval_single_statement: start = Py_single_input; break;
|
||||||
|
case eval_statements: start = Py_file_input; break;
|
||||||
|
default: pybind11_fail("invalid evaluation mode");
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *f = fopen(fname.c_str(), "r");
|
||||||
|
if (!f)
|
||||||
|
pybind11_fail("File \"" + fname + "\" could not be opened!");
|
||||||
|
|
||||||
|
object result(PyRun_FileEx(f, fname.c_str(), Py_file_input, global.ptr(),
|
||||||
|
local.ptr(), 1),
|
||||||
|
false);
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
throw error_already_set();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
NAMESPACE_END(pybind11)
|
@ -1,156 +0,0 @@
|
|||||||
/*
|
|
||||||
pybind11/exec.h: Functions to execute python from C++. Based on code from boost.python.
|
|
||||||
|
|
||||||
Copyright (c) 2005 Stefan Seefeld
|
|
||||||
|
|
||||||
This code is based on the boost.python implementation, so a different license applies to this file.
|
|
||||||
|
|
||||||
Boost Software License - Version 1.0 - August 17th, 2003
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person or organization
|
|
||||||
obtaining a copy of the software and accompanying documentation covered by
|
|
||||||
this license (the "Software") to use, reproduce, display, distribute,
|
|
||||||
execute, and transmit the Software, and to prepare derivative works of the
|
|
||||||
Software, and to permit third-parties to whom the Software is furnished to
|
|
||||||
do so, all subject to the following:
|
|
||||||
|
|
||||||
The copyright notices in the Software and this entire statement, including
|
|
||||||
the above license grant, this restriction and the following disclaimer,
|
|
||||||
must be included in all copies of the Software, in whole or in part, and
|
|
||||||
all derivative works of the Software, unless such copies or derivative
|
|
||||||
works are solely in the form of machine-executable object code generated by
|
|
||||||
a source language processor.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
||||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
||||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "pytypes.h"
|
|
||||||
|
|
||||||
NAMESPACE_BEGIN(pybind11)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline object eval (const std::string& st, object global = object(), object local = object()) {
|
|
||||||
if (!global) {
|
|
||||||
if (PyObject *g = PyEval_GetGlobals())
|
|
||||||
global = object(g, true);
|
|
||||||
else
|
|
||||||
global = dict();
|
|
||||||
}
|
|
||||||
if (!local)
|
|
||||||
local = global;
|
|
||||||
|
|
||||||
PyObject *res = PyRun_String(st.c_str() , Py_eval_input, global.ptr(), local.ptr());
|
|
||||||
|
|
||||||
if (res == nullptr)
|
|
||||||
throw error_already_set();
|
|
||||||
|
|
||||||
return {res, false};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline object exec (const std::string& st, object global = object(), object local = object()) {
|
|
||||||
if (!global) {
|
|
||||||
if (PyObject *g = PyEval_GetGlobals())
|
|
||||||
global = object(g, true);
|
|
||||||
else
|
|
||||||
global = dict();
|
|
||||||
}
|
|
||||||
if (!local)
|
|
||||||
local = global;
|
|
||||||
|
|
||||||
PyObject *res = PyRun_String(st.c_str() , Py_file_input, global.ptr(), local.ptr());
|
|
||||||
|
|
||||||
if (res == nullptr)
|
|
||||||
throw error_already_set();
|
|
||||||
|
|
||||||
return {res, false};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline object exec_statement (const std::string& st, object global = object(), object local = object()) {
|
|
||||||
if (!global) {
|
|
||||||
if (PyObject *g = PyEval_GetGlobals())
|
|
||||||
global = object(g, true);
|
|
||||||
else
|
|
||||||
global = dict();
|
|
||||||
}
|
|
||||||
if (!local)
|
|
||||||
local = global;
|
|
||||||
|
|
||||||
PyObject *res = PyRun_String(st.c_str() , Py_single_input, global.ptr(), local.ptr());
|
|
||||||
if (res == nullptr)
|
|
||||||
throw error_already_set();
|
|
||||||
|
|
||||||
return {res, false};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline object exec_file(const std::string& filename, object global = object(), object local = object()) {
|
|
||||||
// Set suitable default values for global and local dicts.
|
|
||||||
if (!global) {
|
|
||||||
if (PyObject *g = PyEval_GetGlobals())
|
|
||||||
global = object(g, true);
|
|
||||||
else
|
|
||||||
global = dict();
|
|
||||||
}
|
|
||||||
if (!local) local = global;
|
|
||||||
|
|
||||||
std::string f = filename; //need to copy for the signature of PyFile_FromString
|
|
||||||
|
|
||||||
|
|
||||||
// Let python open the file to avoid potential binary incompatibilities.
|
|
||||||
#if PY_VERSION_HEX >= 0x03040000
|
|
||||||
const static int close_it = 1;
|
|
||||||
FILE *fs = _Py_fopen(f.c_str(), "r");
|
|
||||||
#elif PY_VERSION_HEX >= 0x03000000
|
|
||||||
const static int close_it = 1;
|
|
||||||
PyObject *fo = Py_BuildValue("s", f.c_str());
|
|
||||||
FILE *fs = _Py_fopen(fo, "r");
|
|
||||||
Py_DECREF(fo);
|
|
||||||
#else
|
|
||||||
const static int close_it = 0;
|
|
||||||
PyObject *pyfile = PyFile_FromString(&f.front(), const_cast<char*>("r"));
|
|
||||||
if (!pyfile)
|
|
||||||
throw std::invalid_argument(std::string(f) + " : no such file");
|
|
||||||
object file(pyfile, false);
|
|
||||||
FILE *fs = PyFile_AsFile(file.ptr());
|
|
||||||
#endif
|
|
||||||
if (fs == nullptr)
|
|
||||||
throw std::invalid_argument(std::string(f) + " : could not be opened");
|
|
||||||
|
|
||||||
PyObject* res = PyRun_FileEx(fs,
|
|
||||||
f.c_str(),
|
|
||||||
Py_file_input,
|
|
||||||
global.ptr(), local.ptr(),
|
|
||||||
close_it);
|
|
||||||
|
|
||||||
if (res == nullptr)
|
|
||||||
throw error_already_set();
|
|
||||||
|
|
||||||
return {res, false};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
inline object exec (str string, object global = object(), object local = object()) {
|
|
||||||
return exec(static_cast<std::string>(string), global, local);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline object eval (str string, object global = object(), object local = object()) {
|
|
||||||
return eval(static_cast<std::string>(string), global, local);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline object exec_file(str filename, object global = object(), object local = object()) {
|
|
||||||
return exec_file(static_cast<std::string>(filename), global, local);
|
|
||||||
}
|
|
||||||
inline object exec_statement (str string, object global = object(), object local = object()) {
|
|
||||||
return exec_statement(static_cast<std::string>(string), global, local);
|
|
||||||
}
|
|
||||||
|
|
||||||
NAMESPACE_END(pybind11)
|
|
Loading…
Reference in New Issue
Block a user