{
 "metadata": {
  "name": "",
  "signature": "sha256:70182e1ed50aea78649f1bef2946ce2b6e335126b84963ba06fce0660ac72886"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 24: Error Handling"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example tsignal.c, Page 634"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import signal\n",
      "import sys\n",
      "def handler(sig,frame):\n",
      "    print \"Handler called for signal\",sig\n",
      "print \"Installing handler for signal\", signal.SIGINT\n",
      "orig_handler=signal.signal(signal.SIGINT,handler)\n",
      "print \"Changing handler to SIG_IGN\"\n",
      "signal.signal(signal.SIGINT,signal.SIG_IGN)\n",
      "print \"Restoring original handler\"\n",
      "signal.signal(signal.SIGINT,orig_handler)\n",
      "print \"Program terminates normally\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Installing handler for signal 2\n",
        "Changing handler to SIG_IGN\n",
        "Restoring original handler\n",
        "Program terminates normally\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example tsetjmp.c, Page 636"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    def f1():\n",
      "        print \"f1 begins\"\n",
      "        f2()\n",
      "        return\n",
      "        print \"f1 returns\"\n",
      "\n",
      "    def f2():\n",
      "        print \"f2 begins\"\n",
      "        #longjmp(env, 1)\n",
      "        setjmp=1\n",
      "        print \"Program terminates: longjmp called\"\n",
      "        return\n",
      "        print \"f2 returns\"\n",
      "\n",
      "    setjmp=0\n",
      "    if (setjmp == 0):\n",
      "        print \"setjmp returned 0\"\n",
      "    else:\n",
      "        print \"Program terminates: longjmp called\"\n",
      "    f1()\n",
      "    return\n",
      "    print \"Program terminates normally\"\n",
      "    \n",
      "if  __name__=='__main__':\n",
      "    main()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "setjmp returned 0\n",
        "f1 begins\n",
        "f2 begins\n",
        "Program terminates: longjmp called\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}