added exec functions

This commit is contained in:
Klemens Morgenstern
2016-06-09 16:10:26 +02:00
committed by Wenzel Jakob
parent c2ee3f52b5
commit c6ad2c4993
9 changed files with 354 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ set(PYBIND11_EXAMPLES
example15.cpp
example16.cpp
example17.cpp
example18.cpp
issues.cpp
)
@@ -66,3 +67,4 @@ foreach(VALUE ${PYBIND11_EXAMPLES})
string(REGEX REPLACE "^(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}")
add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME})
endforeach()

View File

@@ -26,6 +26,7 @@ void init_ex14(py::module &);
void init_ex15(py::module &);
void init_ex16(py::module &);
void init_ex17(py::module &);
void init_ex18(py::module &);
void init_issues(py::module &);
#if defined(PYBIND11_TEST_EIGEN)
@@ -52,6 +53,7 @@ PYBIND11_PLUGIN(example) {
init_ex15(m);
init_ex16(m);
init_ex17(m);
init_ex18(m);
init_issues(m);
#if defined(PYBIND11_TEST_EIGEN)

120
example/example18.cpp Normal file
View File

@@ -0,0 +1,120 @@
/*
example/example18.cpp -- Usage of exec, eval etc.
Copyright (c) 2016 Klemens D. Morgenstern
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#include <pybind11/exec.h>
#include "example.h"
void example18() {
py::module main_module = py::module::import("__main__");
py::object main_namespace = main_module.attr("__dict__");
bool executed = false;
main_module.def("call_test", [&]()-> int {executed = true; return 42;});
cout << "exec test" << endl;
py::exec(
"print('Hello World!');\n"
"x = call_test();",
main_namespace);
if (executed)
cout << "exec passed" << endl;
else {
cout << "exec failed" << endl;
}
cout << "eval test" << endl;
py::object val = py::eval("x", main_namespace);
if (val.cast<int>() == 42)
cout << "eval passed" << endl;
else {
cout << "eval failed" << endl;
}
executed = false;
cout << "exec_statement test" << endl;
py::exec_statement("y = call_test();", main_namespace);
if (executed)
cout << "exec_statement passed" << endl;
else {
cout << "exec_statement failed" << endl;
}
cout << "exec_file test" << endl;
int val_out;
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 {
py::exec("non-sense code ...");
}
catch (py::error_already_set & err) {
executed = true;
}
if (executed)
cout << "exec failure test passed" << endl;
else {
cout << "exec failure test failed" << endl;
}
executed = 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;
try {
py::eval("print('dummy')");
}
catch (py::error_already_set & err) {
executed = true;
}
if (executed)
cout << "eval failure test passed" << endl;
else {
cout << "eval failure test failed" << endl;
}
}
void init_ex18(py::module & m) {
m.def("example18", &example18);
}

5
example/example18.py Normal file
View File

@@ -0,0 +1,5 @@
from example import example18
example18()

15
example/example18.ref Normal file
View File

@@ -0,0 +1,15 @@
exec test
Hello World!
exec passed
eval test
eval passed
exec_statement test
exec_statement passed
exec_file test
exec_file passed
exec failure test
exec failure test passed
exec_file failure test
exec_file failure test passed
eval failure test
eval failure test passed

View File

@@ -0,0 +1 @@
call_test2(y)