Какова цель отслеживания блоков в байт-коде Python?
В документации здесь упоминается:
... Для каждого кадра имеется стек блоков, обозначающих вложенные циклы, операторы try и т. Д.
Но на самом деле они не кажутся необходимыми для выполнения циклов. Например, играя с REPL, я вижу:
>>> def foo():
... while True:
... print('hi')
...
>>> for inst in list(dis.get_instructions(foo)): print(inst)
...
Instruction(opname='SETUP_LOOP', opcode=120, arg=12, argval=14, argrepr='to 14', offset=0, starts_line=2, is_jump_target=False)
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=2, starts_line=3, is_jump_target=True)
Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval='hi', argrepr="'hi'", offset=4, starts_line=None, is_jump_target=False)
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=6, starts_line=None, is_jump_target=False)
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=8, starts_line=None, is_jump_target=False)
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=2, argval=2, argrepr='', offset=10, starts_line=None, is_jump_target=False)
Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False)
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=14, starts_line=None, is_jump_target=True)
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False)
Указанная инструкция JUMP_ABSOLUTE
переходит к указанной инструкции LOAD_GLOBAL
. Из простого ознакомления с инструкциями кажется, что коды операций SETUP_LOOP
и POP_BLOCK
могут быть неактивными.
Из того, что я понимаю, в Python нет блочных переменных, поэтому не нравится, что это также является причиной.