python / python/cpython

LOAD_FAST access names outside of co_varnames

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

還沒有人認領這個 Issue。

docs
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Documentation

LOAD_FAST is documented (https://docs.python.org/3.13/library/dis.html#opcode-LOAD_FAST) as:
Pushes a reference to the local co_varnames[var_num] onto the stack.

However running dis on the following snippet shows that it may load other names:


def simple_cellvar():  # a cellvar in f
    a = 1

    def g():  # a freevar in g
        return a

    return g

import dis

dis.dis(simple_cellvar)
print()
print(f"Var names {simple_cellvar.__code__.co_varnames}")

Output

--           MAKE_CELL                1 (a)

   5           RESUME                   0

   6           LOAD_CONST               1 (1)
               STORE_DEREF              1 (a)

   8           LOAD_FAST                1 (a)      <---- a does not appear in the co_varnames !!!
               BUILD_TUPLE              1
               LOAD_CONST               2 (<code object g at 0x0000027404FB0ED0, ...)
               MAKE_FUNCTION
               SET_FUNCTION_ATTRIBUTE   8 (closure)
               STORE_FAST               0 (g)

  11           LOAD_FAST                0 (g)
               RETURN_VALUE

Disassembly of <code object g at 0x0000027404FB0ED0, ...>:
  --           COPY_FREE_VARS           1

   8           RESUME                   0

   9           LOAD_DEREF               0 (a)
               RETURN_VALUE

Var names ('g',)

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從連結的 Python 3.13 dis 文件中的 LOAD_FAST 項目開始,重現所提供的 simple_cellvar 反組譯結果。檢查顯示的名稱與 co_varnames 和 cell 變數之間的關係,然後更新文件,使其描述與示範的行為一致。

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

評估

技術堆疊
python
領域
documentation
Issue 類型
文件
難度
3/5
預估耗時
半天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
48/100

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

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