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

Nonlocal statement support #854

merged 32 commits into from Aug 7, 2018


None yet
2 participants
Copy link

commented Jun 29, 2018

Nonlocal is used in some built-in module (e.g. in asyncio module). Support for nonlocal statement should enable a number of files in KNOWN_PROBLEM_MODULES to compile.

Refer to GSOC week 10/11 log for implementation details.

New features added in this PR:

  • nonlocal is now supported in function closure and generator closure. e.g:
def func():
    a = 'a from outer'
    def nested_func():
        nonlocal a
        print(a)  # will print 'a from outer'
        a = 'a from inner'
        print(a)  # will print 'a from inner'

    print(a)  # will print 'a from inner'

  • Generator is now able to read variables from enclosing scopes. e.g:
def func():
    a = 'a from outer'
    def gen():
        print(a)  # will print 'a from outer'



BPYap added some commits Jul 2, 2018

@BPYap BPYap changed the title Nonlocal statement support [WIP] Nonlocal statement support Jul 4, 2018

BPYap added some commits Jul 4, 2018

Revert "Updated methods"
This reverts commit 6860a1d.

This comment has been minimized.

Copy link
Contributor Author

commented Aug 2, 2018

@freakboy3742, passing #locals as storage space for closure variables seems to work well. To my surprise it even fixes the test_recursive_in_function case 😄

Well, some part of the code may still be further enhanced to be more efficient.
For now, only function and generator support closure/nonlocal. I'm thinking to add closure support for class and method at later time and in separate PR as they are not as important in getting asyncio to work.

Copy link

left a comment

👍 It took a while, but we finally got there! This looks a lot cleaner - excellent work!

@freakboy3742 freakboy3742 merged commit 0028447 into beeware:master Aug 7, 2018

5 checks passed

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

@BPYap BPYap deleted the BPYap:nonlocal-statement-support branch Aug 8, 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.