This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients benhoyt, docs@python, eryksun, martin.panter, paul.moore, remyroy, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Date 2021-02-24.23:12:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1614208356.41.0.373312437798.issue26111@roundup.psfhosted.org>
In-reply-to
Content
Issue 25994 added support for the context-manager protocol and close() method in 3.6. So it's at least much easier to ensure that the handle gets closed. 

The documentation of scandir() links to WinAPI FindFirstFile and FindNextFile, which at least mentions the "search handle". It's not made explicit that this encapsulates a handle for a kernel file object, nor are the operations (e.g. move, rename, delete) discussed that are allowed directly on the directory. Similarly, the directory stream that's returned by and used by POSIX opendir() and readdir() may or may not encapsulate a file descriptor. 

I don't think Python's documentation is the best place to discuss platform-specific implementation details in most cases. Exceptions should be made in some cases, but I don't think this is one of them because I can't even link to a document about the implementation details of FindNextFile. At a lower level I can link to documents about the NtQueryDirectoryFile[Ex] system call, but that's not much help in terms of officially documenting what FindNextFile does. Microsoft prefers to keep the Windows API details opaque, which gives them wiggle room.

FYI, in Windows 10, deleting files and directories now tries a POSIX delete (if supported by the filesystem) that immediately unlinks the name as soon as the handle that's used to perform the delete is closed, such as the handle that's opened to implement DeleteFile (os.unlink) and RemoveDirectory (os.rmdir). NTFS supports this feature by moving the file/directory to a reserved "\$Extend\$Deleted" directory:

    >>> os.mkdir('spam')
    >>> h = win32file.CreateFile('spam', 0, 0, None, 3, 0x0200_0000, None)
    >>> print(win32file.GetFinalPathNameByHandle(h, 0))
    \\?\C:\Temp\test\test\spam

    >>> os.rmdir('spam')
    >>> print(win32file.GetFinalPathNameByHandle(h, 0))
    \\?\C:\$Extend\$Deleted\001000000000949A5E2FE5BB

Of course, none of the above is documented for RemoveDirectory().
History
Date User Action Args
2021-02-24 23:12:36eryksunsetrecipients: + eryksun, paul.moore, tim.golden, benhoyt, docs@python, martin.panter, zach.ware, serhiy.storchaka, steve.dower, remyroy
2021-02-24 23:12:36eryksunsetmessageid: <1614208356.41.0.373312437798.issue26111@roundup.psfhosted.org>
2021-02-24 23:12:36eryksunlinkissue26111 messages
2021-02-24 23:12:36eryksuncreate