summaryrefslogtreecommitdiffstats
path: root/Lib/xmlrpc/client.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-04 08:01:02 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-04 08:01:02 (GMT)
commit46ba6c8563922f043cad6423202ee0119614c807 (patch)
tree4523d1a3665af25ef77898f7d2da186fbdca8ce7 /Lib/xmlrpc/client.py
parentae2d667ae83548029fed7244619fadd7f625cb24 (diff)
downloadcpython-46ba6c8563922f043cad6423202ee0119614c807.zip
cpython-46ba6c8563922f043cad6423202ee0119614c807.tar.gz
cpython-46ba6c8563922f043cad6423202ee0119614c807.tar.bz2
Issue #22831: Use "with" to avoid possible fd leaks.
Diffstat (limited to 'Lib/xmlrpc/client.py')
-rw-r--r--Lib/xmlrpc/client.py28
1 files changed, 11 insertions, 17 deletions
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py
index 047929a..34208d1 100644
--- a/Lib/xmlrpc/client.py
+++ b/Lib/xmlrpc/client.py
@@ -1010,12 +1010,9 @@ def gzip_encode(data):
if not gzip:
raise NotImplementedError
f = BytesIO()
- gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1)
- gzf.write(data)
- gzf.close()
- encoded = f.getvalue()
- f.close()
- return encoded
+ with gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) as gzf:
+ gzf.write(data)
+ return f.getvalue()
##
# Decode a string using the gzip content encoding such as specified by the
@@ -1036,17 +1033,14 @@ def gzip_decode(data, max_decode=20971520):
"""
if not gzip:
raise NotImplementedError
- f = BytesIO(data)
- gzf = gzip.GzipFile(mode="rb", fileobj=f)
- try:
- if max_decode < 0: # no limit
- decoded = gzf.read()
- else:
- decoded = gzf.read(max_decode + 1)
- except OSError:
- raise ValueError("invalid data")
- f.close()
- gzf.close()
+ with gzip.GzipFile(mode="rb", fileobj=BytesIO(data)) as gzf:
+ try:
+ if max_decode < 0: # no limit
+ decoded = gzf.read()
+ else:
+ decoded = gzf.read(max_decode + 1)
+ except OSError:
+ raise ValueError("invalid data")
if max_decode >= 0 and len(decoded) > max_decode:
raise ValueError("max gzipped payload length exceeded")
return decoded