[ python-Bugs-1615275 ] tempile.TemporaryFile differences between linux and windows

SourceForge.net noreply at sourceforge.net
Mon Apr 9 16:16:23 CEST 2007


Bugs item #1615275, was opened at 2006-12-13 16:20
Message generated for change (Comment added) made by draghuram
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1615275&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: Platform-specific
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: hirzel (hirzel)
Assigned to: Nobody/Anonymous (nobody)
Summary: tempile.TemporaryFile differences between linux and windows

Initial Comment:
This bug came up when trying to write a numpy array to a tempfile.TemporaryFile() using the numpy 'tofile' method on windows using python 2.4.  

with a numpy array 'a', and a TemporaryFile 'f', 
on windows:
>>> a.tofile(f)

throws an IOError, where on Linux it does not.
On windows, you must use a.tofile(f.file)

The cause of this difference is that in windows, tempfile.TemporaryFile() returns <type 'instance'> that has a 'file'  attribute of <type 'file'>, whereas in linux tempfile.TemporaryFile() returns <type 'file'> and there is no 'file' attribute.  

Ideally, the windows version would align with linux, and the module documentation and TemporaryFile() would return a <type 'file'>.  If this is not possible, it seems like the linux version and docs should be changed to match the windows version to align cross-platform behavior.  At least, that seems to be the shared opinion of this thread from the mailing list: numpy-discussion.  http://www.mail-archive.com/numpy-discussion@scipy.org/msg00271.html

To my knowledge, while platform differences in tempfile have been reported in the past, this one has not.



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

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-04-09 10:16

Message:
Logged In: YES 
user_id=984087
Originator: NO


I posted in python-dev about this bug asking if TemporaryFile() can also
return a wrapper on all platforms but immediately realized that always
returning a wrapper instead of a file object would break any existing code
that depends on the return value being a file object. There were no other
comments from any one on the dev list either. I think the best bet is to
update the document with the following details.

1) "NamedTemporaryFile" returns a file-like object. The actual file object
can be accessed with "file" member of the returned instance.

2) "TemporaryFile" on windows is same as "NamedTemporaryFile".

3) cross-platform code that needs access to file object (such as
array.tofile) should check for the return object type and behave
accordingly.


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

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-03-29 14:36

Message:
Logged In: YES 
user_id=984087
Originator: NO


After looking at tempfile.py, the reason for the difference in behaviour
is clear. On windows, "TemporaryFile" is an alias for "NamedTemporaryFile".
NamedTemporaryFile() returns a wrapper instance with file-like interface
but which is not actually a file. This is not a problem when file
operations like "write" and "close" are used directly. But array.tofile()
explicitly checks for file type object and hence fails with
NamedTemporaryFile(). Same is the reason for numpy failure as reported by
OP (I haven't explicitly analyzed numpy failure but gleaned this info from
the discussion thread in the initial post). 

Even though the reason is clear, I think the end result is a bit
unsatisfactory. array.tofile() (and numpy's tofile()) need to pass
different parameters depending on the platform. One possible solution is
for "TemporaryFile" to return a wrapper as well. Then, tofile() can be
called with TemporaryFile().file on all platforms. 






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

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-03-28 10:43

Message:
Logged In: YES 
user_id=984087
Originator: NO


I used the following code to reproduce the problem on windows XP.

----------
import array
import tempfile

testarray = array.array('B')
testarray.fromstring("\x00\x00\x00")
f = tempfile.TemporaryFile()
testarray.tofile(f)
-----------

This works fine on linux but on windows, it gives the following error:

-------------
Traceback (most recent call last):
  File "c:\rags\tofile.py", line 7, in <module>
    testarray.tofile(f)
TypeError: arg must be open file
-------------

Changing "f" to "f.file" seems to work, though, as explained in the
initial post. So this may be the same problem as OP reported even though I
am getting TypeError and he mentioned IOError. 

I tested with 2.4 and 2.5 as I don't know how to set up python development
environment on windows (yet). I will see if I can set that up first before
working on the "fix". 

Raghu.





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

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


More information about the Python-bugs-list mailing list