Closed
Description
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