Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db04489

Browse files
committed
Patch #443899: Check modes on files before performing operations.
Use IOErrors where file objects use them.
1 parent c7f4541 commit db04489

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/gzip.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, filename=None, mode=None,
6161
zlib.DEF_MEM_LEVEL,
6262
0)
6363
else:
64-
raise ValueError, "Mode " + mode + " not supported"
64+
raise IOError, "Mode " + mode + " not supported"
6565

6666
self.fileobj = fileobj
6767
self.offset = 0
@@ -133,6 +133,10 @@ def _read_gzip_header(self):
133133

134134

135135
def write(self,data):
136+
if self.mode != WRITE:
137+
import errno
138+
raise IOError(errno.EBADF, "write() on read-only GzipFile object")
139+
136140
if self.fileobj is None:
137141
raise ValueError, "write() on closed GzipFile object"
138142
if len(data) > 0:
@@ -142,6 +146,10 @@ def write(self,data):
142146
self.offset += len(data)
143147

144148
def read(self, size=-1):
149+
if self.mode != READ:
150+
import errno
151+
raise IOError(errno.EBADF, "write() on read-only GzipFile object")
152+
145153
if self.extrasize <= 0 and self.fileobj is None:
146154
return ''
147155

0 commit comments

Comments
 (0)