On this page
opcode
Prev: numbers | Up: stdlib_module | Next: operator
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — opcode
Tags: #public #python3 #stdlib
Import
import opcode
Overview
CPython bytecode instruction tables (opname, opmap, HAVE_ARGUMENT, stack_effect(), hasconst, hasname, haslocal) used internally by dis and the compiler. Use opcode alongside dis when writing bytecode analyzers, coverage tools, or peephole inspection scripts.
Quick Start
import opcode
load_const = opcode.opmap["LOAD_CONST"]
assert opcode.opname[load_const] == "LOAD_CONST"
assert opcode.stack_effect(load_const, 0) == 1
Implementation Walkthrough
1. Checking Stack Effects of Instructions
opcode.stack_effect(op, oparg=None, *, jump=None) computes the net evaluation-stack delta for an instruction—essential when validating or transforming bytecode:
import opcode
assert opcode.stack_effect(opcode.opmap["POP_TOP"]) == -1
assert opcode.stack_effect(opcode.opmap["RETURN_VALUE"]) == -1
Gotchas & Boundaries
- CPython-specific and version-dependent: Opcode names, numbers, and specialization/cache entries change in almost every Python minor release.
- Pair with dis: Use
dis.get_instructions(fn)for high-level iteration rather than decoding rawcode.co_codebytes manually.
Common Functions
- opcode.stack_effect() —
stack_effect(opcode, oparg=None, /, *, jump=None)— Compute the stack effect of the opcode.
Constants & Attributes
opcode.EXTENDED_ARG(int) =69opcode.HAVE_ARGUMENT(int) =43opcode.cmp_op(tuple) =('<', '<=', '==', '!=', '>', '>=')opcode.hasarg(list) =[128, 255, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55...opcode.hascompare(list) =[56]opcode.hasconst(list) =[82]opcode.hasexc(list) =[263, 264, 265]opcode.hasfree(list) =[62, 90, 97, 111]opcode.hasjabs(list) =[]opcode.hasjrel(list) =[68, 70, 75, 76, 77, 100, 101, 102, 103, 106, 237, 248, 2...opcode.hasjump(list) =[68, 70, 75, 76, 77, 100, 101, 102, 103, 106, 237, 248, 2...opcode.haslocal(list) =[63, 83, 84, 85, 86, 87, 88, 89, 112, 113, 114, 261, 266]opcode.hasname(list) =[61, 64, 65, 72, 73, 80, 91, 92, 93, 96, 110, 115, 116, 249]opcode.opmap(dict) ={'CACHE': 0, 'RESERVED': 17, 'RESUME': 128, 'INSTRUMENTED...opcode.opname(list) =['CACHE', 'BINARY_SLICE', 'BUILD_TEMPLATE', 'BINARY_OP_IN...
Linked from
Related notes
- tuts/python3/stdlib_module/numbers Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/opcode/stack_effect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dis Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sys Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sysconfig Linked here; shared tags: #stdlib