summaryrefslogtreecommitdiffstats
path: root/Lib/bz2.py
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-08-04 13:29:28 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-08-04 13:29:28 (GMT)
commit8280b4ba02db287b1d3fd70cfd63bc6343124ad0 (patch)
tree6e48acec1f8bb42b6aa970f2a14f0212df6ff770 /Lib/bz2.py
parentd9f38bc7043c6d94ea7d76249d48d18322b46e92 (diff)
downloadcpython-8280b4ba02db287b1d3fd70cfd63bc6343124ad0.zip
cpython-8280b4ba02db287b1d3fd70cfd63bc6343124ad0.tar.gz
cpython-8280b4ba02db287b1d3fd70cfd63bc6343124ad0.tar.bz2
#15546: Fix BZ2File.read1()'s handling of pathological input data.
Diffstat (limited to 'Lib/bz2.py')
-rw-r--r--Lib/bz2.py51
1 files changed, 28 insertions, 23 deletions
diff --git a/Lib/bz2.py b/Lib/bz2.py
index 2e808cd..a50adf7 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -174,29 +174,31 @@ class BZ2File(io.BufferedIOBase):
# Fill the readahead buffer if it is empty. Returns False on EOF.
def _fill_buffer(self):
- if self._buffer:
- return True
-
- if self._decompressor.unused_data:
- rawblock = self._decompressor.unused_data
- else:
- rawblock = self._fp.read(_BUFFER_SIZE)
-
- if not rawblock:
- if self._decompressor.eof:
- self._mode = _MODE_READ_EOF
- self._size = self._pos
- return False
+ # Depending on the input data, our call to the decompressor may not
+ # return any data. In this case, try again after reading another block.
+ while True:
+ if self._buffer:
+ return True
+
+ if self._decompressor.unused_data:
+ rawblock = self._decompressor.unused_data
else:
- raise EOFError("Compressed file ended before the "
- "end-of-stream marker was reached")
-
- # Continue to next stream.
- if self._decompressor.eof:
- self._decompressor = BZ2Decompressor()
+ rawblock = self._fp.read(_BUFFER_SIZE)
+
+ if not rawblock:
+ if self._decompressor.eof:
+ self._mode = _MODE_READ_EOF
+ self._size = self._pos
+ return False
+ else:
+ raise EOFError("Compressed file ended before the "
+ "end-of-stream marker was reached")
+
+ # Continue to next stream.
+ if self._decompressor.eof:
+ self._decompressor = BZ2Decompressor()
- self._buffer = self._decompressor.decompress(rawblock)
- return True
+ self._buffer = self._decompressor.decompress(rawblock)
# Read data until EOF.
# If return_data is false, consume the data without returning it.
@@ -256,11 +258,14 @@ class BZ2File(io.BufferedIOBase):
return self._read_block(size)
def read1(self, size=-1):
- """Read up to size uncompressed bytes with at most one read
- from the underlying stream.
+ """Read up to size uncompressed bytes, while trying to avoid
+ making multiple reads from the underlying stream.
Returns b'' if the file is at EOF.
"""
+ # Usually, read1() calls _fp.read() at most once. However, sometimes
+ # this does not give enough data for the decompressor to make progress.
+ # In this case we make multiple reads, to avoid returning b"".
with self._lock:
self._check_can_read()
if (size == 0 or self._mode == _MODE_READ_EOF or