[Python-checkins] bpo-43787: Add __iter__ to GzipFile, BZ2File, and LZMAFile (GH-25353)

methane webhook-mailer at python.org
Tue Apr 13 00:51:57 EDT 2021


https://github.com/python/cpython/commit/d2a8e69c2c605fbaa3656a5f99aa8d295f74c80e
commit: d2a8e69c2c605fbaa3656a5f99aa8d295f74c80e
branch: master
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2021-04-13T13:51:49+09:00
summary:

bpo-43787: Add __iter__ to GzipFile, BZ2File, and LZMAFile (GH-25353)

files:
A Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst
M Lib/bz2.py
M Lib/gzip.py
M Lib/lzma.py

diff --git a/Lib/bz2.py b/Lib/bz2.py
index 43f321ae85239..a2c588e7487f3 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 0a8993ba35471..9a4e0f9c00c58 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 c8b197055cddc..2ada7d81d3c81 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.
 
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 0000000000000..9b8d945cbb8a8
--- /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.



More information about the Python-checkins mailing list