8000 gh-119786: move exception handling doc to InternalDocs by iritkatriel · Pull Request #119815 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119786: move exception handling doc to InternalDocs #119815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 3, 2024
Prev Previous commit
Next Next commit
formatting
  • Loading branch information
iritkatriel committed May 31, 2024
commit 3436cdf7d7ee0f003fa22cde38620ecfc6e0390b
36 changes: 20 additions & 16 deletions InternalDocs/exception_handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ The cost of raising an exception is increased, but not by much.

The following code:

<code>
```
try:
g(0)
except:
res = "fail"

</code>
```

compiles into pseudo-code like the following:

```
`RESUME` 0
RESUME 0

1 `SETUP_FINALLY` 8 (to L1)
1 SETUP_FINALLY 8 (to L1)

2 `LOAD_NAME` 0 (g)
`PUSH_NULL`
`LOAD_CONST` 0 (0)
`CALL` 1
`POP_TOP`
`POP_BLOCK`
2 LOAD_NAME 0 (g)
PUSH_NULL
LOAD_CONST 0 (0)
CALL 1
POP_TOP
POP_BLOCK

-- L1: `PUSH_EXC_INFO`
-- L1: PUSH_EXC_INFO

3 `POP_TOP`
3 POP_TOP

4 `LOAD_CONST` 1 ('fail')
`STORE_NAME` 1 (res)
4 LOAD_CONST 1 ('fail')
STORE_NAME 1 (res)
```

The `SETUP_FINALLY` instruction specifies that henceforth, exceptions
Expand Down Expand Up @@ -91,8 +91,8 @@ of the following steps:
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`
Expand All @@ -108,7 +108,7 @@ For it to be searchable quickly, we need to support binary search giving us log(
Binary search typically assumes fixed size entries, but that is not necessary, 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`
`start, size, target, depth, push-lasti`.

Also, sizes are limited to 2**30 as the code length cannot exceed 2**31 and each code unit takes 2 bytes.
It also happens that depth is generally quite small.
Expand All @@ -128,17 +128,21 @@ The 8 bits of a byte are (msb left) SXdddddd where S is the start bit. X is the
In addition, we 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)
Expand Down
0