classification
Title: 'GzipFile' object has no attribute 'extrastart'
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Laurent.Gautier, gkcn, nadeem.vawda, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2013-06-28 11:26 by Laurent.Gautier, last changed 2018-07-11 07:40 by serhiy.storchaka.

Messages (5)
msg191989 - (view) Author: Laurent Gautier (Laurent.Gautier) Date: 2013-06-28 11:26
When creating a `gzip.GzipFile` using the named parameter `fileobj`
rather than filename, iterating over it is broken:
```
AttributeError: 'GzipFile' object has no attribute 'extrastart'
```

The short example below is demonstrating the problem:

## ---

import gzip, tempfile
_data = (b'@WXOVW:25:85', b'ATACGCGGCT'+b'GATCGTAGCG',
         b'+',
         b'@@))CCCCBB'+b'???ECCEECC')

data = gzip.zlib.compress(b'\n'.join(_data))
foo = tempfile.NamedTemporaryFile()
foo.write(data)
foo.flush()
foo.seek(0)

gzf = gzip.GzipFile(fileobj=foo)

# this will trigger the AttributeError
for x in gzf:
    print(x)
msg191998 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-06-28 15:30
Based on a quick glance at the code, the problem isn't that you are passing fileobj, it is that fileobj has been opened for writing, not reading, and you are attempting to read from it.  extrastart (and other attibutes) are only set if mode starts with 'r'.

There is certainly a bug here, but it might just be that a clearer error should be generated if you try to read a fileobj open for writing.
msg191999 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-06-28 15:31
On the other hand, it seems reasonable to be able to read if you've done a seek(0), so a more complicated fix may also be appropriate.
msg196147 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-08-25 15:28
We can add in readline() the check that GzipFile is opened in read mode. However it will slowdown readline(). See alse issue18003.

Note that the original example is not correct. zlib.compress() doesn't produce legal Gzip file.
msg243391 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-17 09:14
As a workaround specify mode='r' for GzipFile.
History
Date User Action Args
2018-07-11 07:40:02serhiy.storchakasettype: crash -> behavior
2015-05-17 09:14:42serhiy.storchakasetmessages: + msg243391
2013-08-25 15:28:06serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg196147
2013-08-25 13:04:13gkcnsetnosy: + gkcn
2013-06-28 15:31:03r.david.murraysetmessages: + msg191999
2013-06-28 15:30:02r.david.murraysetnosy: + r.david.murray
messages: + msg191998
2013-06-28 13:04:55pitrousetnosy: + nadeem.vawda
2013-06-28 11:26:57Laurent.Gautiercreate