]> granicus.if.org Git - python/commitdiff
Issue #5148: Ignore 'U' in mode given to gzip.open() and gzip.GzipFile().
authorNadeem Vawda <nadeem.vawda@gmail.com>
Sun, 21 Oct 2012 16:15:05 +0000 (18:15 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Sun, 21 Oct 2012 16:15:05 +0000 (18:15 +0200)
Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS

index 8fdac8397d4d7e9e385dfd445abc28513619dab8..2ae7c0cffe3817d03a6dad2efcf4a85bca04309e 100644 (file)
@@ -81,6 +81,10 @@ class GzipFile(io.BufferedIOBase):
 
         """
 
+        # Make sure we don't inadvertently enable universal newlines on the
+        # underlying file object - in read mode, this causes data corruption.
+        if mode:
+            mode = mode.replace('U', '')
         # guarantee the file is opened in binary mode on platforms
         # that care about that sort of thing
         if mode and 'b' not in mode:
index a28cd34b0ca9a06b467c7f807466b13e6db87e94..9f7bfd2ed557a0fb99347fd5c0212f4bd72950f1 100644 (file)
@@ -53,6 +53,13 @@ class TestGzip(unittest.TestCase):
             d = f.read()
         self.assertEqual(d, data1*50)
 
+    def test_read_universal_newlines(self):
+        # Issue #5148: Reading breaks when mode contains 'U'.
+        self.test_write()
+        with gzip.GzipFile(self.filename, 'rU') as f:
+            d = f.read()
+        self.assertEqual(d, data1*50)
+
     def test_io_on_closed_object(self):
         # Test that I/O operations on closed GzipFile objects raise a
         # ValueError, just like the corresponding functions on file objects.
index 7e91943ceae20f96b928b5c9d28c24bbe03ab43d..4fa216d55a6d620ce130f0b5f8a5d617f1970801 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -122,6 +122,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #5148: Ignore 'U' in mode given to gzip.open() and gzip.GzipFile().
+
 - Issue #16220: wsgiref now always calls close() on an iterable response.
   Patch by Brent Tubbs.