[issue44055] NamedTemporaryFile opened twice on Windows

Eryk Sun report at bugs.python.org
Thu May 6 13:06:35 EDT 2021


Eryk Sun <eryksun at gmail.com> added the comment:

Your example uses delete=False. In Windows, the provision about reopening the file while it's open applies to delete=True. With the latter, the file is opened with the O_TEMPORARY flag. At the OS level, this flag modifies the CreateFileW() call as follows:

     dwDesiredAccess |= DELETE; 
     dwShareMode |= FILE_SHARE_DELETE; 
     dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;

Because the open has delete access, which it shares, it can be opened again only if the open shares delete access. An open that doesn't share delete access will fail with a sharing violation. It can be reopened with os.open() with the O_TEMPORARY flag, since this shares delete access. But Python's builtin open() does not share delete access, and neither do most other programs with which one might want to reopen the file. 

This behavior is limiting to the point of making NamedTemporaryFile() practically useless in Windows with delete=True. There is an ongoing discussion about redesigning NamedTemporaryFile() to never use O_TEMPORARY in Windows.

----------
nosy: +eryksun
type:  -> enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44055>
_______________________________________


More information about the Python-bugs-list mailing list