[python-win32] file descriptor issues using msvcrt.open_osfhandle()+os.fdopen()
Scott Leerssen
sleerssen at gmail.com
Tue Dec 20 04:38:58 CET 2011
On Dec 19, 2011, at 7:38 PM, Amaury Forgeot d'Arc wrote:
> 2011/12/19 Scott Leerssen <sleerssen at gmail.com>
> >>> I just tried that, and after closing the file object and attempting to call win32api.ClosHandle() on the value returned by detach, I get an exception of "error: (6, 'CloseHandle', 'The handle is invalid.')" Here's what the code looks like:
> >>>
> >>> h = win32file.CreateFile(
> >>> fname,
> >>> win32file.GENERIC_READ,
> >>> win32file.FILE_SHARE_READ,
> >>> None,
> >>> win32file.OPEN_EXISTING,
> >>> win32file.FILE_FLAG_BACKUP_SEMANTICS,
> >>> None
> >>> )
> >>> hd = h.Detach()
> >>> fd = msvcrt.open_osfhandle(hd, os.O_RDONLY)
> >>> f = os.fdopen(fd, 'rb')
> >>> f.read()
> >>> f.close()
> >>> h.Close()
> >>> win32file.CloseHandle(hd)
>
> - h was Detach()ed, so h.Close() is a no-op.
> - you should not win32file.CloseHandle(hd), the handle now owned by the file object:
> f.close() called the C function fclose(), which called close(fd) which called CloseHandle.
>
> f.close() is enough here!
Thanks, that's very helpful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20111219/102a0ad8/attachment.html>
More information about the python-win32
mailing list