]> granicus.if.org Git - python/commitdiff
Issue #22831: Use "with" to avoid possible fd leaks.
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 4 Apr 2015 08:01:02 +0000 (11:01 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 4 Apr 2015 08:01:02 +0000 (11:01 +0300)
Lib/binhex.py
Lib/cgitb.py
Lib/dbm/__init__.py
Lib/http/cookiejar.py
Lib/platform.py
Lib/pydoc.py
Lib/sre_constants.py
Lib/token.py
Lib/trace.py
Lib/xmlrpc/client.py

index 7bf9278430fbfa71fa6ad4da6e9bf6efb4f74869..14badb71aa5e1d2d841796caed333cf8bad34266 100644 (file)
@@ -229,14 +229,13 @@ def binhex(inp, out):
     finfo = getfileinfo(inp)
     ofp = BinHex(finfo, out)
 
-    ifp = io.open(inp, 'rb')
-    # XXXX Do textfile translation on non-mac systems
-    while True:
-        d = ifp.read(128000)
-        if not d: break
-        ofp.write(d)
-    ofp.close_data()
-    ifp.close()
+    with io.open(inp, 'rb') as ifp:
+        # XXXX Do textfile translation on non-mac systems
+        while True:
+            d = ifp.read(128000)
+            if not d: break
+            ofp.write(d)
+        ofp.close_data()
 
     ifp = openrsrc(inp, 'rb')
     while True:
@@ -449,13 +448,12 @@ def hexbin(inp, out):
     if not out:
         out = ifp.FName
 
-    ofp = io.open(out, 'wb')
-    # XXXX Do translation on non-mac systems
-    while True:
-        d = ifp.read(128000)
-        if not d: break
-        ofp.write(d)
-    ofp.close()
+    with io.open(out, 'wb') as ofp:
+        # XXXX Do translation on non-mac systems
+        while True:
+            d = ifp.read(128000)
+            if not d: break
+            ofp.write(d)
     ifp.close_data()
 
     d = ifp.read_rsrc(128000)
index 6eb52e764eefcc057363ee59899cb76f5671d07c..b29110018cd104c4d41256c25a704cb6cd705ba3 100644 (file)
@@ -294,9 +294,8 @@ class Hook:
             (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir)
 
             try:
-                file = os.fdopen(fd, 'w')
-                file.write(doc)
-                file.close()
+                with os.fdopen(fd, 'w') as file:
+                    file.write(doc)
                 msg = '%s contains the description of this error.' % path
             except:
                 msg = 'Tried to save traceback to %s, but failed.' % path
index 5f4664a7c652d2091a4a5b76c6316c7b92994296..6831a844073740d93db8c19ae1bc9a9b57625445 100644 (file)
@@ -153,9 +153,9 @@ def whichdb(filename):
     except OSError:
         return None
 
-    # Read the start of the file -- the magic number
-    s16 = f.read(16)
-    f.close()
+    with f:
+        # Read the start of the file -- the magic number
+        s16 = f.read(16)
     s = s16[0:4]
 
     # Return "" if not at least 4 bytes
index cc9e0bed8696b4d880a00fe0b870ba6e64ff3172..d54f58a27a26fed5798e78eb934644ea94a36e71 100644 (file)
@@ -1999,7 +1999,6 @@ class MozillaCookieJar(FileCookieJar):
 
         magic = f.readline()
         if not self.magic_re.search(magic):
-            f.close()
             raise LoadError(
                 "%r does not look like a Netscape format cookies file" %
                 filename)
index c4ffe95bca3db601108ee1dc2e740c1d129df005..b1c659ebbf42abe1e229ecf30ea799c09aadbf31 100755 (executable)
@@ -163,40 +163,39 @@ def libc_ver(executable=sys.executable, lib='', version='',
         # here to work around problems with Cygwin not being
         # able to open symlinks for reading
         executable = os.path.realpath(executable)
-    f = open(executable, 'rb')
-    binary = f.read(chunksize)
-    pos = 0
-    while 1:
-        if b'libc' in binary or b'GLIBC' in binary:
-            m = _libc_search.search(binary, pos)
-        else:
-            m = None
-        if not m:
-            binary = f.read(chunksize)
-            if not binary:
-                break
-            pos = 0
-            continue
-        libcinit, glibc, glibcversion, so, threads, soversion = [
-            s.decode('latin1') if s is not None else s
-            for s in m.groups()]
-        if libcinit and not lib:
-            lib = 'libc'
-        elif glibc:
-            if lib != 'glibc':
-                lib = 'glibc'
-                version = glibcversion
-            elif glibcversion > version:
-                version = glibcversion
-        elif so:
-            if lib != 'glibc':
+    with open(executable, 'rb') as f:
+        binary = f.read(chunksize)
+        pos = 0
+        while 1:
+            if b'libc' in binary or b'GLIBC' in binary:
+                m = _libc_search.search(binary, pos)
+            else:
+                m = None
+            if not m:
+                binary = f.read(chunksize)
+                if not binary:
+                    break
+                pos = 0
+                continue
+            libcinit, glibc, glibcversion, so, threads, soversion = [
+                s.decode('latin1') if s is not None else s
+                for s in m.groups()]
+            if libcinit and not lib:
                 lib = 'libc'
-                if soversion and soversion > version:
-                    version = soversion
-                if threads and version[-len(threads):] != threads:
-                    version = version + threads
-        pos = m.end()
-    f.close()
+            elif glibc:
+                if lib != 'glibc':
+                    lib = 'glibc'
+                    version = glibcversion
+                elif glibcversion > version:
+                    version = glibcversion
+            elif so:
+                if lib != 'glibc':
+                    lib = 'libc'
+                    if soversion and soversion > version:
+                        version = soversion
+                    if threads and version[-len(threads):] != threads:
+                        version = version + threads
+            pos = m.end()
     return lib, version
 
 def _dist_try_harder(distname, version, id):
index 9f3401f7661d4af1785a14183db9f4a298e14765..d77ed002ecbb871fc679c76a37a17ec172b747b4 100755 (executable)
@@ -1639,9 +1639,8 @@ def writedoc(thing, forceload=0):
     try:
         object, name = resolve(thing, forceload)
         page = html.page(describe(object), html.document(object, name))
-        file = open(name + '.html', 'w', encoding='utf-8')
-        file.write(page)
-        file.close()
+        with open(name + '.html', 'w', encoding='utf-8') as file:
+            file.write(page)
         print('wrote', name + '.html')
     except (ImportError, ErrorDuringImport) as value:
         print(value)
index 8b6bbfaaf36decb9db24e4503dea0f28008cc0ce..fc684ae96fd30a67ca129628663d769912e0a9d7 100644 (file)
@@ -182,8 +182,8 @@ if __name__ == "__main__":
         items = sorted(d)
         for item in items:
             f.write("#define %s_%s %d\n" % (prefix, item, item))
-    f = open("sre_constants.h", "w")
-    f.write("""\
+    with open("sre_constants.h", "w") as f:
+        f.write("""\
 /*
  * Secret Labs' Regular Expression Engine
  *
@@ -199,25 +199,24 @@ if __name__ == "__main__":
 
 """)
 
-    f.write("#define SRE_MAGIC %d\n" % MAGIC)
+        f.write("#define SRE_MAGIC %d\n" % MAGIC)
 
-    dump(f, OPCODES, "SRE_OP")
-    dump(f, ATCODES, "SRE")
-    dump(f, CHCODES, "SRE")
+        dump(f, OPCODES, "SRE_OP")
+        dump(f, ATCODES, "SRE")
+        dump(f, CHCODES, "SRE")
 
-    f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
-    f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
-    f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
-    f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
-    f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
-    f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
-    f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
-    f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
-    f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
+        f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
+        f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
+        f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
+        f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
+        f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
+        f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
+        f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
+        f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
+        f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
 
-    f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
-    f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
-    f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
+        f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
+        f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
+        f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
 
-    f.close()
     print("done")
index bdfcec8ea4b1ddc2063fd9d8fdec8d82d0f2b1c8..a95d9b777af25d61e3eeaea75219be2ef39b75c4 100644 (file)
@@ -97,8 +97,8 @@ def _main():
     except OSError as err:
         sys.stdout.write("I/O error: %s\n" % str(err))
         sys.exit(1)
-    lines = fp.read().split("\n")
-    fp.close()
+    with fp:
+        lines = fp.read().split("\n")
     prog = re.compile(
         "#define[ \t][ \t]*([A-Z0-9][A-Z0-9_]*)[ \t][ \t]*([0-9][0-9]*)",
         re.IGNORECASE)
@@ -116,8 +116,8 @@ def _main():
     except OSError as err:
         sys.stderr.write("I/O error: %s\n" % str(err))
         sys.exit(2)
-    format = fp.read().split("\n")
-    fp.close()
+    with fp:
+        format = fp.read().split("\n")
     try:
         start = format.index("#--start constants--") + 1
         end = format.index("#--end constants--")
@@ -133,8 +133,8 @@ def _main():
     except OSError as err:
         sys.stderr.write("I/O error: %s\n" % str(err))
         sys.exit(4)
-    fp.write("\n".join(format))
-    fp.close()
+    with fp:
+        fp.write("\n".join(format))
 
 
 if __name__ == "__main__":
index fe849734be455db53517344dfc4d14c2177c4ac8..41eff26db828db7ea522e936f07e2c5e6d207c47 100755 (executable)
@@ -232,8 +232,8 @@ class CoverageResults:
         if self.infile:
             # Try to merge existing counts file.
             try:
-                counts, calledfuncs, callers = \
-                        pickle.load(open(self.infile, 'rb'))
+                with open(self.infile, 'rb') as f:
+                    counts, calledfuncs, callers = pickle.load(f)
                 self.update(self.__class__(counts, calledfuncs, callers))
             except (OSError, EOFError, ValueError) as err:
                 print(("Skipping counts file %r: %s"
@@ -361,26 +361,26 @@ class CoverageResults:
 
         n_lines = 0
         n_hits = 0
-        for lineno, line in enumerate(lines, 1):
-            # do the blank/comment match to try to mark more lines
-            # (help the reader find stuff that hasn't been covered)
-            if lineno in lines_hit:
-                outfile.write("%5d: " % lines_hit[lineno])
-                n_hits += 1
-                n_lines += 1
-            elif rx_blank.match(line):
-                outfile.write("       ")
-            else:
-                # lines preceded by no marks weren't hit
-                # Highlight them if so indicated, unless the line contains
-                # #pragma: NO COVER
-                if lineno in lnotab and not PRAGMA_NOCOVER in line:
-                    outfile.write(">>>>>> ")
+        with outfile:
+            for lineno, line in enumerate(lines, 1):
+                # do the blank/comment match to try to mark more lines
+                # (help the reader find stuff that hasn't been covered)
+                if lineno in lines_hit:
+                    outfile.write("%5d: " % lines_hit[lineno])
+                    n_hits += 1
                     n_lines += 1
-                else:
+                elif rx_blank.match(line):
                     outfile.write("       ")
-            outfile.write(line.expandtabs(8))
-        outfile.close()
+                else:
+                    # lines preceded by no marks weren't hit
+                    # Highlight them if so indicated, unless the line contains
+                    # #pragma: NO COVER
+                    if lineno in lnotab and not PRAGMA_NOCOVER in line:
+                        outfile.write(">>>>>> ")
+                        n_lines += 1
+                    else:
+                        outfile.write("       ")
+                outfile.write(line.expandtabs(8))
 
         return n_hits, n_lines
 
index 047929a8611d4440d7102630a60433a2e02dbc02..34208d1c752d33603c5e22d391fa488b9ead5dd4 100644 (file)
@@ -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