-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
gh-104374: Remove access to class scopes for inlined comprehensions #104528
Conversation
JelleZijlstra
commented
May 16, 2023
•
edited by bedevere-bot
Loading
edited by bedevere-bot
- Issue: List comprehensions now have access to the enclosing class scope #104374
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.
This doesn't look bad at all! Thanks for pursuing it. I guess we can present both this and #104519 as options.
Co-authored-by: Carl Meyer <carl@oddbird.net>
Found the following crash with a variation of Carl's fuzzer:
This doesn't reproduce on main, so it's new from this PR. |
Found a different issue trying to simplify it:
|
The problem occurs if it's a cell var in the outer comprehension and a free var in the inner one. In that case we should just do LOAD_FAST, but right now we're probably emitting _DEREF opcodes that explode because those cells don't exist in the class namespace. |
I pushed a fix for the above issues; it's just a matter of correcting how we decide if a comprehension is "in a class block." Looking only at |
|
||
def test_nested_free_var_in_iter(self): | ||
code = """ | ||
items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]] |
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.
Would be nice if we could test here that the name resolution in the inner listcomp here is right. I'll try to come up with some more tests around nested listcomps.
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.
Are you still planning to add more tests here, or improve this one?
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.
Yes, will push some more tests today or tomorrow. If you'd prefer to merge this now we can also do that; I can just write more tests in another PR.
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.
Yes, I think if we get known fixes merged sooner it's easier to do more effective fuzz testing; if we find more issues we can fix them in separate PRs, and we can add more tests that we think of in separate PRs.
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.
Just added two more tests and turned on automerge.
Now this segfaults:
Haven't tried to minimize it, but it's a compiler crash, something related to the cell/freevars:
|
Reduced: def a():
class a:
[(lambda : b) for b in [a]]
print(b) |
This also reproduces on main (without this PR) and with two functions, so it's not just related to class scopes: def a():
def a():
[(lambda : b) for b in [a]]
print(b) Crashes on current main with the same backtrace as above. |
Thanks, I'll open a separate issue and PR for this. |
|
||
def test_nested_free_var_in_iter(self): | ||
code = """ | ||
items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]] |
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.
Are you still planning to add more tests here, or improve this one?