]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43787: Add __iter__ to GzipFile, BZ2File, and LZMAFile (GH-25353)
authorInada Naoki <songofacandy@gmail.com>
Tue, 13 Apr 2021 04:51:49 +0000 (13:51 +0900)
committerGitHub <noreply@github.com>
Tue, 13 Apr 2021 04:51:49 +0000 (13:51 +0900)
Lib/bz2.py
Lib/gzip.py
Lib/lzma.py
Misc/NEWS.d/next/Library/2021-04-12-15-15-50.bpo-43787.wCy_Wd.rst [new file with mode: 0644]

index 43f321ae85239844096a91732ccad4f1d067b4c8..a2c588e7487f3d98d442c7d7c49ae5dc312f55c8 100644 (file)
@@ -197,6 +197,10 @@ class BZ2File(_compression.BaseStream):
         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.
 
index 0a8993ba354711c93a4ed04aebf944d86be7c498..9a4e0f9c00c580ddf3a2ce1ba2f7e7445afd648d 100644 (file)
@@ -398,6 +398,10 @@ class GzipFile(_compression.BaseStream):
         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):
index c8b197055cddceca5db24cd9790f9f6f94f6d499..2ada7d81d3c813b85a6815285bcb23db6f8cd1da 100644 (file)
@@ -221,6 +221,10 @@ class LZMAFile(_compression.BaseStream):
         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 (file)
index 0000000..9b8d945
--- /dev/null
@@ -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.