diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-04 08:01:02 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-04 08:01:02 (GMT) |
commit | 46ba6c8563922f043cad6423202ee0119614c807 (patch) | |
tree | 4523d1a3665af25ef77898f7d2da186fbdca8ce7 /Lib/xmlrpc | |
parent | ae2d667ae83548029fed7244619fadd7f625cb24 (diff) | |
download | cpython-46ba6c8563922f043cad6423202ee0119614c807.zip cpython-46ba6c8563922f043cad6423202ee0119614c807.tar.gz cpython-46ba6c8563922f043cad6423202ee0119614c807.tar.bz2 |
Issue #22831: Use "with" to avoid possible fd leaks.
Diffstat (limited to 'Lib/xmlrpc')
-rw-r--r-- | Lib/xmlrpc/client.py | 28 |
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 |