report at bugs
Jul 1, 2012, 3:46 PM
Post #6 of 8
Richard Oudkerk <shibturn [at] gmail> added the comment:
[issue14243] tempfile.NamedTemporaryFile not particularly useful on Windows
[In reply to]
> Agreed. Richard: do you have time to put something together?
> I'm happy to try if you don't.
I'm looking into it.
Unfortunately, it seems that you need to use non-default flags when reopening a shared file. Eg, if the file is currently opened with SH_DENYNO and O_TEMPORARY, then you must reopen it using SH_DENYNO and O_TEMPORARY.
However, I have an initial implementation of os.sopen() which makes the following work:
import os, tempfile
FNAME = "foo.txt"
DATA = "hello bob"
def opener(name, flag, mode=0o777):
return os.sopen(name, flag | os.O_TEMPORARY, os.SH_DENYNO, mode)
with open(FNAME, "w", opener=opener) as f:
with open(FNAME, "r", opener=opener) as f:
assert f.read() == DATA
assert not os.path.exists(FNAME)
BTW, Maybe it would be better to add a keyword-only shareflag argument to os.open() rather than add os.sopen().
Python tracker <report [at] bugs>
Python-bugs-list mailing list