Skip to content

test_eval: Show example of working closure - #2743

Merged
Skylion007 merged 3 commits into
pybind:masterfrom
EricCousineau-TRI:feature-eval-closure
Aug 6, 2021
Merged

test_eval: Show example of working closure#2743
Skylion007 merged 3 commits into
pybind:masterfrom
EricCousineau-TRI:feature-eval-closure

Conversation

@EricCousineau-TRI

Copy link
Copy Markdown
Collaborator

Description

Shows an example unittest with using py::exec with a closure.
I had forgotten what was necessary, then came across #1742 again. I'm adding an example here just to show it.

@YannickJadoulYannickJadoul left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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!

Comment threadtests/test_eval.cpp Outdated

def func():
return closure_value
)", local, local);

Copy link
Copy Markdown
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?

@YannickJadoul

YannickJadoul commented Dec 25, 2020

Copy link
Copy Markdown
Collaborator

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!

Comment threadtests/test_eval.cpp Outdated
return global;
});

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

Copy link
Copy Markdown
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.

@YannickJadoul

Copy link
Copy Markdown
Collaborator

expand this test

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

Copy link
Copy Markdown
CollaboratorAuthor

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.

Ooh, yeah, that'd be great! Though understandable if it's gotten lost in your stash after a month 😅
Just lemme know!

@YannickJadoul

Copy link
Copy Markdown
Collaborator

Luckily I hardly ever clear out that thing, so it was still somewhere down ;-)
(Also lucky you don't need to pop the stash like a true stack :-P )

@Skylion007Skylion007 added docs Docs or GitHub info needs-review labels Aug 6, 2021
@Skylion007Skylion007 added this to the v2.8 milestone Aug 6, 2021
@Skylion007
Skylion007 requested review from henryiii and rwgkAugust 6, 2021 19:29
rwgk
rwgk approved these changes Aug 6, 2021

@rwgkrwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just documenting that I looked, too :-)

@Skylion007
Skylion007 merged commit 6ac8efe into pybind:masterAug 6, 2021
@github-actionsgithub-actionsBot added the needs changelog Possibly needs a changelog entry label Aug 6, 2021
@henryiiihenryiii removed needs changelog Possibly needs a changelog entry needs-review labels Sep 16, 2021
@rwgkrwgk mentioned this pull request Feb 10, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocs or GitHub info

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@EricCousineau-TRI@YannickJadoul@rwgk@Skylion007@henryiii