summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-01-25 17:43:56 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-01-25 17:43:56 (GMT)
commitc0b0bb6e01f7ea6a1a857609e794842223c4aa5c (patch)
tree23a00341a5a15f7af05ecd0549eb729dd906788a
parenta3642b67ca2656ec6d5c10a52bbb643462236ded (diff)
parent91b0bc237c07b52c771e121098989680cfc3600d (diff)
downloadcpython-c0b0bb6e01f7ea6a1a857609e794842223c4aa5c.zip
cpython-c0b0bb6e01f7ea6a1a857609e794842223c4aa5c.tar.gz
cpython-c0b0bb6e01f7ea6a1a857609e794842223c4aa5c.tar.bz2
Issue #20331: Fixed possible FD leaks in various modules:
http.server, imghdr, mailcap, mimetypes, xml.etree.
-rw-r--r--Lib/http/server.py24
-rw-r--r--Lib/imghdr.py20
-rw-r--r--Lib/mailcap.py4
-rw-r--r--Lib/mimetypes.py7
-rw-r--r--Lib/xml/etree/ElementInclude.py9
5 files changed, 34 insertions, 30 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py
index f170747..f019bd9 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -678,8 +678,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."""
@@ -720,13 +722,17 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
except OSError:
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).
diff --git a/Lib/imghdr.py b/Lib/imghdr.py
index 0cba063..add2ea8 100644
--- a/Lib/imghdr.py
+++ b/Lib/imghdr.py
@@ -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:
diff --git a/Lib/mailcap.py b/Lib/mailcap.py
index bd61b0b..97e3035 100644
--- a/Lib/mailcap.py
+++ b/Lib/mailcap.py
@@ -22,8 +22,8 @@ def getcaps():
fp = open(mailcap, 'r')
except OSError:
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
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index d7161f8..b98c874 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -363,9 +363,10 @@ def read_mime_types(file):
f = open(file)
except OSError:
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():
diff --git a/Lib/xml/etree/ElementInclude.py b/Lib/xml/etree/ElementInclude.py
index 73e491e..963470e 100644
--- a/Lib/xml/etree/ElementInclude.py
+++ b/Lib/xml/etree/ElementInclude.py
@@ -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
##