How are exceptions actually implemented in assembly?

Steven Majewski sdm7g at Virginia.EDU
Wed Jan 23 18:18:35 CET 2002

On 23 Jan 2002, Hung Jung Lu wrote:

> So, how is C (hence Python) exception handling actually implemented?

C doesn't really have exceptions in the language, but the equivalent
functionality is usually coded in C using setjmp & longjmp (which are
sort of global goto's -- setjmp saves a continuation and longjmp
jumps to the last saved continuation, restoring registers and unwinding
the stack. ). setjmp/longjmp are often used with signals, which are
asynchronous notifications that are sort of the software equivalent
of hardware interrupts.

P.J. Plauger's "The Standard C Library" has a good explaination.

However, the Python interpreter doesn't doesn't use that mechanism.
Look at Python/ceval.c, which contains the main interpreter loop,
and you'll see that its just a local goto out of the interpreter
loop. The Python interpreter has to unwind the Python stack.
(And C extensions have to propagate the error codes and return.)

Look at the section in the Python/C API about exception handling:

And read Python/ceval.c and pay attention to all of the 'WHY' codes.

-- Steve Majewski

More information about the Python-list mailing list