[Python-checkins] cpython: Clean up GzipFile mode string handling code.

nadeem.vawda python-checkins at python.org
Sat Feb 11 23:06:33 CET 2012


http://hg.python.org/cpython/rev/6231b507540a
changeset:   74874:6231b507540a
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Sun Feb 12 00:06:02 2012 +0200
summary:
  Clean up GzipFile mode string handling code.

files:
  Lib/gzip.py |  11 +++++------
  1 files changed, 5 insertions(+), 6 deletions(-)


diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -141,7 +141,7 @@
         """
 
         if mode and ('t' in mode or 'U' in mode):
-            raise IOError("Mode " + mode + " not supported")
+            raise ValueError("Invalid mode: {!r}".format(mode))
         if mode and 'b' not in mode:
             mode += 'b'
         if fileobj is None:
@@ -152,10 +152,9 @@
             else:
                 filename = ''
         if mode is None:
-            if hasattr(fileobj, 'mode'): mode = fileobj.mode
-            else: mode = 'rb'
+            mode = getattr(fileobj, 'mode', 'rb')
 
-        if mode[0:1] == 'r':
+        if mode.startswith('r'):
             self.mode = READ
             # Set flag indicating start of a new member
             self._new_member = True
@@ -170,7 +169,7 @@
             self.min_readsize = 100
             fileobj = _PaddedFile(fileobj)
 
-        elif mode[0:1] == 'w' or mode[0:1] == 'a':
+        elif mode.startswith(('w', 'a')):
             self.mode = WRITE
             self._init_write(filename)
             self.compress = zlib.compressobj(compresslevel,
@@ -179,7 +178,7 @@
                                              zlib.DEF_MEM_LEVEL,
                                              0)
         else:
-            raise IOError("Mode " + mode + " not supported")
+            raise ValueError("Invalid mode: {!r}".format(mode))
 
         self.fileobj = fileobj
         self.offset = 0

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


More information about the Python-checkins mailing list