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: tempfile.TemporaryFile differences between linux and windows
Type: Stage:
Components: Documentation, Extension Modules Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, draghuram, georg.brandl, hirzel
Priority: normal Keywords:

Created on 2006-12-13 21:20 by hirzel, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg30804 - (view) Author: hirzel (hirzel) Date: 2006-12-13 21:20
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.

msg30805 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-03-28 14:43

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.



msg30806 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-03-29 18:36

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. 




msg30807 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-04-09 14:16

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.
msg59328 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-05 20:10
The behavior should be documented
msg59389 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-06 15:55
Documented in r59776.
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44336
2008-01-06 15:55:42georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg59389
nosy: + georg.brandl
title: tempile.TemporaryFile differences between linux and windows -> tempfile.TemporaryFile differences between linux and windows
2008-01-05 20:10:38christian.heimessetnosy: + christian.heimes
messages: + msg59328
components: + Documentation
versions: + Python 2.6, Python 2.5
2006-12-13 21:20:55hirzelcreate