Chapter 24: Error Handling

Example tsignal.c, Page 634

In [2]:
import signal
import sys
def handler(sig,frame):
    print "Handler called for signal",sig
print "Installing handler for signal", signal.SIGINT
orig_handler=signal.signal(signal.SIGINT,handler)
print "Changing handler to SIG_IGN"
signal.signal(signal.SIGINT,signal.SIG_IGN)
print "Restoring original handler"
signal.signal(signal.SIGINT,orig_handler)
print "Program terminates normally"
Installing handler for signal 2
Changing handler to SIG_IGN
Restoring original handler
Program terminates normally

Example tsetjmp.c, Page 636

In [18]:
def main():
    def f1():
        print "f1 begins"
        f2()
        return
        print "f1 returns"

    def f2():
        print "f2 begins"
        #longjmp(env, 1)
        setjmp=1
        print "Program terminates: longjmp called"
        return
        print "f2 returns"

    setjmp=0
    if (setjmp == 0):
        print "setjmp returned 0"
    else:
        print "Program terminates: longjmp called"
    f1()
    return
    print "Program terminates normally"
    
if  __name__=='__main__':
    main()
setjmp returned 0
f1 begins
f2 begins
Program terminates: longjmp called
In [ ]: