mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
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:
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user