Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion KeyboardInterrupt eats my error and then won't be caught
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Philip Semanchuk  
View profile  
 More options Jun 18 2009, 11:19 pm
Newsgroups: comp.lang.python
From: Philip Semanchuk <phi...@semanchuk.com>
Date: Thu, 18 Jun 2009 23:19:09 -0400
Local: Thurs, Jun 18 2009 11:19 pm
Subject: KeyboardInterrupt eats my error and then won't be caught
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 and the user hits Ctrl-C, my code  
should return a custom error indicating that the semaphore wait was  
interrupted by a signal.

However, the caller never sees the error I set. Instead they get a  
KeyboardInterrupt that refuses 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()   # User hits Ctrl + C while this is waiting
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" which would then be trapped by the  
except statement which would print the "I caught it!" message.

Instead a KeyboardInterrupt error is propagated up to the interpreter  
and the process is killed as if the try/except wasn't there at all.

I have verified that the C function sem_wait() returns -1 (failure),  
that errno is set to EINTR and that my detects that properly. So far,  
so good. PyErr_Occurred() returns NULL at that point. So my code calls  
PyErr_SetString() to set a custom error for the caller and returns  
NULL. It's apparently at some point after that that the  
KeyboardInterrupt error is being set.

If I substitute my sysv_ipc module for posix_ipc (very similar to  
posix_ipc but uses 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.

If anyone wants to look at my C code, the relevant case statement is  
on line 555 of posix_ipc_module.c.

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

Any suggestions would be appreciated.

Thanks
Philip


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.