Thomas Jollans <[email protected]> added the comment:
I think I know what's going on here.
The way you're using tempfile.TemporaryFile() is, shall we say, unusual.
TemporaryFile() already gives you an open file, there's really no reason why
you'd call open() again. In practice you'd usually want to write something like
with tempfile.TemporaryFile() as f:
# do stuff
Back to the issue. Your code boils down to:
fd = tempfile.TemporaryFile()
with open(fd.fileno()) as f:
pass
fd = tempfile.TemporaryFile()
fd.seek(0)
You had fd.name rather than fd.fileno(). On *nix, these are the same for a
TemporaryFile, which has no name. (On Windows, open(fd.name) fails)
What happens? open(fd.fileno()) creates a new file object for the same
low-level file descriptor. At the end of the with block, this file is closed.
This is fine, but the object returned by TemporaryFile doesn't know this
happened.
You then call tempfile.TemporaryFile() again, which opens a new file. The OS
uses the next available file descriptor, which happens to be the one you just
closed. THEN, the old TemporaryFile object gets deleted. It doesn't know you
already closed its file, so it calls close(). On the FD that has just been
reused.
This has nothing to do with reusing the same name, it's just about what order
things happen in. This achieves the same effect:
tmp1 = tempfile.TemporaryFile()
os.close(tmp1.fileno())
tmp2 = tempfile.TemporaryFile()
del tmp1
tmp2.seek(0)
Deleting the first file object before creating the second one (like in your
test_5) solves this problem. I'm not sure why your test_6 works.
As for why id(fd.name) was the same for you? It's because fd.name is just
fd.fileno() in this case, which is an integer.
TL;DR:
- having two file objects for the same file descriptor is asking for trouble
- I'd say "not a bug"
----------
nosy: +tjollans
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44013>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com