Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Nonlocal statement support #854

Merged
merged 32 commits into from
Aug 7, 2018

Conversation

BPYap
Copy link
Contributor

@BPYap BPYap commented Jun 29, 2018

Nonlocal is used in some built-in module (e.g. tasks.py 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'

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

func()
  • 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'
        yield

    next(gen())

func()

@BPYap BPYap changed the title Nonlocal statement support [WIP] Nonlocal statement support Jul 4, 2018
@BPYap
Copy link
Contributor Author

BPYap 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
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

👍 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
@BPYap BPYap deleted the nonlocal-statement-support branch August 8, 2018 00:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants