Uh oh!
There was an error while loading. Please reload this page.
test_eval: Show example of working closure - #2743
Conversation
YannickJadoul
left a comment
There was a problem hiding this comment.
Ah, cool! I knew things were annoying and complicated, but not this annoying and complicated. Good to have a test on this to remind ourselves!
| def func(): | ||
| return closure_value | ||
| )", local, local); |
There was a problem hiding this comment.
Why is local being passed twice here? And from reading #1742, shouldn't this be called global as it's the globals dict?
Suggestion to expand this test, to show differences between what's captured and what's not captured with locals vs. globals: // 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);
returnstd::make_pair(global, local);
});and deftest_eval_closure():
global_, local=m.test_eval_closure()
assertglobal_["closure_value"] ==42assertlocal["closure_value"] ==0assert"local_value"notinglobal_assertlocal["local_value"] ==0assert"func_global"notinglobal_assertlocal["func_global"]() ==42assert"func_local"notinglobal_withpytest.raises(NameError):
local["func_local"]()Maybe I'm misunderstanding/butchering the original aim of this test & PR, though? Please do tell me, then, if this extension is irrelevant or obfuscating to the closure construction you're trying to test, @EricCousineau-TRI! |
| return global; | ||
| }); | ||
| m.def("test_eval_closure", [global, copy]() { |
There was a problem hiding this comment.
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.
YannickJadoul
commented
Dec 25, 2020
If you like the idea, I have this on my git's stash, btw, so I can just commit and push, if that saves trouble. |
EricCousineau-TRI
commented
Jan 22, 2021
Ooh, yeah, that'd be great! Though understandable if it's gotten lost in your stash after a month 😅 |
YannickJadoul
commented
Jan 25, 2021
Luckily I hardly ever clear out that thing, so it was still somewhere down ;-) |
rwgk
left a comment
There was a problem hiding this comment.
Just documenting that I looked, too :-)
Description
Shows an example unittest with using
py::execwith a closure.I had forgotten what was necessary, then came across #1742 again. I'm adding an example here just to show it.