Issue 6875: add os.close() suggestion to mkstemp documentation (original) (raw)

Created on 2009-09-10 11:47 by vincele, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-doc-mkstemp.patch vincele,2009-09-10 11:47 patch adding os.close() suggestion to the mkstemp documentation
Messages (6)
msg92477 - (view) Author: Vincent Legoll (vincele) Date: 2009-09-10 11:47
As per the blog entry http://www.logilab.org/blogentry/17873 I think the tempfile.mkstemp() documentation could be more helpful by suggesting the use of os.close() appropriately. If some native english speaker could give a review of the language I used in the additional text, I'd be grateful.
msg92479 - (view) Author: Vincent Legoll (vincele) Date: 2009-09-10 11:49
Or a review of the markup I used
msg92533 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-09-12 02:46
I don't think it would be beneficial to just talk about closing the file descriptor, or to talk about os open file limits. Closing open files when they are no longer needed is just good programming practice and IMO a discussion of it doesn't belong in the library docs unless it is at a more generic level (like os.open or open). I don't think the code shown in your blog post is using the library the way it is designed. If the programmer wants an open file object, they should use TemporaryFile or NamedTemporaryFile. If there really is a reason to get an os file handle and turn it into a file object, then os.openfd should be used. Perhaps the appropriate change to the documentation would be to mention os.openfd, and to mention that os.close is the correct function to use to close the file handle when it is no longer needed (unless, of course, openfd has been called). As for your markup, your ``os.close`` should instead be :func:`os.close`, which results in the automatic creation of a link to the os.close documentation.
msg92537 - (view) Author: Vincent Legoll (vincele) Date: 2009-09-12 09:22
The real question I had is why mkstemp return an os-level opened file descriptor instead of a python file object...
msg92549 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-09-12 16:49
Because if you want the Python file object, you should use TemporaryFile or NamedTemporaryFile. mkstemp is a lower level (closer to the os) interface, and is provided because Python has a philosophy of providing those low level interfaces when possible. Note that the TemporaryFile classes use mkstemp in their implementation.
msg92684 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-09-16 13:04
That one has to close open files should be common knowledge. And it's already documented that the filehandle returned is to be treated as if coming from `os.open`, so the "isn't a file object" is documented as well. Insofar, I'm in agreement with David.
History
Date User Action Args
2022-04-11 14:56:52 admin set github: 51124
2013-08-13 14:41:21 Gabi.Davar set nosy: + Gabi.Davar
2009-09-16 13:04:02 georg.brandl set status: open -> closedresolution: works for memessages: +
2009-09-12 16:49:43 r.david.murray set messages: +
2009-09-12 09:22:23 vincele set messages: +
2009-09-12 02:46:53 r.david.murray set priority: normalversions: + Python 2.6, Python 3.1, Python 2.7nosy: + r.david.murraymessages: +
2009-09-10 11:49:11 vincele set messages: +
2009-09-10 11:47:03 vincele create