Skip to content

Commit 1eb6184

Browse files
committed
edit exception handling
1 parent cd7725f commit 1eb6184

File tree

1 file changed

+97
-103
lines changed

1 file changed

+97
-103
lines changed

Diff for: InternalDocs/exception_handling.md

+97-103
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,86 @@
1-
Description of exception handling in Python 3.11
2-
------------------------------------------------
1+
Description of exception handling
2+
---------------------------------
33

4-
Python 3.11 uses what is known as "zero-cost" exception handling.
5-
Prior to 3.11, exceptions were handled by a runtime stack of "blocks".
6-
7-
In zero-cost exception handling, the cost of supporting exceptions is minimized.
8-
In the common case (where no exception is raised) the cost is reduced
9-
to zero (or close to zero).
4+
Python uses a technique known as "zero-cost" exception handling, which
5+
minimizes the cost of supporting exceptions. In the common case (where
6+
no exception is raised) the cost is reduced to zero (or close to zero).
107
The cost of raising an exception is increased, but not by much.
118

129
The following code:
1310

14-
def f():
15-
try:
16-
g(0)
17-
except:
18-
return "fail"
19-
20-
compiles as follows in 3.10:
21-
22-
2 0 SETUP_FINALLY 7 (to 16)
23-
24-
3 2 LOAD_GLOBAL 0 (g)
25-
4 LOAD_CONST 1 (0)
26-
6 CALL_NO_KW 1
27-
8 POP_TOP
28-
10 POP_BLOCK
29-
12 LOAD_CONST 0 (None)
30-
14 RETURN_VALUE
31-
32-
4 >> 16 POP_TOP
33-
18 POP_TOP
34-
20 POP_TOP
35-
36-
5 22 POP_EXCEPT
37-
24 LOAD_CONST 3 ('fail')
38-
26 RETURN_VALUE
39-
40-
Note the explicit instructions to push and pop from the "block" stack:
41-
SETUP_FINALLY and POP_BLOCK.
42-
43-
In 3.11, the SETUP_FINALLY and POP_BLOCK are eliminated, replaced with
44-
a table to determine where to jump to when an exception is raised.
45-
46-
1 0 RESUME 0
47-
48-
2 2 NOP
49-
50-
3 4 LOAD_GLOBAL 1 (g + NULL)
51-
16 LOAD_CONST 1 (0)
52-
18 PRECALL 1
53-
22 CALL 1
54-
32 POP_TOP
55-
34 LOAD_CONST 0 (None)
56-
36 RETURN_VALUE
57-
>> 38 PUSH_EXC_INFO
58-
59-
4 40 POP_TOP
60-
61-
5 42 POP_EXCEPT
62-
44 LOAD_CONST 2 ('fail')
63-
46 RETURN_VALUE
64-
>> 48 COPY 3
65-
50 POP_EXCEPT
66-
52 RERAISE 1
67-
ExceptionTable:
68-
4 to 32 -> 38 [0]
69-
38 to 40 -> 48 [1] lasti
70-
71-
(Note this code is from 3.11, later versions may have slightly different bytecode.)
72-
73-
If an instruction raises an exception then its offset is used to find the target to jump to.
74-
For example, the CALL at offset 22, falls into the range 4 to 32.
75-
So, if g() raises an exception, then control jumps to offset 38.
76-
11+
<code>
12+
try:
13+
g(0)
14+
except:
15+
res = "fail"
16+
</code>
17+
18+
compiles into pseudo-code like the following:
19+
20+
`RESUME` 0
21+
22+
1 `SETUP_FINALLY` 8 (to L1)
23+
24+
2 `LOAD_NAME` 0 (g)
25+
`PUSH_NULL`
26+
`LOAD_CONST` 0 (0)
27+
`CALL` 1
28+
`POP_TOP`
29+
`POP_BLOCK`
30+
31+
-- L1: `PUSH_EXC_INFO`
32+
33+
3 `POP_TOP`
34+
35+
4 `LOAD_CONST` 1 ('fail')
36+
`STORE_NAME` 1 (res)
37+
38+
The `SETUP_FINALLY` instruction specifies that henceforth, exceptions
39+
are handled by the code at label L1. The `POP_BLOCK` instruction
40+
reverses the effect of the last `SETUP_FINALLY`, so the exception
41+
handler reverts to what it was before.
42+
43+
Note that the `SETUP_FINALLY` and `POP_BLOCK` instructions have no effect
44+
when no exceptions are raised. The idea of zero-cost exception handling
45+
is to replace these instructions by metadata which is stored alongside
46+
the code, and which is inspected only when an exception occurs.
47+
This metadata is the exception table, which is stored in the code
48+
object's `co_exceptiontable` field.
49+
50+
When the pseudo-instructions are translated into bytecode, the
51+
`SETUP_FINALLY` and `POP_BLOCK` instructions are removed, and the
52+
exception table is constructed, mapping each instruction to the
53+
the exception handler that covers it, if any. Instructions which
54+
are not covered by any exception handler within the same code
55+
object's bytecode, do not appear in the exception table at all.
56+
57+
For the code object in our example above, the table has a single
58+
entry specifying that all instructions between the `SETUP_FINALLY`
59+
and the `POP_BLOCK` are covered by the exception handler located
60+
at label `L1`.
61+
62+
At runtime, when an exception occurs, the interpreted looks up
63+
the offset of the current instruction in the exception table. If
64+
it finds a handler, control flow transfers to it. Otherwise, the
65+
exception bubbles up to the caller, and the caller's frame is
66+
checked for a handler covering the `CALL` instruction. This
67+
repeats until a handler is found or the topmost frame is reached,
68+
and the program terminates. During unwinding, the traceback
69+
is constructed.
7770

7871
Unwinding
7972
---------
8073

81-
When an exception is raised, the current instruction offset is used to find following:
82-
target to jump to, stack depth, and 'lasti', which determines whether the instruction
83-
offset of the raising instruction should be pushed.
84-
85-
This information is stored in the exception table, described below.
74+
Along with the location of an exception handler, each entry of the
75+
exception table also contains the stack depth of the `try` instruction
76+
and a boolean `lasti` value, which indicates whether the instruction
77+
offset of the raising instruction should be pushed to the stack.
8678

87-
If there is no relevant entry, the exception bubbles up to the caller.
79+
Handling an exception, once an exception table entry is found, consists
80+
of the following steps:
8881

89-
If there is an entry, then:
9082
1. pop values from the stack until it matches the stack depth for the handler.
91-
2. if 'lasti' is true, then push the offset that the exception was raised at.
83+
2. if `lasti` is true, then push the offset that the exception was raised at.
9284
3. push the exception to the stack.
9385
4. jump to the target offset and resume execution.
9486

@@ -97,51 +89,51 @@ Format of the exception table
9789
-----------------------------
9890

9991
Conceptually, the exception table consists of a sequence of 5-tuples:
100-
1. start-offset (inclusive)
101-
2. end-offset (exclusive)
102-
3. target
103-
4. stack-depth
104-
5. push-lasti (boolean)
92+
1. `start-offset` (inclusive)
93+
2. `end-offset` (exclusive)
94+
3. `target`
95+
4. `stack-depth`
96+
5. `push-lasti` (boolean)
10597

106-
All offsets and lengths are in instructions, not bytes.
98+
All offsets and lengths are in code units, not bytes.
10799

108100
We want the format to be compact, but quickly searchable.
109101
For it to be compact, it needs to have variable sized entries so that we can store common (small) offsets compactly, but handle large offsets if needed.
110102
For it to be searchable quickly, we need to support binary search giving us log(n) performance in all cases.
111103
Binary search typically assumes fixed size entries, but that is not necessary, as long as we can identify the start of an entry.
112104

113105
It is worth noting that the size (end-start) is always smaller than the end, so we encode the entries as:
114-
start, size, target, depth, push-lasti
106+
`start, size, target, depth, push-lasti`
115107

116-
Also, sizes are limited to 2**30 as the code length cannot exceed 2**31 and each instruction takes 2 bytes.
108+
Also, sizes are limited to 2**30 as the code length cannot exceed 2**31 and each code unit takes 2 bytes.
117109
It also happens that depth is generally quite small.
118110

119111
So, we need to encode:
120-
start (up to 30 bits)
121-
size (up to 30 bits)
122-
target (up to 30 bits)
123-
depth (up to ~8 bits)
124-
lasti (1 bit)
112+
`start` (up to 30 bits)
113+
`size` (up to 30 bits)
114+
`target` (up to 30 bits)
115+
`depth` (up to ~8 bits)
116+
`lasti` (1 bit)
125117

126118
We need a marker for the start of the entry, so the first byte of entry will have the most significant bit set.
127119
Since the most significant bit is reserved for marking the start of an entry, we have 7 bits per byte to encode offsets.
128120
Encoding uses a standard varint encoding, but with only 7 bits instead of the usual 8.
129-
The 8 bits of a bit are (msb left) SXdddddd where S is the start bit. X is the extend bit meaning that the next byte is required to extend the offset.
121+
The 8 bits of a byte are (msb left) SXdddddd where S is the start bit. X is the extend bit meaning that the next byte is required to extend the offset.
130122

131-
In addition, we will combine depth and lasti into a single value, ((depth<<1)+lasti), before encoding.
123+
In addition, we combine `depth` and `lasti` into a single value, `((depth<<1)+lasti)`, before encoding.
132124

133125
For example, the exception entry:
134-
start: 20
135-
end: 28
136-
target: 100
137-
depth: 3
138-
lasti: False
126+
`start`: 20
127+
`end`: 28
128+
`target`: 100
129+
`depth`: 3
130+
`lasti`: False
139131

140132
is encoded first by converting to the more compact four value form:
141-
start: 20
142-
size: 8
143-
target: 100
144-
depth<<1+lasti: 6
133+
`start`: 20
134+
`size`: 8
135+
`target`: 100
136+
`depth<<1+lasti`: 6
145137

146138
which is then encoded as:
147139
148 (MSB + 20 for start)
@@ -157,6 +149,7 @@ for a total of five bytes.
157149
Script to parse the exception table
158150
-----------------------------------
159151

152+
<code>
160153
def parse_varint(iterator):
161154
b = next(iterator)
162155
val = b & 63
@@ -180,3 +173,4 @@ def parse_exception_table(code):
180173
yield start, end, target, depth, lasti
181174
except StopIteration:
182175
return
176+
</code>

0 commit comments

Comments
 (0)