杰瑞科技汇

Python bytecode list如何生成与解析?

Of course! Here is a comprehensive list and explanation of Python bytecode, the set of instructions that the Python Virtual Machine (PVM) executes.

Python bytecode list如何生成与解析?-图1
(图片来源网络,侵删)

What is Python Bytecode?

When you run a Python script, it's not directly executed by the CPU. Instead, it's first compiled into a lower-level platform-independent set of instructions called bytecode. This bytecode is then executed by the Python Virtual Machine (PVM).

You can think of this process as: Your Python Code (.py file) -> Compiler -> Bytecode (.pyc file) -> Python Virtual Machine (PVM) -> Execution

This intermediate step is what makes Python an interpreted language (though it's more accurately a "compiled-interpreted" language). It also enables powerful features like the dis module for disassembling code.


How to View Bytecode

The easiest way to explore bytecode is by using Python's built-in dis (disassembler) module.

Python bytecode list如何生成与解析?-图2
(图片来源网络,侵删)
import dis
def greet(name):
    message = f"Hello, {name}!"
    print(message)
dis.dis(greet)

Output:

  4           0 LOAD_CONST               1 (f'Hello, {name}!')
              2 STORE_FAST               1 (message)
  5           4 LOAD_GLOBAL              0 (print)
              6 LOAD_FAST                1 (message)
              8 CALL_FUNCTION            1
             10 POP_TOP
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE

The Core Components of Bytecode

  1. Instruction (Opcode): A single byte that represents an operation (e.g., LOAD_CONST, STORE_FAST, CALL_FUNCTION). This is the "verb" of the instruction.
  2. Operand: The argument for the instruction (e.g., the index of a constant in the constant list, the name of a variable). This is the "noun". Some instructions don't need an operand.
  3. The Code Object: A block of bytecode is associated with a code object, which contains metadata like:
    • co_consts: A tuple of literal constants used in the code (e.g., strings, numbers, None).
    • co_names: A tuple of global variable names.
    • co_varnames: A tuple of local variable names.
    • co_code: The actual bytecode string (a sequence of opcodes and operands).

Comprehensive Bytecode Instruction List

Here is a categorized list of common Python bytecode instructions, with explanations and examples.

Stack Operations

These instructions manipulate the main execution stack.

Instruction Operand Description Example
LOAD_CONST const_index Pushes a constant from co_consts onto the stack. LOAD_CONST 1 (pushes the string "hello")
LOAD_FAST var_index Pushes a local variable from co_varnames onto the stack. LOAD_FAST 0 (pushes the first local variable)
LOAD_GLOBAL name_index Pushes a global variable from co_names onto the stack. LOAD_GLOBAL 0 (pushes the print function)
STORE_FAST var_index Pops a value from the stack and stores it in a local variable. STORE_FAST 0 (stores the top of stack in the first local var)
POP_TOP Pops the top value from the stack and discards it. Used after print call, which returns None.
DUP_TOP Duplicates the top item on the stack. Useful when you need to use a value multiple times.
ROT_TWO Swaps the top two items on the stack. a, b = b, a would use ROT_TWO.
ROT_THREE Rotates the top three items on the stack. a, b, c = b, c, a would use ROT_THREE.

Function and Class Operations

Instruction Operand Description Example
CALL_FUNCTION argc Calls a function. argc is the number of positional arguments. CALL_FUNCTION 1 (calls the function with 1 arg from the stack)
CALL_METHOD argc Calls a method on an object. argc is the number of positional arguments. CALL_METHOD 1 (calls a method with 1 arg)
MAKE_FUNCTION Creates a new function object from a code object on the stack. Used for def my_func(): ...
RETURN_VALUE Pops a value from the stack and returns it from the current function. The final instruction in most functions.
YIELD_VALUE Pops a value from the stack and yields it in a generator function. yield x
SETUP_LOOP target Sets up a loop block. target is the address of the loop's end. for i in range(3):
JUMP_ABSOLUTE target Unconditionally jumps to a new instruction address. JUMP_ABSOLUTE 4 (jump to instruction 4)
JUMP_FORWARD delta Jumps forward by delta bytes. JUMP_FORWARD 10
POP_BLOCK Exits the current block (e.g., a loop or with statement). Used at the end of a loop body.

Control Flow (Jumps and Comparisons)

Instruction Operand Description Example
COMPARE_OP oparg Pops two items, compares them, and pushes the result (True/False). COMPARE_OP 2 (is )
POP_JUMP_IF_FALSE target Pops the top value. If it's false, jumps to target. if x:
POP_JUMP_IF_TRUE target Pops the top value. If it's true, jumps to target. if not x:
JUMP_IF_FALSE_OR_POP target Pops the top value. If false, jumps to target. Otherwise, pushes the value back. Used for short-circuiting and.
JUMP_IF_TRUE_OR_POP target Pops the top value. If true, jumps to target. Otherwise, pushes the value back. Used for short-circuiting or.

Collections and Operations

Instruction Operand Description Example
BUILD_LIST count Pops count items from the stack and builds a list. BUILD_LIST 3
BUILD_TUPLE count Pops count items from the stack and builds a tuple. BUILD_TUPLE 2 (for (a, b))
BUILD_MAP count Pops 2*count key/value pairs and builds a dictionary. BUILD_MAP 0
STORE_SUBSCR Pops [obj, key, value] and executes obj[key] = value. my_dict['key'] = 'value'
BINARY_ADD Pops two values, adds them, and pushes the result. a + b
BINARY_SUBTRACT Pops two values, subtracts them, and pushes the result. a - b
BINARY_MULTIPLY Pops two values, multiplies them, and pushes the result. a * b
INPLACE_ADD Pops two values, adds them, and pushes the result. Similar to BINARY_ADD but often used for in-place ops like list +=. my_list += [1, 2]
UNARY_POSITIVE Pops a value, applies , and pushes the result. +x
UNARY_NEGATIVE Pops a value, applies , and pushes the result. -x
UNARY_NOT Pops a value, applies not, and pushes the result. not x

Attribute and Item Access

Instruction Operand Description Example
LOAD_ATTR name_index Pops an object, loads an attribute from it, and pushes the attribute. obj.method
STORE_ATTR name_index Pops [obj, value] and executes obj.attr = value. obj.attr = value
DELETE_ATTR name_index Pops an
Python bytecode list如何生成与解析?-图3
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