[python-win32] file descriptor issues using msvcrt.open_osfhandle()+os.fdopen()
Amaury Forgeot d'Arc
amauryfa at gmail.com
Tue Dec 20 01:38:47 CET 2011
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!
--
Amaury Forgeot d'Arc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20111220/904c081e/attachment.html>
More information about the python-win32
mailing list