python / python/cpython

dis.cmp_op doc is inaccurate in Python 3.12.2 and 3.13.0a5

Open
#117,270 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 docs type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

From https://docs.python.org/3.12/library/dis.html#opcode-COMPARE_OP,
dis.cmp_op should be able lookup the comparison operation string from the opcode's argument.
However, in Python 3.12.2 and 3.13.0a5, this fails with "IndexError: tuple index out of range"

import dis

def my_fun(a, b):
    return a <= b

for instruction in dis.get_instructions(my_fun):
    if instruction.opname == 'COMPARE_OP':
        print(dis.cmp_op[instruction.arg])

In Python 3.11.8, it prints "<=" as expected.

dis.dis output of my_fun in Python 3.12.2:

  1           0 RESUME                   0

  2           2 LOAD_FAST                0 (a)
              4 LOAD_FAST                1 (b)
              6 COMPARE_OP              26 (<=)
             10 RETURN_VALUE

dis.cmp_op in Python 3.12.2:

('<', '<=', '==', '!=', '>', '>=')

Workaround: use instruction.argrepr.

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-117272
  • gh-117274

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 dis.cmp_op documentation and the reported dis.get_instructions() example, then compare the Python 3.12 and 3.13 behavior with Python 3.11. Review linked PRs gh-117272 and gh-117274 for the current direction; done means the documentation accurately explains the comparison-operation lookup and the available workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.