19 auto global = py::dict(py::module_::import(
"__main__").attr(
"__dict__"));
21 m.def(
"test_eval_statements", [global]() {
22 auto local = py::dict();
23 local[
"call_test"] = py::cpp_function([&]() ->
int {
return 42; });
26 py::exec(
"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 {
return 42; });
56 auto result = py::eval<py::eval_single_statement>(
"x = call_test()", py::dict(),
local);
57 auto x =
local[
"x"].cast<
int>();
58 return result.is_none() &&
x == 42;
62 auto local = py::dict();
63 local[
"y"] = py::int_(43);
66 local[
"call_test2"] = py::cpp_function([&](
int value) { val_out =
value; });
69 return val_out == 43 &&
result.is_none();
72 m.def(
"test_eval_failure", []() {
75 }
catch (py::error_already_set &) {
81 m.def(
"test_eval_file_failure", []() {
84 }
catch (std::exception &) {
91 m.def(
"eval_empty_globals", [](py::object global) {
92 if (global.is_none()) {
95 auto int_class =
py::eval(
"isinstance(42, int)", global);
100 m.def(
"test_eval_closure", []() {
102 global[
"closure_value"] = 42;
104 local[
"closure_value"] = 0;
106 local_value = closure_value
116 return std::make_pair(global,
local);