]> granicus.if.org Git - python/commitdiff
Issue #20331: Fixed possible FD leaks in various modules:
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 25 Jan 2014 17:43:02 +0000 (19:43 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 25 Jan 2014 17:43:02 +0000 (19:43 +0200)
http.server, imghdr, mailcap, mimetypes, xml.etree.

Lib/http/server.py
Lib/imghdr.py
Lib/mailcap.py
Lib/mimetypes.py
Lib/xml/etree/ElementInclude.py

index 7050b95352203cca45e639b2829807567cbc889a..dab1eb65c4bc6327f4b1d7f2578612c0b5cec92e 100644 (file)
@@ -670,8 +670,10 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         """Serve a GET request."""
         f = self.send_head()
         if f:
-            self.copyfile(f, self.wfile)
-            f.close()
+            try:
+                self.copyfile(f, self.wfile)
+            finally:
+                f.close()
 
     def do_HEAD(self):
         """Serve a HEAD request."""
@@ -712,13 +714,17 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         except IOError:
             self.send_error(404, "File not found")
             return None
-        self.send_response(200)
-        self.send_header("Content-type", ctype)
-        fs = os.fstat(f.fileno())
-        self.send_header("Content-Length", str(fs[6]))
-        self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
-        self.end_headers()
-        return f
+        try:
+            self.send_response(200)
+            self.send_header("Content-type", ctype)
+            fs = os.fstat(f.fileno())
+            self.send_header("Content-Length", str(fs[6]))
+            self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
+            self.end_headers()
+            return f
+        except:
+            f.close()
+            raise
 
     def list_directory(self, path):
         """Helper to produce a directory listing (absent index.html).
index 6ee45daab8bc8fe708817eeeb0c5d5e11c324121..bdd47eea0dc55fdd3b6e49f1bd377564c8ecbcd6 100644 (file)
@@ -7,18 +7,16 @@ __all__ = ["what"]
 #-------------------------#
 
 def what(file, h=None):
-    if h is None:
-        if isinstance(file, str):
-            f = open(file, 'rb')
-            h = f.read(32)
-        else:
-            location = file.tell()
-            h = file.read(32)
-            file.seek(location)
-            f = None
-    else:
-        f = None
+    f = None
     try:
+        if h is None:
+            if isinstance(file, str):
+                f = open(file, 'rb')
+                h = f.read(32)
+            else:
+                location = file.tell()
+                h = file.read(32)
+                file.seek(location)
         for tf in tests:
             res = tf(h, f)
             if res:
index 99f4958bf70681abcce08839852b81aaacd13c8a..0c0b19c47c5a1a82238c8cbe2d3489392e0131dd 100644 (file)
@@ -22,8 +22,8 @@ def getcaps():
             fp = open(mailcap, 'r')
         except IOError:
             continue
-        morecaps = readmailcapfile(fp)
-        fp.close()
+        with fp:
+            morecaps = readmailcapfile(fp)
         for key, value in morecaps.items():
             if not key in caps:
                 caps[key] = value
index 3e742a755b1ae355e1da23275152375f5aa67a5c..cdebf7a66d27fd19e3168883b12fd65fd7ee9be2 100644 (file)
@@ -363,9 +363,10 @@ def read_mime_types(file):
         f = open(file)
     except IOError:
         return None
-    db = MimeTypes()
-    db.readfp(f, True)
-    return db.types_map[True]
+    with f:
+        db = MimeTypes()
+        db.readfp(f, True)
+        return db.types_map[True]
 
 
 def _default_mime_types():
index 6cc1b44e958e0d9179671afd1dd12458742d1914..71eeb05a6ecb6fb45a8411bf8f8827016d696c8e 100644 (file)
@@ -76,14 +76,13 @@ class FatalIncludeError(SyntaxError):
 
 def default_loader(href, parse, encoding=None):
     if parse == "xml":
-        file = open(href, 'rb')
-        data = ElementTree.parse(file).getroot()
+        with open(href, 'rb') as file:
+            data = ElementTree.parse(file).getroot()
     else:
         if not encoding:
             encoding = 'UTF-8'
-        file = open(href, 'r', encoding=encoding)
-        data = file.read()
-    file.close()
+        with open(href, 'r', encoding=encoding) as file:
+            data = file.read()
     return data
 
 ##