python / python/cpython

LOAD_FAST access names outside of co_varnames

未关闭
#122,794 4 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

docs
主要语言
Python
星标
77.2k
派生
35.9k
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 和单元格变量之间的关系,然后更新文档,使其描述与演示的行为一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
3/5
预计耗时
半天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。