bpo-17611. Move unwinding of stack for "pseudo exceptions" from inter… · pythoncapi/cpython@520b7ae · GitHub
Skip to content

Commit 520b7ae

Browse files
serhiy-storchakamarkshannonpitrou
authored
bpo-17611. Move unwinding of stack for "pseudo exceptions" from interpreter to compiler. (pythonGH-5006)
Co-authored-by: Mark Shannon <mark@hotpy.org> Co-authored-by: Antoine Pitrou <antoine@python.org>
1 parent 4af8fd5 commit 520b7ae

19 files changed

Lines changed: 4497 additions & 4383 deletions

File tree

Doc/library/dis.rst

Lines changed: 77 additions & 43 deletions

Doc/whatsnew/3.8.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,21 @@ Changes in the Python API
137137
:func:`dbm.dumb.open` with flags ``'r'`` and ``'w'`` no longer creates
138138
a database if it does not exist.
139139
(Contributed by Serhiy Storchaka in :issue:`32749`.)
140+
141+
142+
CPython bytecode changes
143+
------------------------
144+
145+
* The interpreter loop has been simplified by moving the logic of unrolling
146+
the stack of blocks into the compiler. The compiler emits now explicit
147+
instructions for adjusting the stack of values and calling the cleaning
148+
up code for :keyword:`break`, :keyword:`continue` and :keyword:`return`.
149+
150+
Removed opcodes :opcode:`BREAK_LOOP`, :opcode:`CONTINUE_LOOP`,
151+
:opcode:`SETUP_LOOP` and :opcode:`SETUP_EXCEPT`. Added new opcodes
152+
:opcode:`ROT_FOUR`, :opcode:`BEGIN_FINALLY`, :opcode:`CALL_FINALLY` and
153+
:opcode:`POP_FINALLY`. Changed the behavior of :opcode:`END_FINALLY`
154+
and :opcode:`WITH_CLEANUP_START`.
155+
156+
(Contributed by Mark Shannon, Antoine Pitrou and Serhiy Storchaka in
157+
:issue:`17611`.)

Include/opcode.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern "C" {
1212
#define ROT_THREE 3
1313
#define DUP_TOP 4
1414
#define DUP_TOP_TWO 5
15+
#define ROT_FOUR 6
1516
#define NOP 9
1617
#define UNARY_POSITIVE 10
1718
#define UNARY_NEGATIVE 11
@@ -32,6 +33,7 @@ extern "C" {
3233
#define GET_AITER 50
3334
#define GET_ANEXT 51
3435
#define BEFORE_ASYNC_WITH 52
36+
#define BEGIN_FINALLY 53
3537
#define INPLACE_ADD 55
3638
#define INPLACE_SUBTRACT 56
3739
#define INPLACE_MULTIPLY 57
@@ -55,7 +57,6 @@ extern "C" {
5557
#define INPLACE_AND 77
5658
#define INPLACE_XOR 78
5759
#define INPLACE_OR 79
58-
#define BREAK_LOOP 80
5960
#define WITH_CLEANUP_START 81
6061
#define WITH_CLEANUP_FINISH 82
6162
#define RETURN_VALUE 83
@@ -92,9 +93,6 @@ extern "C" {
9293
#define POP_JUMP_IF_FALSE 114
9394
#define POP_JUMP_IF_TRUE 115
9495
#define LOAD_GLOBAL 116
95-
#define CONTINUE_LOOP 119
96-
#define SETUP_LOOP 120
97-
#define SETUP_EXCEPT 121
9896
#define SETUP_FINALLY 122
9997
#define LOAD_FAST 124
10098
#define STORE_FAST 125
@@ -127,6 +125,8 @@ extern "C" {
127125
#define BUILD_TUPLE_UNPACK_WITH_CALL 158
128126
#define LOAD_METHOD 160
129127
#define CALL_METHOD 161
128+
#define CALL_FINALLY 162
129+
#define POP_FINALLY 163
130130

131131
/* EXCEPT_HANDLER is a special, implicit block type which is created when
132132
entering an except handler. It is not an opcode but we define it here

Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def _write_atomic(path, data, mode=0o666):
246246
# Python 3.7a2 3391 (update GET_AITER #31709)
247247
# Python 3.7a4 3392 (PEP 552: Deterministic pycs #31650)
248248
# Python 3.7b1 3393 (remove STORE_ANNOTATION opcode #32550)
249+
# Python 3.8a1 3400 (move frame block handling to compiler #17611)
249250
#
250251
# MAGIC must change whenever the bytecode emitted by the compiler may no
251252
# longer be understood by older implementations of the eval loop (usually
@@ -254,7 +255,7 @@ def _write_atomic(path, data, mode=0o666):
254255
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
255256
# in PC/launcher.c must also be updated.
256257

257-
MAGIC_NUMBER = (3393).to_bytes(2, 'little') + b'\r\n'
258+
MAGIC_NUMBER = (3400).to_bytes(2, 'little') + b'\r\n'
258259
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
259260

260261
_PYCACHE = '__pycache__'

Lib/opcode.py

Lines changed: 5 additions & 6 deletions

0 commit comments

Comments
 (0)