-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[mypyc] fix: UnboundLocalError incorrectly raised as AttributeError #20085
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
Open
BobTheBuidler
wants to merge
16
commits into
python:master
Choose a base branch
from
BobTheBuidler:patch-20
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2a264d6
[mypyc] fix: UnboundLocalError incorrectly raised as AttributeError
BobTheBuidler 9bd4714
Update run-generators.test
BobTheBuidler 6fd8e50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 77031c3
cleanup
BobTheBuidler 64ca907
Update emitfunc.py
BobTheBuidler b1548b6
Update exc_ops.c
BobTheBuidler e559b4e
Update CPy.h
BobTheBuidler ff7629a
Update CPy.h
BobTheBuidler 7bc94c7
Update emitfunc.py
BobTheBuidler c685ece
Update CPy.h
BobTheBuidler 248dfde
Update exc_ops.c
BobTheBuidler 9c6e615
Merge branch 'master' into patch-20
BobTheBuidler 3430494
fix UnboundLocalError handling for env classes (#9)
BobTheBuidler 997a12f
Rebase temp/patch-20 on patch-20 (#23)
BobTheBuidler df8e857
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bd60398
Merge branch 'master' into patch-20
BobTheBuidler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -429,19 +429,21 @@ def visit_get_attr(self, op: GetAttr) -> None: | |
| ): | ||
| # Generate code for the following branch here to avoid | ||
| # redundant branches in the generated code. | ||
| self.emit_attribute_error(branch, cl.name, op.attr) | ||
| self.emit_attribute_error(branch, cl, op.attr) | ||
| self.emit_line("goto %s;" % self.label(branch.true)) | ||
| merged_branch = branch | ||
| self.emitter.emit_line("}") | ||
| if not merged_branch: | ||
| exc_class = "PyExc_AttributeError" | ||
| self.emitter.emit_line( | ||
| 'PyErr_SetString({}, "attribute {} of {} undefined");'.format( | ||
| exc_class, | ||
| repr(op.attr.removeprefix(GENERATOR_ATTRIBUTE_PREFIX)), | ||
| repr(cl.name), | ||
| ) | ||
| ) | ||
| var_name = op.attr.removeprefix(GENERATOR_ATTRIBUTE_PREFIX) | ||
| if cl.is_generated: | ||
| # A generated class does not "exist" to the user, this is just an unbound | ||
| # variable in their code, not a missing attribute on the generated class. | ||
| exc_class = "PyExc_UnboundLocalError" | ||
| exc_msg = f"local variable {var_name!r} referenced before assignment" | ||
| else: | ||
| exc_class = "PyExc_AttributeError" | ||
| exc_msg = f"attribute {var_name!r} of {cl.name!r} undefined" | ||
| self.emitter.emit_line(f'PyErr_SetString({exc_class}, "{exc_msg}");') | ||
|
|
||
| if attr_rtype.is_refcounted and not op.is_borrowed: | ||
| if not merged_branch and not always_defined: | ||
|
|
@@ -935,20 +937,32 @@ def emit_traceback(self, op: Branch) -> None: | |
| if op.traceback_entry is not None: | ||
| self.emitter.emit_traceback(self.source_path, self.module_name, op.traceback_entry) | ||
|
|
||
| def emit_attribute_error(self, op: Branch, class_name: str, attr: str) -> None: | ||
| def emit_attribute_error(self, op: Branch, class_ir: ClassIR, attr: str) -> None: | ||
| assert op.traceback_entry is not None | ||
| globals_static = self.emitter.static_name("globals", self.module_name) | ||
| self.emit_line( | ||
| 'CPy_AttributeError("%s", "%s", "%s", "%s", %d, %s);' | ||
| % ( | ||
| self.source_path.replace("\\", "\\\\"), | ||
| op.traceback_entry[0], | ||
| class_name, | ||
| attr.removeprefix(GENERATOR_ATTRIBUTE_PREFIX), | ||
| op.traceback_entry[1], | ||
| globals_static, | ||
| if class_ir.is_generated: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put this here so it will do its job in any place emit_attribute_error might be used in the future, it didn't seem appropriate to add another helper but this doesn't really seem appropriate either |
||
| self.emit_line( | ||
| 'CPy_UnboundLocalError("%s", "%s", "%s", %d, %s);' | ||
| % ( | ||
| self.source_path.replace("\\", "\\\\"), | ||
| op.traceback_entry[0], | ||
| attr.removeprefix(GENERATOR_ATTRIBUTE_PREFIX), | ||
| op.traceback_entry[1], | ||
| globals_static, | ||
| ) | ||
| ) | ||
| else: | ||
| self.emit_line( | ||
| 'CPy_AttributeError("%s", "%s", "%s", "%s", %d, %s);' | ||
| % ( | ||
| self.source_path.replace("\\", "\\\\"), | ||
| op.traceback_entry[0], | ||
| class_ir.name, | ||
| attr.removeprefix(GENERATOR_ATTRIBUTE_PREFIX), | ||
| op.traceback_entry[1], | ||
| globals_static, | ||
| ) | ||
| ) | ||
| ) | ||
| if DEBUG_ERRORS: | ||
| self.emit_line('assert(PyErr_Occurred() != NULL && "failure w/o err!");') | ||
|
|
||
|
|
||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better approach would be to add a new flag to ClassIR such as
is_environmentthat is set when the attributes represent local variables. This way if we add a new kind of generated class that doesn't represent locals,AttributeErrorwill be used.