@@ -335,6 +335,14 @@ The Python compiler currently generates the following bytecode instructions.
335
335
three.
336
336
337
337
338
+ .. opcode :: ROT_FOUR
339
+
340
+ Lifts second, third and forth stack items one position up, moves top down
341
+ to position four.
342
+
343
+ .. versionadded :: 3.8
344
+
345
+
338
346
.. opcode :: DUP_TOP
339
347
340
348
Duplicates the reference on top of the stack.
@@ -605,17 +613,6 @@ the original TOS1.
605
613
is terminated with :opcode: `POP_TOP `.
606
614
607
615
608
- .. opcode :: BREAK_LOOP
609
-
610
- Terminates a loop due to a :keyword: `break ` statement.
611
-
612
-
613
- .. opcode :: CONTINUE_LOOP (target)
614
-
615
- Continues a loop due to a :keyword: `continue ` statement. *target * is the
616
- address to jump to (which should be a :opcode: `FOR_ITER ` instruction).
617
-
618
-
619
616
.. opcode :: SET_ADD (i)
620
617
621
618
Calls ``set.add(TOS1[-i], TOS) ``. Used to implement set comprehensions.
@@ -676,7 +673,7 @@ iterations of the loop.
676
673
.. opcode :: POP_BLOCK
677
674
678
675
Removes one block from the block stack. Per frame, there is a stack of
679
- blocks, denoting nested loops, try statements, and such.
676
+ blocks, denoting :keyword: ` try ` statements, and such.
680
677
681
678
682
679
.. opcode :: POP_EXCEPT
@@ -687,11 +684,50 @@ iterations of the loop.
687
684
popped values are used to restore the exception state.
688
685
689
686
687
+ .. opcode :: POP_FINALLY (preserve_tos)
688
+
689
+ Cleans up the value stack and the block stack. If *preserve_tos * is not
690
+ ``0 `` TOS first is popped from the stack and pushed on the stack after
691
+ perfoming other stack operations:
692
+
693
+ * If TOS is ``NULL `` or an integer (pushed by :opcode: `BEGIN_FINALLY `
694
+ or :opcode: `CALL_FINALLY `) it is popped from the stack.
695
+ * If TOS is an exception type (pushed when an exception has been raised)
696
+ 6 values are popped from the stack, the last three popped values are
697
+ used to restore the exception state. An exception handler block is
698
+ removed from the block stack.
699
+
700
+ It is similar to :opcode: `END_FINALLY `, but doesn't change the bytecode
701
+ counter nor raise an exception. Used for implementing :keyword: `break `
702
+ and :keyword: `return ` in the :keyword: `finally ` block.
703
+
704
+ .. versionadded :: 3.8
705
+
706
+
707
+ .. opcode :: BEGIN_FINALLY
708
+
709
+ Pushes ``NULL `` onto the stack for using it in :opcode: `END_FINALLY `,
710
+ :opcode: `POP_FINALLY `, :opcode: `WITH_CLEANUP_START ` and
711
+ :opcode: `WITH_CLEANUP_FINISH `. Starts the :keyword: `finally ` block.
712
+
713
+ .. versionadded :: 3.8
714
+
715
+
690
716
.. opcode :: END_FINALLY
691
717
692
718
Terminates a :keyword: `finally ` clause. The interpreter recalls whether the
693
- exception has to be re-raised, or whether the function returns, and continues
694
- with the outer-next block.
719
+ exception has to be re-raised or execution has to be continued depending on
720
+ the value of TOS.
721
+
722
+ * If TOS is ``NULL `` (pushed by :opcode: `BEGIN_FINALLY `) continue from
723
+ the next instruction. TOS is popped.
724
+ * If TOS is an integer (pushed by :opcode: `CALL_FINALLY `), sets the
725
+ bytecode counter to TOS. TOS is popped.
726
+ * If TOS is an exception type (pushed when an exception has been raised)
727
+ 6 values are popped from the stack, the first three popped values are
728
+ used to re-raise the exception and the last three popped values are used
729
+ to restore the exception state. An exception handler block is removed
730
+ from the block stack.
695
731
696
732
697
733
.. opcode :: LOAD_BUILD_CLASS
@@ -704,9 +740,9 @@ iterations of the loop.
704
740
705
741
This opcode performs several operations before a with block starts. First,
706
742
it loads :meth: `~object.__exit__ ` from the context manager and pushes it onto
707
- the stack for later use by :opcode: `WITH_CLEANUP `. Then,
743
+ the stack for later use by :opcode: `WITH_CLEANUP_START `. Then,
708
744
:meth: `~object.__enter__ ` is called, and a finally block pointing to *delta *
709
- is pushed. Finally, the result of calling the enter method is pushed onto
745
+ is pushed. Finally, the result of calling the `` __enter__() `` method is pushed onto
710
746
the stack. The next opcode will either ignore it (:opcode: `POP_TOP `), or
711
747
store it in (a) variable(s) (:opcode: `STORE_FAST `, :opcode: `STORE_NAME `, or
712
748
:opcode: `UNPACK_SEQUENCE `).
@@ -716,30 +752,31 @@ iterations of the loop.
716
752
717
753
.. opcode :: WITH_CLEANUP_START
718
754
719
- Cleans up the stack when a :keyword: `with ` statement block exits. TOS is the
720
- context manager's :meth: `__exit__ ` bound method. Below TOS are 1--3 values
721
- indicating how/why the finally clause was entered:
755
+ Starts cleaning up the stack when a :keyword: `with ` statement block exits.
722
756
723
- * SECOND = `` None ``
724
- * (SECOND, THIRD) = (`` WHY_{RETURN,CONTINUE} ``), retval
725
- * SECOND = `` WHY_* ``; no retval below it
726
- * (SECOND, THIRD, FOURTH) = exc_info()
757
+ At the top of the stack are either `` NULL `` (pushed by
758
+ :opcode: ` BEGIN_FINALLY `) or 6 values pushed if an exception has been
759
+ raised in the with block. Below is the context manager's
760
+ :meth: ` ~object.__exit__ ` or :meth: ` ~object.__aexit__ ` bound method.
727
761
728
- In the last case, ``TOS(SECOND, THIRD, FOURTH) `` is called, otherwise
729
- ``TOS(None, None, None) ``. Pushes SECOND and result of the call
730
- to the stack.
762
+ If TOS is ``NULL ``, calls ``SECOND(None, None, None) ``,
763
+ removes the function from the stack, leaving TOS, and pushes ``None ``
764
+ to the stack. Otherwise calls ``SEVENTH(TOP, SECOND, THIRD) ``,
765
+ shifts the bottom 3 values of the stack down, replaces the empty spot
766
+ with ``NULL `` and pushes TOS. Finally pushes the result of the call.
731
767
732
768
733
769
.. opcode :: WITH_CLEANUP_FINISH
734
770
735
- Pops exception type and result of 'exit' function call from the stack .
771
+ Finishes cleaning up the stack when a :keyword: ` with ` statement block exits .
736
772
737
- If the stack represents an exception, *and * the function call returns a
738
- 'true' value, this information is "zapped" and replaced with a single
739
- ``WHY_SILENCED `` to prevent :opcode: `END_FINALLY ` from re-raising the
740
- exception. (But non-local gotos will still be resumed.)
773
+ TOS is result of ``__exit__() `` or ``__aexit__() `` function call pushed
774
+ by :opcode: `WITH_CLEANUP_START `. SECOND is ``None `` or an exception type
775
+ (pushed when an exception has been raised).
741
776
742
- .. XXX explain the WHY stuff!
777
+ Pops two values from the stack. If SECOND is not None and TOS is true
778
+ unwinds the EXCEPT_HANDLER block which was created when the exception
779
+ was caught and pushes ``NULL `` to the stack.
743
780
744
781
745
782
All of the following opcodes use their arguments.
@@ -987,22 +1024,19 @@ All of the following opcodes use their arguments.
987
1024
Loads the global named ``co_names[namei] `` onto the stack.
988
1025
989
1026
990
- .. opcode :: SETUP_LOOP (delta)
991
-
992
- Pushes a block for a loop onto the block stack. The block spans from the
993
- current instruction with a size of *delta * bytes.
994
-
1027
+ .. opcode :: SETUP_FINALLY (delta)
995
1028
996
- .. opcode :: SETUP_EXCEPT (delta)
1029
+ Pushes a try block from a try-finally or try-except clause onto the block
1030
+ stack. *delta * points to the finally block or the first except block.
997
1031
998
- Pushes a try block from a try-except clause onto the block stack. *delta *
999
- points to the first except block.
1000
1032
1033
+ .. opcode :: CALL_FINALLY (delta)
1001
1034
1002
- .. opcode :: SETUP_FINALLY (delta)
1035
+ Pushes the address of the next instruction onto the stack and increments
1036
+ bytecode counter by *delta *. Used for calling the finally block as a
1037
+ "subroutine".
1003
1038
1004
- Pushes a try block from a try-except clause onto the block stack. *delta *
1005
- points to the finally block.
1039
+ .. versionadded :: 3.8
1006
1040
1007
1041
1008
1042
.. opcode :: LOAD_FAST (var_num)
0 commit comments