[Python-Dev] The purpose of SETUP_LOOP, BREAK_LOOP, CONTINUE_LOOP

Antoine Pitrou solipsis at pitrou.net
Sat Mar 12 11:59:01 CET 2011


Hello,

> Am I missing something? Does SETUP_LOOP serve any other purpose?

Not to my knowledge.

> Similarly, it looks like BREAK_LOOP and CONTINUE_LOOP are just jumps
> that respect try/finally blocks (i.e. jumping out of try executes
> finally). Is there more semantics to them than this?

There are also "with" blocks :-) (which use separate opcodes, although
they are similar in principle to try/finally blocks)

> 1) If not in try/finally, simply generate a direct jump outside of the
> loop (break) or to the start of the loop (continue).

Right.

> 2) If in try/finally, generate a new instruction JUMP_FROM_TRY which
> replaces both BREAK_LOOP and CONTINUE_LOOP. It behaves the same way as
> CONTINUE_LOOP but without restriction to only jump backwards (could
> reuse CONTINUE_LOOP, but the name would be misleading).

There may be complications with nested try/finally blocks. You either
need to generate separate bytecode for when the "finally" clause is
entered following a given continue/break (so as to hardcode the jump
offset at the end of the clause), or save the jump offsets somewhere on
a stack for each finally clause to pop, IMO.

(the current code does the latter with various complications, see
WITH_CLEANUP for some aspects of the fun)

Regards

Antoine.




More information about the Python-Dev mailing list