python / python/cpython

LOAD_FAST access names outside of co_varnames

Open
#122,794 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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',)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the LOAD_FAST entry in the linked Python 3.13 dis documentation and reproduce the supplied simple_cellvar disassembly. Check how the displayed name relates to co_varnames and cell variables, then update the documentation so its description matches the demonstrated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
Half a day
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.