Kaydet (Commit) 3b4a4f51 authored tarafından Nadeem Vawda's avatar Nadeem Vawda

Stylistic tweaks to the bz2 module to make it easier to transplant changes from

it to http://pypi.python.org/pypi/bz2file.
üst fc7b0ec5
...@@ -9,7 +9,6 @@ __all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor", ...@@ -9,7 +9,6 @@ __all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor",
__author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>" __author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>"
import builtins
import io import io
import warnings import warnings
...@@ -28,6 +27,8 @@ _MODE_WRITE = 3 ...@@ -28,6 +27,8 @@ _MODE_WRITE = 3
_BUFFER_SIZE = 8192 _BUFFER_SIZE = 8192
_builtin_open = open
class BZ2File(io.BufferedIOBase): class BZ2File(io.BufferedIOBase):
...@@ -90,10 +91,10 @@ class BZ2File(io.BufferedIOBase): ...@@ -90,10 +91,10 @@ class BZ2File(io.BufferedIOBase):
mode_code = _MODE_WRITE mode_code = _MODE_WRITE
self._compressor = BZ2Compressor(compresslevel) self._compressor = BZ2Compressor(compresslevel)
else: else:
raise ValueError("Invalid mode: {!r}".format(mode)) raise ValueError("Invalid mode: %r" % (mode,))
if isinstance(filename, (str, bytes)): if isinstance(filename, (str, bytes)):
self._fp = builtins.open(filename, mode) self._fp = _builtin_open(filename, mode)
self._closefp = True self._closefp = True
self._mode = mode_code self._mode = mode_code
elif hasattr(filename, "read") or hasattr(filename, "write"): elif hasattr(filename, "read") or hasattr(filename, "write"):
...@@ -189,15 +190,17 @@ class BZ2File(io.BufferedIOBase): ...@@ -189,15 +190,17 @@ class BZ2File(io.BufferedIOBase):
if not rawblock: if not rawblock:
if self._decompressor.eof: if self._decompressor.eof:
# End-of-stream marker and end of file. We're good.
self._mode = _MODE_READ_EOF self._mode = _MODE_READ_EOF
self._size = self._pos self._size = self._pos
return False return False
else: else:
# Problem - we were expecting more compressed data.
raise EOFError("Compressed file ended before the " raise EOFError("Compressed file ended before the "
"end-of-stream marker was reached") "end-of-stream marker was reached")
# Continue to next stream.
if self._decompressor.eof: if self._decompressor.eof:
# Continue to next stream.
self._decompressor = BZ2Decompressor() self._decompressor = BZ2Decompressor()
self._buffer = self._decompressor.decompress(rawblock) self._buffer = self._decompressor.decompress(rawblock)
...@@ -412,7 +415,7 @@ class BZ2File(io.BufferedIOBase): ...@@ -412,7 +415,7 @@ class BZ2File(io.BufferedIOBase):
self._read_all(return_data=False) self._read_all(return_data=False)
offset = self._size + offset offset = self._size + offset
else: else:
raise ValueError("Invalid value for whence: {}".format(whence)) raise ValueError("Invalid value for whence: %s" % (whence,))
# Make it so that offset is the number of bytes to skip forward. # Make it so that offset is the number of bytes to skip forward.
if offset < self._pos: if offset < self._pos:
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment