python / python/cpython

Wrong binding of closure variable with intervening list comprehension

未關閉
#121,377 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Bug report

Bug description:
def f(x):
    def g():
        print([x for x in range(10)])
        def h():
            print(x)
        return h
    print(x)
    return g
f(1)()()

The above had always bound x in h to the parameter of f. But it no longer works and somehow binds to a local in g, throwing the following error:

NameError: cannot access free variable 'x' where it is not associated with a value in enclosing scope
CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Linked PRs
  • gh-124696

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

在 CPython 3.12 上重現 closure 和列表推導式範例,並將其輸出與回報的 NameError 進行比較。先檢視該範例涉及的作用域和編譯路徑,然後檢查關聯的 PR gh-124696。當範例將 x 繫結至 f 的參數並在沒有該錯誤的情況下完成時,即表示完成。

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

評估

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

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

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