From 452add08a186a09f346a28306c96f4c89bf93c9e Mon Sep 17 00:00:00 2001 From: Nadeem Vawda Date: Mon, 1 Oct 2012 23:02:50 +0200 Subject: Issue #16304: Another performance optimization for BZ2File. Patch by Serhiy Storchaka. --- Lib/bz2.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/bz2.py b/Lib/bz2.py index 37918a8..87bed21 100644 --- a/Lib/bz2.py +++ b/Lib/bz2.py @@ -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(): -- cgit v0.12 From 138ad5066d5e523eb81cabd4a05ed51d6664210d Mon Sep 17 00:00:00 2001 From: Nadeem Vawda Date: Mon, 1 Oct 2012 23:04:11 +0200 Subject: Fix error handling in new fast path of BZ2File.readline(). --- Lib/bz2.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/bz2.py b/Lib/bz2.py index 87bed21..da19e27 100644 --- a/Lib/bz2.py +++ b/Lib/bz2.py @@ -323,6 +323,7 @@ class BZ2File(io.BufferedIOBase): 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 -- cgit v0.12 From eb70be2b46e12ab81a80342906f63ee9c9b6ea3d Mon Sep 17 00:00:00 2001 From: Nadeem Vawda Date: Mon, 1 Oct 2012 23:05:32 +0200 Subject: Issue #16304: Further optimize BZ2File.readlines?(). --- Lib/bz2.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/bz2.py b/Lib/bz2.py index da19e27..c307507 100644 --- a/Lib/bz2.py +++ b/Lib/bz2.py @@ -319,9 +319,10 @@ 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. @@ -341,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) -- cgit v0.12 From 219c7b90d0d2e1eda3a76e2d777240c005c658cb Mon Sep 17 00:00:00 2001 From: Stefan Krah Date: Mon, 1 Oct 2012 23:21:45 +0200 Subject: Issue #15599: Increase the switch interval. Several systems cannot handle gil_interval == 1 in extreme situations. --- Lib/test/test_threaded_import.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py index 4a5d7be..0528b13 100644 --- a/Lib/test/test_threaded_import.py +++ b/Lib/test/test_threaded_import.py @@ -225,11 +225,9 @@ class ThreadedImportTests(unittest.TestCase): @reap_threads def test_main(): old_switchinterval = None - # Issue #15599: FreeBSD/KVM cannot handle gil_interval == 1. - new_switchinterval = 0.00001 if 'freebsd' in sys.platform else 0.00000001 try: old_switchinterval = sys.getswitchinterval() - sys.setswitchinterval(new_switchinterval) + sys.setswitchinterval(1e-5) except AttributeError: pass try: -- cgit v0.12