[AstroPy] pyfits does not write to gzip ab+ objects

Erik Bray embray at stsci.edu
Thu Aug 29 10:31:55 EDT 2013


On 08/28/2013 04:24 PM, John K. Parejko wrote:
> On 27Aug 2013, at 11:49, Erik Bray wrote:
>
>> I think that the gzip module does not actually support 'ab+' mode, and that if
>> you pass that in it gets confused though it doesn't actually give you any useful
>> errors up front.  Essentially 'ab' is a write-only mode, but 'ab+' is
>> read/write, so when you pass in 'ab+' it opens the underlying file object in a
>> read/write mode but the GzipFile itself is write-only.  In think somewhere this
>> incongruity causes problems...
>
> Except that 'ab+' worked just fine with pyfits 2.4 (I don't know which pyfits version first behaved differently). That's why I suspect it's a pyfits change, not a gzip change.

I see now, you are correct that there's some change in pyfits with respect to 
this.  Actually, it's related to a fix meant to handle inconsistency between 
platforms in the behavior of "a+" mode.  Different OS's and even different 
Python versions handle it differently, so it gets normalized to r+ by default 
(though pyfits still seeks to the end of the file for writing which is what the 
behavior of a+ is *supposed* to provide).

>> In this case if you're just writing a new file 'wb' should suffice too.
>
>
> I'll check up on whether 'ab+' was really necessary for our purposes.

Unless you're appending to an existing file it isn't necessary. Though in that 
case 'rb+' would still suffice for now.  The problem is that when you use ab+ 
the GzipFile object is write-only.  As such PyFITS should not try to do any read 
calls on it.  But its internal bookkeeping is a little messed up by the a+ mode 
normalization and so it ends up thinking the file might be writeable after all. 
  Definitely something to fix.

> John
>
> --
> *************************
> John Parejko
> john.parejko at yale.edu
> http://www.astro.yale.edu/~jp727/
> 203 432-9759
> JWG 465
> Department of Physics
> Yale University
> New Haven, CT
> **************************
>
>
>
>
>
>




More information about the AstroPy mailing list