[Numpy-discussion] fromfile and tofile access with a tempfile.TemporaryFile()

Tim Hirzel hirzel at resonon.com
Tue Dec 12 15:56:08 CST 2006


Good thought Chris,
Normal reading and writing does seem to work. .. 
But, my friend Daniel figured out a workaround when I asked to confirm 
this behavior on his windows setup (and it is does behave the same for 
him).  The first clue was this:

 >>> f = tempfile.TemporaryFile()
 >>> type(f)
<type 'instance'>
 >>> g = open("temp","w+b")
 >>> type(g)
<type 'file'>

so Daniel did a dir(f) and found the 'file' attribute

so if you do (where 'a' is a numpy array)
 >>> a.tofile(f.file)
It works!

writing to the "file" attribute of the TemporaryFile, it works fine!  So 
that's good, but still a little hinky.  Especially since it works on 
linux...
on a linux platform, what does type(tempfile.TemporaryFile()) return?  I 
assume an <type 'instance'>  as well...

anways, so at least there is a quick repair for now.  Good news is, I 
assume using 'f.file' would work on linux too in terms of having a 
single cross-platform solution.


cheers,
tim





Christopher Barker wrote:
> did you try reading and writing to/from that temp file with regular old 
> python functions?
>
> -Chris
>
>
> Tim Hirzel wrote:
>   
>>> I'm running linux and the current svn version of numpy. Maybe the 
>>> problem is with the tempfile module on windows. Do fromfile and tofile 
>>> work for files opened normally?
>>>
>>> Chuck
>>>       
>> fromfile and tofile work fine on regular files.  From skimming the code 
>> a bit, it's hard to imagine numpy code is the culprit, since it must be 
>> getting a NULL pointer back from PyFile_AsFile(file)...  Perhaps this is 
>> a question for a python dev list?  My gut says it's probably something 
>> in the windows tempfile module.  But perhaps in the PyFile_AsFile(file) 
>> implementation.  Seems one of those isn't playing nice.  It's all quite 
>> mysterious to me...
>>
>> tim
>>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>     
>
>   


More information about the Numpy-discussion mailing list