win32com: Setting ByRef parameters in an event

Mark Hammond MarkH at ActiveState.com
Sat Aug 25 23:03:00 EDT 2001


Paul Moore wrote:

> On Fri, 17 Aug 2001 01:32:46 +0200, Mark Hammond <MarkH at ActiveState.com>
> wrote:
> 
> 
>>>>(And, BTW, if I hit Ctrl-C, I get an error
>>>>"Unhandled exception detected before entering Python." - shouldn't I
>>>>get a KeybordInterrupt error?)
>>>>
>>Well, the Ctrl+C key is being hit _before_ Python code is entered.  In 
>>this case we possibly do _not_ want Python to handle the exception - for 
>>all we know, the object itself has handled the Ctrl+C key, and is 
>>calling back into Python to handle it.
>>
> 
> Hmm, thinking some more about this, the key point is that we don't *know* that
> the object has handled the exception. But if the object does handle the
> exception, surely the Python support code won't see it (it'll have already
> been translated into an OnCtrlC event or whatever, and handled). So the only
> time Python sees the Ctrl-C is when the object *hasn't* handled it - and so
> Python *should* handle it. After all, in the current situation, where the
> object does not handle Ctrl-C, I have no way of either handling it on the next
> event in Python, nor of suppressing the message.

We had this exact problem in the Python XPCOM bindings.

An XPCOM program was running, and the user hit Ctrl+C.  There was no 
explicit handler for this in the program, so the program begun 
termination.  This termination process begun the XPCOM finalization 
process, which itself calls back into Python, so the Python XPCOM 
bindings can be shut down.

If Python honoured this Ctrl+C notification, the cleanup code was not 
executed.

The exact same issue can apply with win32com.  A program sees Ctrl+C, 
and as part of the shutdown process, releases references to existing COM 
objects.  One of these objects may be a Python COM object, and this 
final release of the object may cause the __del__ method on the class to 
be executed.

This is kinda-of "damned if you do, damned if you don't".  I think the 
correct answer is for the program itself to install its own Ctrl+C 
handler - this should be possible from Python itself.

Mark.




More information about the Python-list mailing list