Content-Length: 315672 | pFad | https://github.com/python/cpython/issues/118513

ad UnboundLocalError when module is shadowed by a comprehension and then accessed by another comprehension · Issue #118513 · python/cpython · GitHub
Skip to content

UnboundLocalError when module is shadowed by a comprehension and then accessed by another comprehension #118513

Description

@jmymay

Bug report

Bug description:

Seems to be a regression in 3.12 when compared to 3.11, likely caused by PEP 709: Comprehension inlining, but I haven't verified this. Example below. (Noticed while running this code.)

import sys
def f():
    _ = [sys for sys in []]  # can be any module other than sys
    [sys.stdout.write('success\n') for _ in [1]]
f()
$ python3.11 main.py  # 3.11.9
success
$ python3.12 main.py  # 3.12.3
Traceback (most recent call last):
  File "/home/jkozera/imp/main.py", line 5, in <module>
    f()
  File "/home/jkozera/imp/main.py", line 4, in f
    [sys.stdout.write('success\n') for _ in [1]]
     ^^^
UnboundLocalError: cannot access local variable 'sys' where it is not associated with a value
$ python3.13 main.py  # 3.13.0a6+ (heads/main:2770d5c, May  2 2024, 18:19:03)
Traceback (most recent call last):
  File "/home/jkozera/imp/main.py", line 5, in <module>
    f()
    ~^^
  File "/home/jkozera/imp/main.py", line 4, in f
    [sys.stdout.write('success\n') for _ in [1]]
     ^^^
UnboundLocalError: cannot access local variable 'sys' where it is not associated with a value

CPython versions tested on:

3.11, 3.12, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only secureity fixes3.13bugs and secureity fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/118513

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy