Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a414fa6
Add SETUP_CLEANUP bytecode to track lasti for re-raising exceptions i…
markshannon Apr 19, 2021
902690d
Move some handling of exceptions to bytecode from unwind code.
markshannon Apr 21, 2021
21646b1
Move setting up except block out of unwind code into bytecode.
markshannon Apr 21, 2021
9c9575a
Move cleanup code for try-finally and try-except in bytecode.
markshannon Apr 21, 2021
3be89db
Move cleanup code for with and sync with into bytecode.
markshannon Apr 21, 2021
cf664f7
Simplify exit code for async for loops.
markshannon Apr 21, 2021
722b722
Remove SETUP_EXCEPT opcode.
markshannon Apr 21, 2021
caa355f
Remove EXCEPT_HANDLER as a type of exception handling block.
markshannon Apr 21, 2021
4c21ddd
Factor out all block pops and pushes into simple opcodes.
markshannon Apr 21, 2021
6a8d90c
Generate exception handling table
markshannon Apr 22, 2021
9115cea
Parse and verify exception table when unwinding exceptions.
markshannon Apr 23, 2021
350334c
Update test to account for new code object constructor.
markshannon Apr 26, 2021
d6da9cb
Push lasti to stack when cleaning up exception handlers, and use it t…
markshannon Apr 26, 2021
a6b9292
Remove block stack from frames add convert SETUP/POP_BLOCK instructio…
markshannon Apr 26, 2021
9d0a6c1
Make dis understand the exception table.
markshannon Apr 29, 2021
ca482d0
Merge branch 'master' into zero-cost-except
markshannon Apr 29, 2021
4629a97
Set line numbers at start of exception handlers in try-except and try…
markshannon Apr 30, 2021
58b4a5a
Handle exceptions in trace functions correctly.
markshannon Apr 30, 2021
e7a9221
Fix syntax error test.
markshannon Apr 30, 2021
062ffa4
Get frame.setlineno working again.
markshannon Apr 30, 2021
71d95c1
Merge branch 'master' into zero-cost-except
markshannon Apr 30, 2021
e5c2c3e
Remove debugging prints
markshannon Apr 30, 2021
7571e90
Fix test_dis after merge
markshannon Apr 30, 2021
812e78f
Layout frameobject in most compact form.
markshannon Apr 30, 2021
5801089
Add NEWS.
markshannon Apr 30, 2021
8cb6366
Remove SETUP_FINALLY, SETUP_ASYNC_WITH, SETUP_CLEANUP, POP_BLOCK inst…
markshannon Apr 30, 2021
4ec3417
Update dis.rst
markshannon Apr 30, 2021
07cd46d
fix NEWS formatting
markshannon Apr 30, 2021
3b52a3f
Fix typos and clarify text.
markshannon Apr 30, 2021
594a636
Restore dis after discarded experiment.
markshannon Apr 30, 2021
585f306
Bump magic number for 3.11
markshannon Apr 30, 2021
0f11d34
Fix typo
markshannon Apr 30, 2021
63a9b0e
Merge branch 'master' into zero-cost-except
markshannon May 3, 2021
133f74f
Re-order optimization passes to remove more NOPs.
markshannon May 3, 2021
299e051
Update importlib yet again.
markshannon May 3, 2021
43c199d
Assorted clarifications.
markshannon May 6, 2021
630e755
Rename frame.setlineno assistant code to clarify that we now model th…
markshannon May 6, 2021
6333f6c
Revert mistaken change.
markshannon May 6, 2021
e1d6e1e
Update Include/opcode.h
markshannon May 6, 2021
73b600a
Extend notes on exception handling.
markshannon May 6, 2021
f263ee2
Fix refleaks
markshannon May 6, 2021
2f31c84
Merge branch 'main' into zero-cost-except
markshannon May 7, 2021
0c3fd95
Document removal frame push/pop C-API functions.
markshannon May 7, 2021
b92ada2
Update importlib
markshannon May 7, 2021
1878117
Clarify exception handling notes
markshannon May 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extend notes on exception handling.
  • Loading branch information
markshannon committed May 6, 2021
commit 73b600acaec136887157cc88863f9ae9ea906409
178 changes: 178 additions & 0 deletions Objects/exception_handling_notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
Description of exception handling in Python 3.11
------------------------------------------------

Prior to 3.11, exceptions were handled by a runtime stack of "blocks".
Python 3.11 uses what is known as "zero-cost" exception handling.

