Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added closure related test cases #873

Merged
merged 3 commits into from Aug 11, 2018

Conversation

Projects
None yet
2 participants
@BPYap
Copy link
Contributor

commented Jul 22, 2018

While all but one test cases failed because voc currently doesn't support closure in generator, class and method context yet, I want to highlight 1 particular test case which isn't addressed in voc's current closure creation logic. As pointed out by @freakboy3742 , consider the following:

def func():
    closure_var = 'before nested is defined'
    def nested():
        print(closure_var)
    closure_var = 'after nested is defined'
    nested() 
func()

CPython3 will print after nested is defined in console while voc prints before nested is defined.
This is because voc takes the value of closure variable at the moment the closure function is defined and that value is stored in closure_vars field of that closure. Thus any rebinding of closure variables in enclosing scope after closure definition is not known to the closure.

As @freakboy3742 mentioned, current implementation of closure in voc may require some modifications /rework to allow closure to acquire the 'latest' value of closure variable.
Perhaps we could make use of code.co_cellvars in enclosing scope's context and store free variables somewhere else instead of local register of enclosing context so both enclosing scope and closure scope can be directed to that storage space when they want to access free variables. Plus it would also make nonlocal implementation easier because closure can directly modify nonlocal value in that same storage space.

@freakboy3742
Copy link
Member

left a comment

👍

@freakboy3742 freakboy3742 merged commit 97878b7 into beeware:master Aug 11, 2018

5 checks passed

beekeeper:0/beefore:javacheckstyle Java lint checks passed.
Details
beekeeper:0/beefore:pycodestyle Python lint checks passed.
Details
beekeeper:1/smoke-test Smoke build (Python 3.4) passed.
Details
beekeeper:2/full-test:py3.5 Python 3.5 tests passed.
Details
beekeeper:2/full-test:py3.6 Python 3.6 tests passed.
Details

@BPYap BPYap deleted the BPYap:closure-test-case branch Aug 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.