Commit 7d85d61c authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #20331: Fixed possible FD leaks in various modules:

SimpleHTTPServer, imghdr, mailcap, mimetypes, xml.etree.
parent 677c6f41
......@@ -43,7 +43,9 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Serve a GET request."""
f = self.send_head()
if f:
try:
self.copyfile(f, self.wfile)
finally:
f.close()
def do_HEAD(self):
......@@ -88,6 +90,7 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
except IOError:
self.send_error(404, "File not found")
return None
try:
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
......@@ -95,6 +98,9 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
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).
......
......@@ -7,6 +7,8 @@ __all__ = ["what"]
#-------------------------#
def what(file, h=None):
f = None
try:
if h is None:
if isinstance(file, basestring):
f = open(file, 'rb')
......@@ -15,10 +17,6 @@ def what(file, h=None):
location = file.tell()
h = file.read(32)
file.seek(location)
f = None
else:
f = None
try:
for tf in tests:
res = tf(h, f)
if res:
......
......@@ -22,8 +22,8 @@ def getcaps():
fp = open(mailcap, 'r')
except IOError:
continue
with fp:
morecaps = readmailcapfile(fp)
fp.close()
for key, value in morecaps.iteritems():
if not key in caps:
caps[key] = value
......
......@@ -373,6 +373,7 @@ def read_mime_types(file):
f = open(file)
except IOError:
return None
with f:
db = MimeTypes()
db.readfp(f, True)
return db.types_map[True]
......
......@@ -75,14 +75,13 @@ class FatalIncludeError(SyntaxError):
# @throws IOError If the loader fails to load the resource.
def default_loader(href, parse, encoding=None):
file = open(href)
with open(href) as file:
if parse == "xml":
data = ElementTree.parse(file).getroot()
else:
data = file.read()
if encoding:
data = data.decode(encoding)
file.close()
return data
##
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment