[PATCH 1 of 5] port win32.py to using the Python ctypes library

Adrian Buehlmann adrian at cadifra.com
Sun Feb 13 08:20:29 CST 2011


On 2011-02-13 14:53, Patrick Mézard wrote:
> Le 13/02/11 14:07, Adrian Buehlmann a écrit :
>> On 2011-02-13 12:39, Patrick Mézard wrote:
>>> Le 08/02/11 17:52, Adrian Buehlmann a écrit :
>>>> +_SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD)
>>>> +_signal_handler = []
>>>>  
>>>>  def set_signal_handler_win32():
>>>> -    """Register a termination handler for console events including
>>>> +    '''Register a termination handler for console events including
>>>>      CTRL+C. python signal handlers do not work well with socket
>>>>      operations.
>>>> -    """
>>>> +    '''
>>>>      def handler(event):
>>>> -        win32process.ExitProcess(1)
>>>> -    win32api.SetConsoleCtrlHandler(handler)
>>>> +        _kernel32.ExitProcess(1)
>>>> +
>>>> +    if _signal_handler:
>>>> +        return # already registered
>>>
>>> (This behaviour change could have gone in another patch too, unless the ctypes version does behave differently than the pywin32 one.)
>>
>> What behavior change?
> 
> The part about not-registering the handler more than once. Perhaps pywin32 was doing it. When reading the code, it just appeared like an additional feature, and I am not sure it is completely necessary given the handler already ExitProcess(). But I may be missing something.

I had to introduce _signal_handler to keep Python from garbage
collecting the _SIGNAL_HANDLER object (which lead to a crash of
python.exe if not done), as Python doesn't know anything about the
additional implicit reference count stemming from the callback function
being registered inside Windows.

So I thought it was convenient to use _signal_handler to avoid
registering another instance of the same handler we already have.

I think it is pointless to register more than one instance of the very
same handler. But I may be missing something.

If you prefer to have that 'if' removed, I'll remove it.


More information about the Mercurial-devel mailing list