[Python-bugs-list] [ python-Bugs-439992 ] [win32] KeyboardInterrupt Not Caught

noreply@sourceforge.net noreply@sourceforge.net
Sat, 13 Jul 2002 18:25:07 -0700


Bugs item #439992, was opened at 2001-07-10 19:24
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=439992&group_id=5470

Category: Python Interpreter Core
Group: Platform-specific
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
>Assigned to: Tim Peters (tim_one)
Summary: [win32] KeyboardInterrupt Not Caught

Initial Comment:
The following program, run under unix (FreeBSD 4.3) and Windows 2000 SP2
with the command line:  python test.py

In both cases, running python 2.1.

The program works as expected under unix and IDLE on Windows 2000, but it
does *not* catch the interrupt in the command line version of Win32 python...
i.e., The traceback message appears rather than my "Leaving Dodge..." message.


while 1:
    try:
        x = raw_input().upper()
        print x
    except KeyboardInterrupt:
        print "Leaving Dodge...\n"
        break

----------------------------------------------------------------------

>Comment By: Mark Hammond (mhammond)
Date: 2002-07-14 11:25

Message:
Logged In: YES 
user_id=14198

This is an interesting one :)

When Python is inside an fgets() and a Ctrl+C is hit, we see
two things:
* fgets() returns immediately with an "eof" condition. 
There is no errno set to indicate the interrupt like there
is on *nix.
* The signal handler is called asynchronously on another thread.

So what the user sees is:
* The EOF flag causes Python to raise an EOFError when
Ctrl+C is pressed.
* When the signal is finally triggered, Python sees
*another* exception, this time a keyboard interrupt.

For example, the following code:
import time, sys
while 1:
    try:
        x = raw_input().upper()
        print x
    except KeyboardInterrupt:
        print "Done"
        break
    except EOFError:
        print "EOF"
        break

will *usually* print "EOF" when Ctrl+C is pressed.

I have determined that when Ctrl+C is pressed, the Win32
function GetLastError() returns ERROR_OPERATION_ABORTED.  By
using this undocumented feature, I have created a patch that
makes Ctrl+C work as it does on *nix.

One side effect is that Ctrl+C in an interactive Python
session no longer terminates the process - just like *nix,
it now prints "KeyboardInterrupt" then returns to the
interactive prompt.

Tim - what say you on this?

----------------------------------------------------------------------

Comment By: Skip Montanaro (montanaro)
Date: 2002-03-10 12:41

Message:
Logged In: YES 
user_id=44345

just a note to remind folks that GvR suggested closing
this as "won't fix" a few months ago.  Seems like it's
still the correct course unless MS has changed Windows
dramtically in the intervening interval. -skip


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-12-12 17:08

Message:
Logged In: YES 
user_id=31435

Umm, I have no idea whether it can be fixed.  Who was this 
assigned to?  Assigning to MarkH in case he has an idea.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-12-12 16:34

Message:
Logged In: YES 
user_id=6380

Shall we close this as Won't Fix? Is there a point in
keeping it open while we know we can't fix it?

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-08-08 01:51

Message:
Logged In: YES 
user_id=31435

For posterity, a delightfully confused section of 
MS's "signal" docs:

"""
Note SIGINT is not supported for any Win32 application 
including Windows NT and Windows 95. When a CTRL+C 
interrupt occurs, Win32 operating systems generate a new 
thread to specifically handle that interrupt. This can 
cause a single-thread application such as UNIX, to become 
multithreaded, resulting in unexpected behavior. 
"""

I kid you not.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-07 23:59

Message:
Logged In: YES 
user_id=6380

Yes, I'm afraid signal handling doesn't always work on
Windows.
I don't know Windows enough to understand how to fix this.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=439992&group_id=5470