In zero-cost exception handling, the cost of code that does not raise exceptions
is minimized, but the cost of raising an exception is increased.
Since exceptions are relatively rare this results in improved performance.

The following code:

def f():
try:
g(0)
except:
return "fail"

compiles as follows in 3.10:

2 0 SETUP_FINALLY 7 (to 16)

3 2 LOAD_GLOBAL 0 (g)
4 LOAD_CONST 1 (0)
6 CALL_FUNCTION 1
8 POP_TOP
10 POP_BLOCK
12 LOAD_CONST 0 (None)
14 RETURN_VALUE

4 >> 16 POP_TOP
18 POP_TOP
20 POP_TOP

5 22 POP_EXCEPT
24 LOAD_CONST 3 ('fail')
26 RETURN_VALUE

Note the explicit instructions to push and pop from the "block" stack:
SETUP_FINALLY and POP_BLOCK.

In 3.11, the SETUP_FINALLY and POP_BLOCK are eliminated, replaced with
a table to determine where to jump to when an exception is raised.

2 0 NOP

3 2 LOAD_GLOBAL 0 (g)
4 LOAD_CONST 1 (0)
6 CALL_FUNCTION 1
8 POP_TOP
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
>> 14 PUSH_EXC_INFO

4 16 POP_TOP
18 POP_TOP
20 POP_TOP

5 22 POP_EXCEPT
24 LOAD_CONST 2 ('fail')
26 RETURN_VALUE
>> 28 POP_EXCEPT_AND_RERAISE
ExceptionTable:
2 to 8 -> 14 [0]
14 to 20 -> 28 [3] lasti

(Note this code is from an early 3.11 alpha, the NOP may well have be removed before release).

If an instruction raises an exception then its offset is used to find the target to jump to.
For example, the CALL_FUNCTION at offset 6, falls into the range 2 to 8.
So, if g() raises an exception, then control jumps to offset 14,
once the exception has been pushed to the stack.


Unwinding
---------

When an exception is raised, the current instruction offset is used to find following:
target to jump to, stack depth, and 'lasti', which determines the instruction offset should be pushed.

This information is stored in the exception table, described below.

If there is no relevant entry, the exception bubbles up to the caller.

If there is an entry, then:
1. pop values from the stack until it matches the stack depth for the handler,
2. if 'lasti' is true, then push the offset that the exception was raised at.
3. push the exception to the stack as three values: traceback, value, type,
4. jump to the target offset and resume execution.


Format of the exception table
-----------------------------

Conceptually, the exception table consists of a sequence of 5-tuples:
1. start-offset (inclusive)
2. end-offset (exclusive)
3. target
4. stack-depth
5. push-lasti (boolean)

All offsets and lengths are in instructions, not bytes.

We want the format to be compact, but quickly searchable.
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.
For it to be searchable quickly, we need to support binary search giving us log(n) performance in all cases.
Binary search typically assumes fixed size entries, but that is not necesary, as long as we can identify the start of an entry.

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

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

So, we need to encode:
start (up to 30 bits)
size (up to 30 bits)
target (up to 30 bits)
depth (up to ~8 bits)
lasti (1 bit)

We need a marker for the start of the entry, so the first byte of entry will have the most significant bit set.
Since the most significant bit is reserved for marking the start of an entry, we have 7 bits per byte to encode offsets.
Encoding uses a standard varint encoding, but with only 7 bits instead of the usual 8.
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.

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

For example, the exception entry:
start: 20
end: 28
target: 100
depth: 3
lasti: False

is encoded first by converting to the more compact four value form:
start: 20
size: 8
target: 100
depth<<1+lasti: 6

which is then encoded as:
148 (MSB + 20 for start)
8 (size)
65 (Extend bit + 1)
36 (Remainder of target, 100 == (1<<6)+36)
6

for a total of five bytes.



Script to parse the exception table
-----------------------------------

def parse_varint(iterator):
b = next(iterator)
val = b & 63
while b&64:
val <<= 6
b = next(iterator)
val |= b&63
return val

def parse_exception_table(code):
iterator = iter(code.co_exceptiontable)
try:
while True:
start = parse_varint(iterator)*2
length = parse_varint(iterator)*2
end = start + length - 2 # Present as inclusive, not exclusive
target = parse_varint(iterator)*2
dl = parse_varint(iterator)
depth = dl >> 1
lasti = bool(dl&1)
yield start, end, target, depth, lasti
except StopIteration:
return
86 changes: 0 additions & 86 deletions Objects/exception_table_notes.txt

This file was deleted.