Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TEST_SUBMODULE(eval_, m) {
// test_evals

auto global = py::dict(py::module_::import("__main__").attr("__dict__"));
py::handle copy = py::module::import("copy").attr("copy");

m.def("test_eval_statements", [global]() {
auto local = py::dict();
Expand Down Expand Up @@ -96,4 +97,15 @@ TEST_SUBMODULE(eval_, m) {
auto int_class = py::eval("isinstance(42, int)", global);
return global;
});

m.def("test_eval_closure", [global, copy]() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add the // test_eval_closure comment above. In this case, it's a bit over the top since your function is already called that, but I like the existing convention of indicating the Python-side tests that C++-side bindings belong to.

py::dict local = copy(global);
py::exec(R"(
closure_value = 10

def func():
return closure_value
)", local, local);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is local being passed twice here? And from reading #1742, shouldn't this be called global as it's the globals dict?

return local;
});
}
5 changes: 5 additions & 0 deletions tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def test_eval_empty_globals():
g = {}
assert "__builtins__" in m.eval_empty_globals(g)
assert "__builtins__" in g


def test_eval_closure():
local = m.test_eval_closure()
assert local["func"]() == 10