Repository navigation
Restore the scope when a class body raises - #906
Open
HoneyBearTech wants to merge 1 commit into
Open
HoneyBearTech wants to merge 1 commit into
HoneyBearTech wants to merge 1 commit into
Conversation
ast_classdef switches into the class namespace before running the body and only switched back after the body finished normally. When the body raised, pyscript stayed in the class namespace, so after the exception was caught, later statements bound their names in the failed class instead of the script. Inside a function the scope stack was left one entry too long. Run the body in a try/finally so the outer scope is always restored, including for the SyntaxError raised for return or break in a class body. Add tests compared against CPython. Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was broken
When a class body raises, pyscript stayed in the class's namespace. After the exception was caught, later statements bound their names in the failed class instead of the script:
Inside a function, the scope stack was left one entry too long.
Why
ast_classdefpushes the current symbol table and switches into the class namespace before running the body, but it only switched back after the body finished normally.What I changed
The class body now runs in a
try/finally, so the outer scope is always restored, including for theSyntaxErrorraised forreturnorbreakin a class body.Testing
tests/test_unit_eval.py, compared against CPython:Note
#902's second commit also changes
ast_classdef. Whichever merges second needs a small rebase; I'm happy to do it.