Skip to content
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

Comprehension iteration variable overwrite a variable of the same name in the outer scope inside a function with try..except #108654

Closed
2 tasks done
Fatal1ty opened this issue Aug 29, 2023 · 3 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes release-blocker type-bug An unexpected behavior, bug, or error

Comments

@Fatal1ty
Copy link

Fatal1ty commented Aug 29, 2023

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

@Eclips4
Copy link
Member

Eclips4 commented Aug 29, 2023

cc @carljm

@Eclips4 Eclips4 added 3.12 bugs and security fixes 3.13 new features, bugs and security fixes labels Aug 29, 2023
@carljm carljm self-assigned this Aug 29, 2023
carljm added a commit that referenced this issue Aug 30, 2023
…08659)

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 30, 2023
…on (pythonGH-108659)

(cherry picked from commit d52c448)

Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Yhg1s pushed a commit that referenced this issue Aug 31, 2023
…ion (GH-108659) (#108700)

gh-108654: restore comprehension locals before handling exception (GH-108659)
(cherry picked from commit d52c448)

Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
@AlexWaygood
Copy link
Member

This looks completed, but please reopen if not @carljm!

@Fatal1ty
Copy link
Author

Fatal1ty commented Sep 6, 2023

I can confirm that it's fixed it 3.12.0rc2 🎉

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 9, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 11, 2023
…109189)

(cherry picked from commit c0f488b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Yhg1s pushed a commit that referenced this issue Sep 12, 2023
…09271)

gh-109182: Fix and improve tests for gh-108654 (GH-109189)
(cherry picked from commit c0f488b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes release-blocker type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

4 participants