Skip to content

Parse and display exception table #564

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

whoami730
Copy link
Contributor

@whoami730 whoami730 commented Jul 15, 2025

Parses and displays Exception Table starting Py 3.11+
This is required for supporting new "zero-cost" exception handling mechanism.

Sample file -

$ cat try_except.py
try:
    x = 1 / 0
except ZeroDivisionError as e:
    print(f"Caught an exception: {e}")
finally:
    print("Finally clause executed")

Disassembly by pycdas for py 3.12 compilation -

$ ./pycdas __pycache__/try_except.cpython-312.pyc
try_except.cpython-312.pyc (Python 3.12)
[Code]
    File Name: try_except.py
    Object Name: <module>
    Qualified Name: <module>
    Arg Count: 0
    Pos Only Arg Count: 0
    KW Only Arg Count: 0
    Stack Size: 5
    Flags: 0x00000000
    [Names]
        'x'
        'ZeroDivisionError'
        'e'
        'print'
    [Locals+Names]
    [Constants]
        1
        0
        'Caught an exception: '
        None
        'Finally clause executed'
    [Disassembly]
        0       RESUME                          0
        2       NOP
        4       LOAD_CONST                      0: 1
        6       LOAD_CONST                      1: 0
        8       BINARY_OP                       11 (/)
        12      STORE_NAME                      0: x
        14      PUSH_NULL
        16      LOAD_NAME                       3: print
        18      LOAD_CONST                      4: 'Finally clause executed'
        20      CALL                            1
        28      POP_TOP
        30      RETURN_CONST                    3: None
        32      PUSH_EXC_INFO
        34      LOAD_NAME                       1: ZeroDivisionError
        36      CHECK_EXC_MATCH
        38      POP_JUMP_IF_FALSE               21 (to 82)
        40      STORE_NAME                      2: e
        42      PUSH_NULL
        44      LOAD_NAME                       3: print
        46      LOAD_CONST                      2: 'Caught an exception: '
        48      LOAD_NAME                       2: e
        50      FORMAT_VALUE                    0 (FVC_NONE)
        52      BUILD_STRING                    2
        54      CALL                            1
        62      POP_TOP
        64      POP_EXCEPT
        66      LOAD_CONST                      3: None
        68      STORE_NAME                      2: e
        70      DELETE_NAME                     2: e
        72      JUMP_BACKWARD                   30 (to 14)
        74      LOAD_CONST                      3: None
        76      STORE_NAME                      2: e
        78      DELETE_NAME                     2: e
        80      RERAISE                         1
        82      RERAISE                         0
        84      COPY                            3
        86      POP_EXCEPT
        88      RERAISE                         1
        90      PUSH_EXC_INFO
        92      PUSH_NULL
        94      LOAD_NAME                       3: print
        96      LOAD_CONST                      4: 'Finally clause executed'
        98      CALL                            1
        106     POP_TOP
        108     RERAISE                         0
        110     COPY                            3
        112     POP_EXCEPT
        114     RERAISE                         1
    [Exception Table]
        4 to 14 -> 32 [0]
        32 to 42 -> 84 [1] lasti
        42 to 64 -> 74 [1] lasti
        64 to 74 -> 90 [0]
        74 to 84 -> 84 [1] lasti
        84 to 90 -> 90 [0]
        90 to 110 -> 110 [1] lasti

@whoami730
Copy link
Contributor Author

whoami730 commented Jul 16, 2025

NOTE - I am trying to look into new "zero-cost" exception handling decompilation support but it's honestly quite hard and we have lots of old code which is harder to understand as well, I am not sure if I will be able to do much but I shall try atleast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant