python / python/cpython

inlined comprehension and closed over variables have buggy interactions

未關閉
#156,664 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

We are a bit late to report this, but the following of PyPy's unit tests got broken by inlined comprehensions in CPython 3.12:

def test_inlined_comprehension_private_name_reuse():
    x = 3

    def f():
        [x for x in [1]]
        return [x for _ in [1]]

    assert f() == [3]

test_inlined_comprehension_private_name_reuse()

this passes fine in CPython 3.11 but breaks like this in 3.12 onwards, including on main:

$ ./python /tmp/bug.py
Traceback (most recent call last):
  File "/tmp/bug.py", line 10, in <module>
    test_inlined_comprehension_private_name_reuse()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/bug.py", line 8, in test_inlined_comprehension_private_name_reuse
    assert f() == [3]
           ~^^
  File "/tmp/bug.py", line 6, in f
    return [x for _ in [1]]
            ^
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value

I wonder whether that is the kind of bug that would be easier to fix when doing #124697

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-156691

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,在 CPython main 上執行提供的重現程式 /tmp/bug.py,並與 Python 3.11 進行比較。在開始調查之前,閱讀與 issue #124697 的互動內容以及連結的 PR gh-156691。完成條件是範例在不發生 UnboundLocalError 的情況下回傳 [3],同時保留內嵌 comprehension 的行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
compilers
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。