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

Patrick Mézard pmezard at gmail.com
Sun Feb 13 08:40:20 CST 2011


Le 13/02/11 15:20, Adrian Buehlmann a écrit :
> 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.

No that's fine, it was not clear to me if you added this because you spot the multiple handlers while rewriting the code, or as a quasi-side-effect of the garbage collector workaround.

--
Patrick Mézard


More information about the Mercurial-devel mailing list