Skip to content

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

@Christopher-Chianelli

Description

@Christopher-Chianelli

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesdocsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions