KeyboardInterrupt not caught

Discussion in 'Python' started by ruka_at_@fastmail.fm, Feb 16, 2007.

  1. Guest

    Hi,
    why is KeyboardInterrupt not caught (xp)?
    import sys
    try:
    inp = sys.stdin.read()
    except (KeyboardInterrupt, SystemExit):
    print "kbd-interr,SystemExit"
    except EOFError:
    print "eof encountered"
    except:
    print "caught all"
    self.showtraceback()
    print "normal end"

    result after script startet and ^C hit:
    >ctrl_test.py

    normal end
    Traceback (most recent call last):
    File "C:\work\py_src\ctrl_test.py", line 11, in ?
    print "normal end"
    KeyboardInterrupt

    br Rudi
     
    , Feb 16, 2007
    #1
    1. Advertising

  2. > why is KeyboardInterrupt not caught (xp)?
    > import sys
    > try:
    > inp = sys.stdin.read()
    > except (KeyboardInterrupt, SystemExit):
    > print "kbd-interr,SystemExit"
    > except EOFError:
    > print "eof encountered"
    > except:
    > print "caught all"
    > self.showtraceback()
    > print "normal end"
    >
    > result after script startet and ^C hit:
    > >ctrl_test.py

    > normal end
    > Traceback (most recent call last):
    > File "C:\work\py_src\ctrl_test.py", line 11, in ?
    > print "normal end"
    > KeyboardInterrupt


    Hi, are you sure this is exactly what you run?
    The code above works perfectly for me and prints

    kbd-interr,SystemExit
    normal end

    as it should upon pressing Ctrl-C (although I'm on linux not xp but
    that shouldn't matter at all).
     
    Daniel Nogradi, Feb 16, 2007
    #2
    1. Advertising

  3. En Fri, 16 Feb 2007 06:58:54 -0300, Daniel Nogradi <>
    escribió:

    >> why is KeyboardInterrupt not caught (xp)?

    >
    > Hi, are you sure this is exactly what you run?
    > The code above works perfectly for me and prints
    >
    > kbd-interr,SystemExit
    > normal end
    >
    > as it should upon pressing Ctrl-C (although I'm on linux not xp but
    > that shouldn't matter at all).


    I'm using XP and it works as expected too.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 16, 2007
    #3
  4. On Fri, 16 Feb 2007 01:47:43 -0800, ruka_at_ wrote:

    > Hi,
    > why is KeyboardInterrupt not caught (xp)?
    > import sys
    > try:
    > inp = sys.stdin.read()
    > except (KeyboardInterrupt, SystemExit):
    > print "kbd-interr,SystemExit"
    > except EOFError:
    > print "eof encountered"


    I don't think you ever get an EOFError from stdin. If you type ^D
    immediately, stdin.read() returns an empty string.


    > except:
    > print "caught all"
    > self.showtraceback()


    I don't imagine you'll get any other exceptions either.

    Not that it matters, but what's self?



    > print "normal end"
    >
    > result after script startet and ^C hit:
    >>ctrl_test.py

    > normal end
    > Traceback (most recent call last):
    > File "C:\work\py_src\ctrl_test.py", line 11, in ?
    > print "normal end"
    > KeyboardInterrupt


    It works as expected for me.

    I seem to have a vague recollection that the keyboard interrupt under
    Windows isn't ^C but something else... ^Z maybe?



    --
    Steven.
     
    Steven D'Aprano, Feb 16, 2007
    #4
  5. En Fri, 16 Feb 2007 07:26:09 -0300, Steven D'Aprano
    <> escribió:

    > I seem to have a vague recollection that the keyboard interrupt under
    > Windows isn't ^C but something else... ^Z maybe?


    Ctrl-C is the keyboard interrupt, Ctrl-Z means EOF.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 16, 2007
    #5
  6. Guest

    On 16 Feb., 11:44, "Gabriel Genellina" <> wrote:

    Thanks to all of you, for the fast answers.
    The code I showed you is actually the code running. I tried to catch
    eof, cause I read ^C could produce EOF (the self.showtraceback() was
    just a stupid cut 'n paste). But not even the except that should catch
    all exceptions is triggered.
    Thanks again,
    br Rudi
     
    , Feb 16, 2007
    #6
  7. Guest

    On 16 Feb., 12:16, wrote:
    > On 16 Feb., 11:44, "Gabriel Genellina" <> wrote:
    >
    > I've tried it in cygwin, result:

    $ python.exe c:/work/py_src/ctrl_test.py
    kbd-interr,SystemExit
    normal end
    br Rudi
     
    , Feb 16, 2007
    #7
  8. On 16 Feb 2007 01:47:43 -0800, declaimed the
    following in comp.lang.python:

    > Hi,
    > why is KeyboardInterrupt not caught (xp)?


    Using a variation of your sample

    -=-=-=-=-
    import time
    import sys
    inp = None
    try:
    time.sleep(10) #########
    inp = sys.stdin.read()
    except (KeyboardInterrupt, SystemExit):
    print "kbd-interr,SystemExit"
    except EOFError:
    print "eof encountered"
    except:
    print "caught all"
    print repr(inp)
    print "normal end"
    -=-=-=-=-=-=-

    results in the following -- the first one is when I hit the <ctrl-c>
    immediately after entering the command line; the other is when I waited
    the 10+ seconds so the read became active.

    E:\UserData\DENNIS~1\MYDOCU~1>python Script1.py
    kbd-interr,SystemExit
    None
    normal end

    E:\UserData\DENNIS~1\MYDOCU~1>python Script1.py
    ''
    normal end
    ^C
    E:\UserData\DENNIS~1\MYDOCU~1>python Script1.py
    ^Z
    ''
    normal end

    E:\UserData\DENNIS~1\MYDOCU~1>

    Off hand, it appears that a pending read of stdin will absorb all
    characters; keyboard interrupt only occurs when the OS itself gets the
    <ctrl-c> during some blocking call OTHER THAN a read of stdin -- ie, the
    time.sleep()


    > import sys
    > try:
    > inp = sys.stdin.read()
    > except (KeyboardInterrupt, SystemExit):
    > print "kbd-interr,SystemExit"
    > except EOFError:
    > print "eof encountered"
    > except:
    > print "caught all"
    > self.showtraceback()
    > print "normal end"
    >
    > result after script startet and ^C hit:
    > >ctrl_test.py

    > normal end
    > Traceback (most recent call last):
    > File "C:\work\py_src\ctrl_test.py", line 11, in ?
    > print "normal end"
    > KeyboardInterrupt
    >
    > br Rudi

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 16, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Holden

    ASP KeyboardInterrupt errors

    Steve Holden, Jun 24, 2003, in forum: Python
    Replies:
    2
    Views:
    728
    Mark Hammond
    Jun 28, 2003
  2. Michael Goerz
    Replies:
    1
    Views:
    1,002
    Donn Cave
    Feb 22, 2008
  3. Replies:
    1
    Views:
    282
    Rhamphoryncus
    May 7, 2008
  4. Igor Katson
    Replies:
    8
    Views:
    1,150
    Igor Katson
    May 17, 2009
  5. Philip Semanchuk
    Replies:
    6
    Views:
    382
    Philip Semanchuk
    Jun 21, 2009
Loading...

Share This Page