Use .read1() if available when using GzipFile

This commit is contained in:
Paul Tremberth 2016-01-15 14:57:15 +01:00
parent 131f463247
commit ee4fadc007
3 changed files with 16 additions and 11 deletions

View File

@ -11,9 +11,6 @@ env:
- TOXENV=py33
- TOXENV=py35
- TOXENV=docs
matrix:
allow_failures:
- env: TOXENV=py35
install:
- pip install -U tox twine wheel codecov
script: tox

View File

@ -4,30 +4,38 @@ try:
from cStringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
from io import UnsupportedOperation
from gzip import GzipFile
class ReadOneGzipFile(GzipFile):
def readone(self, size=-1):
try:
return self.read1(size)
except UnsupportedOperation:
return self.read(size)
def gunzip(data):
"""Gunzip the given data and return as much data as possible.
This is resilient to CRC checksum errors.
"""
f = GzipFile(fileobj=BytesIO(data))
f = ReadOneGzipFile(fileobj=BytesIO(data))
output = b''
chunk = b'.'
while chunk:
try:
chunk = f.read(8196)
chunk = f.readone(8196)
output += chunk
except (IOError, EOFError, struct.error):
# complete only if there is some data, otherwise re-raise
# see issue 87 about catching struct.error
# some pages are quite small so output is '' and f.extrabuf
# contains the whole page content
if output or f.extrabuf:
output += f.extrabuf
break
if output or getattr(f, 'extrabuf', None):
try:
output += f.extrabuf
finally:
break
else:
raise
return output

View File

@ -34,7 +34,7 @@ class MarshalFifoDiskQueueTest(t.FifoDiskQueueTest):
# Trigger Twisted bug #7989
import twisted.persisted.styles # NOQA
q = self.queue()
self.assertRaises(ValueError, q.push, lambda x: x)
self.assertRaises((ValueError, AttributeError), q.push, lambda x: x)
class ChunkSize1MarshalFifoDiskQueueTest(MarshalFifoDiskQueueTest):
chunksize = 1
@ -114,7 +114,7 @@ class MarshalLifoDiskQueueTest(t.LifoDiskQueueTest):
# Trigger Twisted bug #7989
import twisted.persisted.styles # NOQA
q = self.queue()
self.assertRaises(ValueError, q.push, lambda x: x)
self.assertRaises((ValueError, AttributeError), q.push, lambda x: x)
class PickleLifoDiskQueueTest(MarshalLifoDiskQueueTest):