[Python-bugs-list] [ python-Bugs-563750 ] os.tmpfile should use w+b, not w+

noreply@sourceforge.net noreply@sourceforge.net
Mon, 10 Jun 2002 12:23:50 -0700


Bugs item #563750, was opened at 2002-06-03 01:59
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=563750&group_id=5470

Category: Python Library
Group: Platform-specific
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Alex Martelli (aleax)
Assigned to: Nobody/Anonymous (nobody)
Summary: os.tmpfile should use w+b, not w+

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-06-10 15:23

Message:
Logged In: YES 
user_id=6380

Thanks! Fixed, and marked as bugfix candidate.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=563750&group_id=5470