Closed
Description
Bug report
Checklist
- I am confident this is a bug in CPython, not a bug in a third-party project
- I have searched the CPython issue tracker,
and am confident this bug has not been reported before
CPython versions tested on:
3.12
Operating systems tested on:
macOS
Output from running 'python -VV' on the command line:
Python 3.12.0rc1 (main, Aug 29 2023, 19:34:25) [Clang 12.0.5 (clang-1205.0.22.9)]
A clear and concise description of the bug:
In the release notes for 3.12.0rc1 there is the following paragraph about comprehension inlining:
Comprehension iteration variables remain isolated; they don’t overwrite a variable of the same name in the outer scope, nor are they visible after the comprehension. This isolation is now maintained via stack/locals manipulation, not via separate function scope.
I found a case when a comprehension iteration variable overwrite a variable of the same name in the outer scope:
def foo(value):
try:
{int(key): value for key, value in value.items()}
except:
print(repr(value)) # will print 'baz' instead of {'bar': 'baz'}
foo({'bar':'baz'})
However, if you run this code outside of the function, isolation works:
value = {'bar': 'baz'}
try:
{int(key): value for key, value in value.items()}
except:
print(repr(value)) # ok, it will print {'bar': 'baz'}
Linked PRs
Metadata
Metadata
Assignees
Projects
Status
Done