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

Adrian Buehlmann adrian at cadifra.com
Sun Feb 13 07:38:16 CST 2011


(I forgot to respond to this one in my first reply, sorry)

On 2011-02-13 12:39, Patrick Mézard wrote:
> 
> [ ... skipping types definitions ...]
> 
>> +def _raiseoserror(name):
>> +    err = ctypes.WinError()
>> +    raise OSError(err.errno, '%s: %s' % (name, err.strerror))
>> +
>> +def _getfileinfo(name):
>> +    fh = _kernel32.CreateFileA(name,
>> +            0, _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
>> +            None, _OPEN_EXISTING, 0, None)
>> +    if fh == _INVALID_HANDLE_VALUE:
>> +        _raiseoserror(name)
>> +    try:
>> +        fi = _BY_HANDLE_FILE_INFORMATION()
>> +        if not _kernel32.GetFileInformationByHandle(fh, ctypes.byref(fi)):
>> +            _raiseoserror(name)
>> +        return fi
>> +    finally:
>> +        if fh != _INVALID_HANDLE_VALUE:
> 
> Isn't this always True?

Right. That 'if' is pointless (but harmless).

FWIW, calling CloseHandle(-1) is harmless anyway (if we ignore the fact that it
might obscure the GetLastError value).

  $ python
  Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import ctypes
  >>> ctypes.windll.kernel32.CloseHandle(-1)
  0
  >>> ctypes.windll.kernel32.GetLastError()
  6

(6 is ERROR_INVALID_HANDLE)

>> +            _kernel32.CloseHandle(fh)
>>  


More information about the Mercurial-devel mailing list