[Python-checkins] cpython (2.7): Issue #5148: Ignore 'U' in mode given to gzip.open() and gzip.GzipFile().

nadeem.vawda python-checkins at python.org
Sun Oct 21 18:30:14 CEST 2012


http://hg.python.org/cpython/rev/e647229c422b
changeset:   79877:e647229c422b
branch:      2.7
parent:      79867:437a12f6dc9c
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Sun Oct 21 18:15:05 2012 +0200
summary:
  Issue #5148: Ignore 'U' in mode given to gzip.open() and gzip.GzipFile().

files:
  Lib/gzip.py           |  4 ++++
  Lib/test/test_gzip.py |  7 +++++++
  Misc/NEWS             |  2 ++
  3 files changed, 13 insertions(+), 0 deletions(-)


diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -81,6 +81,10 @@
 
         """
 
+        # 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:
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -53,6 +53,13 @@
             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.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -122,6 +122,8 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list