Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-104377: fix cell in comprehension that is free in outer scope#104394
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
59 changes: 55 additions & 4 deletionsLib/test/test_listcomps.py
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 |
|---|---|---|
| @@ -117,15 +117,15 @@ def get_output(moddict, name): | ||
| newcode = code | ||
| def get_output(moddict, name): | ||
| return moddict[name] | ||
| newns = ns.copy() if ns else {} | ||
| try: | ||
| exec(newcode,newns) | ||
| except raises as e: | ||
| # We care about e.g. NameError vs UnboundLocalError | ||
| self.assertIs(type(e), raises) | ||
| else: | ||
| for k, v in (outputs or {}).items(): | ||
| self.assertEqual(get_output(newns, k), v) | ||
| def test_lambdas_with_iteration_var_as_default(self): | ||
| code = """ | ||
| @@ -180,6 +180,26 @@ def test_closure_can_jump_over_comp_scope(self): | ||
| z = [x() for x in items] | ||
| """ | ||
| outputs = {"z": [2, 2, 2, 2, 2]} | ||
| self._check_in_scopes(code, outputs, scopes=["module", "function"]) | ||
| def test_cell_inner_free_outer(self): | ||
| code = """ | ||
| def f(): | ||
| return [lambda: x for x in (x, [1])[1]] | ||
| x = ... | ||
| y = [fn() for fn in f()] | ||
| """ | ||
| outputs = {"y": [1]} | ||
| self._check_in_scopes(code, outputs, scopes=["module", "function"]) | ||
| def test_free_inner_cell_outer(self): | ||
| code = """ | ||
| g = 2 | ||
| def f(): | ||
| return g | ||
| y = [g for x in [1]] | ||
| """ | ||
| outputs = {"y": [2]} | ||
| self._check_in_scopes(code, outputs) | ||
| def test_inner_cell_shadows_outer_redefined(self): | ||
| @@ -203,6 +223,37 @@ def inner(): | ||
| outputs = {"x": -1} | ||
| self._check_in_scopes(code, outputs, ns={"g": -1}) | ||
| def test_explicit_global(self): | ||
| code = """ | ||
| global g | ||
Member 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 never realized you could write | ||
| x = g | ||
| g = 2 | ||
| items = [g for g in [1]] | ||
| y = g | ||
| """ | ||
| outputs = {"x": 1, "y": 2, "items": [1]} | ||
| self._check_in_scopes(code, outputs, ns={"g": 1}) | ||
| def test_explicit_global_2(self): | ||
| code = """ | ||
| global g | ||
| x = g | ||
| g = 2 | ||
| items = [g for x in [1]] | ||
| y = g | ||
| """ | ||
| outputs = {"x": 1, "y": 2, "items": [2]} | ||
| self._check_in_scopes(code, outputs, ns={"g": 1}) | ||
| def test_explicit_global_3(self): | ||
| code = """ | ||
| global g | ||
| fns = [lambda: g for g in [2]] | ||
| items = [fn() for fn in fns] | ||
| """ | ||
| outputs = {"items": [2]} | ||
| self._check_in_scopes(code, outputs, ns={"g": 1}) | ||
| def test_assignment_expression(self): | ||
| code = """ | ||
| x = -1 | ||
| @@ -250,7 +301,7 @@ def g(): | ||
| g() | ||
| """ | ||
| outputs = {"x": 1} | ||
| self._check_in_scopes(code, outputs, scopes=["module", "function"]) | ||
| def test_introspecting_frame_locals(self): | ||
| code = """ | ||
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.