[capi-sig] KeyboardInterrupt eats my error and then won't be caught

Philip Semanchuk philip at semanchuk.com
Sat Jun 13 23:36:56 CEST 2009


Hi all,
I need help understanding how Python deals with Ctrl-C.

A user has reported a bug in my posix_ipc module. When a Python app is  
waiting to acquire an IPC semaphore, a KeyboardInterrupt seems to (a)  
overwrite my error and (b) refuse to be caught by try/except. Here's a  
sample program that demonstrates the problem when run from the command  
line:

# -----------------------------------
import posix_ipc

sem = posix_ipc.Semaphore(None, posix_ipc.O_CREX)

try:
     sem.acquire()
except:
     print "********* I caught it!"

sem.close()
sem.unlink()
# -----------------------------------

I expected that code to raise a posix_ipc.Error with the text, "The  
wait was interrupted by a signal". Instead a KeyboardInterrupt error  
is propagated up to the interpreter and the process is killed as if  
the try/except wasn't even there.

By adding some debug messages to my C code I can see that sem_wait()  
behaves as advertised: it returns -1 and errno is set to EINTR. In  
response to that, my C function calls PyErr_SetString(pBaseException,  
"The wait was interrupted by a signal") and returns NULL. Somewhere  
along the line, the error I set is getting lost.

If I substitute my sysv_ipc module for posix_ipc (very similar to  
posix_ipc but using Sys V semaphores instead of POSIX), I get the same  
behavior.

I see this w/Python 2.5 under OS X and also w/Python 2.5 under Ubuntu  
8.0.4.

http://semanchuk.com/philip/posix_ipc/
http://semanchuk.com/philip/sysv_ipc/


Any suggestions would be appreciated.

Thanks
Philip






More information about the capi-sig mailing list