mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Clean up GzipFile mode string handling code.
This commit is contained in:
parent
d1a107132c
commit
be66af424b
1 changed files with 5 additions and 6 deletions
11
Lib/gzip.py
11
Lib/gzip.py
|
@ -141,7 +141,7 @@ class GzipFile(io.BufferedIOBase):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if mode and ('t' in mode or 'U' in mode):
|
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:
|
if mode and 'b' not in mode:
|
||||||
mode += 'b'
|
mode += 'b'
|
||||||
if fileobj is None:
|
if fileobj is None:
|
||||||
|
@ -152,10 +152,9 @@ class GzipFile(io.BufferedIOBase):
|
||||||
else:
|
else:
|
||||||
filename = ''
|
filename = ''
|
||||||
if mode is None:
|
if mode is None:
|
||||||
if hasattr(fileobj, 'mode'): mode = fileobj.mode
|
mode = getattr(fileobj, 'mode', 'rb')
|
||||||
else: mode = 'rb'
|
|
||||||
|
|
||||||
if mode[0:1] == 'r':
|
if mode.startswith('r'):
|
||||||
self.mode = READ
|
self.mode = READ
|
||||||
# Set flag indicating start of a new member
|
# Set flag indicating start of a new member
|
||||||
self._new_member = True
|
self._new_member = True
|
||||||
|
@ -170,7 +169,7 @@ class GzipFile(io.BufferedIOBase):
|
||||||
self.min_readsize = 100
|
self.min_readsize = 100
|
||||||
fileobj = _PaddedFile(fileobj)
|
fileobj = _PaddedFile(fileobj)
|
||||||
|
|
||||||
elif mode[0:1] == 'w' or mode[0:1] == 'a':
|
elif mode.startswith(('w', 'a')):
|
||||||
self.mode = WRITE
|
self.mode = WRITE
|
||||||
self._init_write(filename)
|
self._init_write(filename)
|
||||||
self.compress = zlib.compressobj(compresslevel,
|
self.compress = zlib.compressobj(compresslevel,
|
||||||
|
@ -179,7 +178,7 @@ class GzipFile(io.BufferedIOBase):
|
||||||
zlib.DEF_MEM_LEVEL,
|
zlib.DEF_MEM_LEVEL,
|
||||||
0)
|
0)
|
||||||
else:
|
else:
|
||||||
raise IOError("Mode " + mode + " not supported")
|
raise ValueError("Invalid mode: {!r}".format(mode))
|
||||||
|
|
||||||
self.fileobj = fileobj
|
self.fileobj = fileobj
|
||||||
self.offset = 0
|
self.offset = 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue