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.

classification
Title: os.tmpfile should use w+b, not w+
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aleax, gvanrossum
Priority: normal Keywords:

Created on 2002-06-03 05:59 by aleax, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (2)
msg11039 - (view) Author: Alex Martelli (aleax) * (Python committer) Date: 2002-06-03 05:59
os.tmpfile is only present on Unix and Windows, and on 
Unix it makes no difference.  On Windows, Microsoft's
docs indicate tmpfile uses mode w+b -- which makes a lot 
of sense as it lets you shunt temporarily to disk binary 
stuff without any ill effect for text stuff.  Unfortunately the 
implementation of os.tmpfile in Modules/posixmodule.c 
calls PyFile_FromFile with 'w+', not 'w+b', so that's what 
gets recorded as the mode attribute of the file object.

So, a careful function which receives a file object 
argument
and checks that its mode contains a 'b' (because it needs
to to binary I/O on the file) will diagnose inexistent 
problems
when it's called with an argument that's a file object 
returned
from os.tmpfile().

Suggested fix: change 'w+' to 'w+b' in posixmodule.c and
in the docs for module os, function tmpfile in each case.

This could only possibly break carelessly coded functions
that check the mode attribute and require it to be identical
to 'w+' when they actually can work perfectly well with
'w+b' (because w+b is what they actually GET, though it
falsely claims to be w+).  I think this hypothetical 
breakage
(I know of no such function, and anybody careful enough 
to
check the mode is likely also careful enough to do the 
RIGHT check) is quite tolerable in exchange for having 
the
os.tmpfile function reflect reality and let careful well-coded
functions work naturally and correctly.
msg11040 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-10 19:23
Logged In: YES 
user_id=6380

Thanks! Fixed, and marked as bugfix candidate.
History
Date User Action Args
2022-04-10 16:05:23adminsetgithub: 36691
2002-06-03 05:59:27aleaxcreate