diff options
| author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-10-01 21:11:35 (GMT) |
|---|---|---|
| committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-10-01 21:11:35 (GMT) |
| commit | 40ee15745808cbb1ff3bd0375f6959bec076583d (patch) | |
| tree | 19b3446ac63b05827570a6a847aced17a04931ce | |
| parent | 4993cc0a5b34dc91da2b41c50e33d809f0191355 (diff) | |
| parent | eb70be2b46e12ab81a80342906f63ee9c9b6ea3d (diff) | |
| download | cpython-40ee15745808cbb1ff3bd0375f6959bec076583d.zip cpython-40ee15745808cbb1ff3bd0375f6959bec076583d.tar.gz cpython-40ee15745808cbb1ff3bd0375f6959bec076583d.tar.bz2 | |
Merge: #16304: Optimizations for BZ2File, and minor bugfix.
| -rw-r--r-- | Lib/bz2.py | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -159,21 +159,18 @@ class BZ2File(io.BufferedIOBase): raise ValueError("I/O operation on closed file") def _check_can_read(self): - if self.closed: - raise ValueError("I/O operation on closed file") if self._mode not in (_MODE_READ, _MODE_READ_EOF): + self._check_not_closed() raise io.UnsupportedOperation("File not open for reading") def _check_can_write(self): - if self.closed: - raise ValueError("I/O operation on closed file") if self._mode != _MODE_WRITE: + self._check_not_closed() raise io.UnsupportedOperation("File not open for writing") def _check_can_seek(self): - if self.closed: - raise ValueError("I/O operation on closed file") if self._mode not in (_MODE_READ, _MODE_READ_EOF): + self._check_not_closed() raise io.UnsupportedOperation("Seeking is only supported " "on files open for reading") if not self._fp.seekable(): @@ -322,10 +319,12 @@ class BZ2File(io.BufferedIOBase): non-negative, no more than size bytes will be read (in which case the line may be incomplete). Returns b'' if already at EOF. """ - if not hasattr(size, "__index__"): - raise TypeError("Integer argument expected") - size = size.__index__() + if not isinstance(size, int): + if not hasattr(size, "__index__"): + raise TypeError("Integer argument expected") + size = size.__index__() with self._lock: + self._check_can_read() # Shortcut for the common case - the whole line is in the buffer. if size < 0: end = self._buffer.find(b"\n", self._buffer_offset) + 1 @@ -343,9 +342,10 @@ class BZ2File(io.BufferedIOBase): further lines will be read once the total size of the lines read so far equals or exceeds size. """ - if not hasattr(size, "__index__"): - raise TypeError("Integer argument expected") - size = size.__index__() + if not isinstance(size, int): + if not hasattr(size, "__index__"): + raise TypeError("Integer argument expected") + size = size.__index__() with self._lock: return io.BufferedIOBase.readlines(self, size) |
