test_eval: Show example of working closure (#2743)

* test_eval: Show example of working closure

* Extend test_eval_closure with weirder examples of closures for py::eval

Co-authored-by: Yannick Jadoul <yannick.jadoul@belgacom.net>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
This commit is contained in:
Eric Cousineau
2021-08-06 15:51:53 -04:00
committed by GitHub
parent af7007331a
commit 6ac8efe52d
2 changed files with 35 additions and 0 deletions

View File

@@ -98,4 +98,22 @@ TEST_SUBMODULE(eval_, m) {
auto int_class = py::eval("isinstance(42, int)", global);
return global;
});
// test_eval_closure
m.def("test_eval_closure", []() {
py::dict global;
global["closure_value"] = 42;
py::dict local;
local["closure_value"] = 0;
py::exec(R"(
local_value = closure_value
def func_global():
return closure_value
def func_local():
return local_value
)", global, local);
return std::make_pair(global, local);
});
}