From abf4ee4c2820d50d06f614d9bc78381ff4f13b7c Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 12 Apr 2021 15:08:03 +0900 Subject: [PATCH 1/3] bpo-43787: Add __iter__() method to GzipFile, BZ2File, and LZMAFile --- Lib/bz2.py | 4 ++++ Lib/gzip.py | 4 ++++ Lib/lzma.py | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/Lib/bz2.py b/Lib/bz2.py index 43f321ae852398..a2c588e7487f3d 100644 --- a/Lib/bz2.py +++ b/Lib/bz2.py @@ -197,6 +197,10 @@ def readline(self, size=-1): self._check_can_read() return self._buffer.readline(size) + def __iter__(self): + self._check_can_read() + return self._buffer.__iter__() + def readlines(self, size=-1): """Read a list of lines of uncompressed bytes from the file. diff --git a/Lib/gzip.py b/Lib/gzip.py index 0a8993ba354711..9a4e0f9c00c580 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -398,6 +398,10 @@ def readline(self, size=-1): self._check_not_closed() return self._buffer.readline(size) + def __iter__(self): + self._check_not_closed() + return self._buffer.__iter__() + class _GzipReader(_compression.DecompressReader): def __init__(self, fp): diff --git a/Lib/lzma.py b/Lib/lzma.py index c8b197055cddce..2ada7d81d3c813 100644 --- a/Lib/lzma.py +++ b/Lib/lzma.py @@ -221,6 +221,10 @@ def readline(self, size=-1): self._check_can_read() return self._buffer.readline(size) + def __iter__(self): + self._check_can_read() + return self._buffer.__iter__() + def write(self, data): """Write a bytes object to the file. From 063a68385c00b8dbb17b19c9000262ea37c97959 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 12 Apr 2021 15:15:55 +0900 Subject: [PATCH 2/3] Add news --- .../next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst diff --git a/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst b/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst new file mode 100644 index 00000000000000..97004b3ba6cdd7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst @@ -0,0 +1,3 @@ +Add `__iter__()` method to :class:`bz2.BZ2File`, :class:`gzip.GzipFile`, and +:class:`lzma.LZMAFile`. It makes iterating them about 2x faster. Patch by +Inada Naoki. From e422cd9c76175ffd508a2a8be39a0bdd179b6ffd Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 12 Apr 2021 15:17:18 +0900 Subject: [PATCH 3/3] fix news rest format. --- .../next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst b/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst index 97004b3ba6cdd7..9b8d945cbb8a8b 100644 --- a/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst +++ b/Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst @@ -1,3 +1,3 @@ -Add `__iter__()` method to :class:`bz2.BZ2File`, :class:`gzip.GzipFile`, and +Add ``__iter__()`` method to :class:`bz2.BZ2File`, :class:`gzip.GzipFile`, and :class:`lzma.LZMAFile`. It makes iterating them about 2x faster. Patch by Inada Naoki.