17 auto global = py::dict(py::module_::import(
"__main__").attr(
"__dict__"));
19 m.def(
"test_eval_statements", [global]() {
20 auto local = py::dict();
21 local[
"call_test"] = py::cpp_function([&]() ->
int {
27 "message = 'Hello World!'\n"
40 auto x = local[
"x"].cast<
int>();
45 m.def(
"test_eval", [global]() {
46 auto local = py::dict();
47 local[
"x"] = py::int_(42);
49 return x.cast<
int>() == 42;
52 m.def(
"test_eval_single_statement", []() {
53 auto local = py::dict();
54 local[
"call_test"] = py::cpp_function([&]() ->
int {
58 auto result = py::eval<py::eval_single_statement>(
"x = call_test()", py::dict(), local);
59 auto x = local[
"x"].cast<
int>();
60 return result.is_none() &&
x == 42;
63 m.def(
"test_eval_file", [global](py::str filename) {
64 auto local = py::dict();
65 local[
"y"] = py::int_(43);
68 local[
"call_test2"] = py::cpp_function([&](
int value) { val_out =
value; });
71 return val_out == 43 && result.is_none();
74 m.def(
"test_eval_failure", []() {
77 }
catch (py::error_already_set &) {
83 m.def(
"test_eval_file_failure", []() {
86 }
catch (std::exception &) {
93 m.def(
"eval_empty_globals", [](py::object global) {
96 auto int_class =
py::eval(
"isinstance(42, int)", global);