Skip to content
This repository has been archived by the owner. It is now read-only.

Fixed #866 RunTimeError when generator is nested in more than 1 level of function definition #867

Merged
merged 3 commits into from Jul 14, 2018

Conversation

@BPYap
Copy link
Contributor

@BPYap BPYap commented Jul 12, 2018

On closer inspection, the reason for the RuntimeError in #866 was due to incorrect class path name.

Considering the code below (Test.py):

def wrapped():
    def func():
        def gen():
            yield 'Hello World'

        print(next(gen()))

    func()
    
wrapped()

The path to gen would be python.Test.func$gen, as indicated in compilation output:

Compiling Test.py ...
Writing .\python\Test.class ...
Writing .\python\Test\func.class ...
Writing .\python\Test\func$gen.class ...

However, during runtime, the path to gen becomes python.Test.func.gen instead of python.Test.func$gen, causing java.lang.ClassNotFoundException which is wrapped in RuntimeError.

The root cause is on line 1118 of methods.py, when declaring classpath of generator method, self.klass.class_name returns python.Test.func.gen. To fix this, I replaced it with self.class_descriptor.replace('/', '.') to get python.Test.func$gen

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Patch makes sense, but needs a test

@BPYap
Copy link
Contributor Author

@BPYap BPYap commented Jul 13, 2018

@freakboy3742 Done 😄

@freakboy3742 freakboy3742 merged commit 7e9cdf2 into beeware:master Jul 14, 2018
5 checks passed
@BPYap BPYap deleted the fix-nested-generator branch Jul 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants