You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Comprehension iteration variable overwrite a variable of the same name in the outer scope inside a function with try..except #108654
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:
deffoo(value):
try:
{int(key): valueforkey, valueinvalue.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): valueforkey, valueinvalue.items()}
except:
print(repr(value)) # ok, it will print {'bar': 'baz'}
Activity
Eclips4 commentedon Aug 29, 2023
cc @carljm
pythongh-108654: restore comprehension locals before handling exception
pythongh-108654: restore comprehension locals before handling exception
pythongh-108654: restore comprehension locals before handling exception
gh-108654: restore comprehension locals before handling exception (#1…
pythongh-108654: restore comprehension locals before handling excepti…
[3.12] gh-108654: restore comprehension locals before handling except…
AlexWaygood commentedon Aug 31, 2023
This looks completed, but please reopen if not @carljm!
Fatal1ty commentedon Sep 6, 2023
I can confirm that it's fixed it 3.12.0rc2 🎉
pythongh-109182: Fix and improve tests for pythongh-108654
gh-109182: Fix and improve tests for gh-108654 (GH-109189)
pythongh-109182: Fix and improve tests for pythongh-108654 (pythonGH-…
[3.12] gh-109182: Fix and improve tests for gh-108654 (GH-109189) (#1…