pybind11/tests/test_eval.cpp

103 lines
2.5 KiB
C++
Raw Normal View History

2016-06-09 14:10:26 +00:00
/*
tests/test_eval.cpp -- Usage of eval() and eval_file()
2016-06-09 14:10:26 +00:00
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.
*/
2016-07-08 08:52:10 +00:00
#include <pybind11/eval.h>
#include "pybind11_tests.h"
2016-06-09 14:10:26 +00:00
void example_eval() {
2016-06-09 14:10:26 +00:00
py::module main_module = py::module::import("__main__");
py::object main_namespace = main_module.attr("__dict__");
2016-07-08 08:52:10 +00:00
bool ok = false;
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
main_module.def("call_test", [&]() -> int {
ok = true;
return 42;
});
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
cout << "eval_statements test" << endl;
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
auto result = py::eval<py::eval_statements>(
2016-06-09 14:10:26 +00:00
"print('Hello World!');\n"
2016-07-08 08:52:10 +00:00
"x = call_test();", main_namespace);
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
if (ok && result == py::none())
cout << "eval_statements passed" << endl;
else
cout << "eval_statements failed" << endl;
2016-06-09 14:10:26 +00:00
cout << "eval test" << endl;
py::object val = py::eval("x", main_namespace);
if (val.cast<int>() == 42)
cout << "eval passed" << endl;
2016-07-08 08:52:10 +00:00
else
2016-06-09 14:10:26 +00:00
cout << "eval failed" << endl;
2016-07-08 08:52:10 +00:00
ok = false;
cout << "eval_single_statement test" << endl;
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
py::eval<py::eval_single_statement>(
"y = call_test();", main_namespace);
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
if (ok)
cout << "eval_single_statement passed" << endl;
else
cout << "eval_single_statement failed" << endl;
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
cout << "eval_file test" << endl;
2016-06-09 14:10:26 +00:00
int val_out;
main_module.def("call_test2", [&](int value) {val_out = value;});
try {
result = py::eval_file("test_eval_call.py", main_namespace);
2016-07-08 08:52:10 +00:00
} catch (...) {
result = py::eval_file("tests/test_eval_call.py", main_namespace);
2016-06-09 14:10:26 +00:00
}
2016-07-08 08:52:10 +00:00
if (val_out == 42 && result == py::none())
cout << "eval_file passed" << endl;
else
cout << "eval_file failed" << endl;
2016-06-09 14:10:26 +00:00
2016-07-08 08:52:10 +00:00
ok = false;
2016-06-09 14:10:26 +00:00
cout << "eval failure test" << endl;
try {
2016-07-08 08:52:10 +00:00
py::eval("nonsense code ...");
} catch (py::error_already_set &) {
PyErr_Clear();
ok = true;
2016-06-09 14:10:26 +00:00
}
2016-07-08 08:52:10 +00:00
if (ok)
2016-06-09 14:10:26 +00:00
cout << "eval failure test passed" << endl;
2016-07-08 08:52:10 +00:00
else
2016-06-09 14:10:26 +00:00
cout << "eval failure test failed" << endl;
2016-07-08 08:52:10 +00:00
ok = false;
cout << "eval_file failure test" << endl;
try {
py::eval_file("nonexisting file");
} catch (std::exception &) {
ok = true;
2016-06-09 14:10:26 +00:00
}
2016-07-08 08:52:10 +00:00
if (ok)
cout << "eval_file failure test passed" << endl;
else
cout << "eval_file failure test failed" << endl;
2016-06-09 14:10:26 +00:00
}
void init_ex_eval(py::module & m) {
m.def("example_eval", &example_eval);
2016-06-09 14:10:26 +00:00
}