Kaydet (Commit) 91b0bc23 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

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

http.server, imghdr, mailcap, mimetypes, xml.etree.
üst 93320968
...@@ -670,8 +670,10 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): ...@@ -670,8 +670,10 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
"""Serve a GET request.""" """Serve a GET request."""
f = self.send_head() f = self.send_head()
if f: if f:
self.copyfile(f, self.wfile) try:
f.close() self.copyfile(f, self.wfile)
finally:
f.close()
def do_HEAD(self): def do_HEAD(self):
"""Serve a HEAD request.""" """Serve a HEAD request."""
...@@ -712,13 +714,17 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): ...@@ -712,13 +714,17 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
except IOError: except IOError:
self.send_error(404, "File not found") self.send_error(404, "File not found")
return None return None
self.send_response(200) try:
self.send_header("Content-type", ctype) self.send_response(200)
fs = os.fstat(f.fileno()) self.send_header("Content-type", ctype)
self.send_header("Content-Length", str(fs[6])) fs = os.fstat(f.fileno())
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) self.send_header("Content-Length", str(fs[6]))
self.end_headers() self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
return f self.end_headers()
return f
except:
f.close()
raise
def list_directory(self, path): def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html). """Helper to produce a directory listing (absent index.html).
......
...@@ -7,18 +7,16 @@ __all__ = ["what"] ...@@ -7,18 +7,16 @@ __all__ = ["what"]
#-------------------------# #-------------------------#
def what(file, h=None): def what(file, h=None):
if h is None: f = 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
try: 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: for tf in tests:
res = tf(h, f) res = tf(h, f)
if res: if res:
......
...@@ -22,8 +22,8 @@ def getcaps(): ...@@ -22,8 +22,8 @@ def getcaps():
fp = open(mailcap, 'r') fp = open(mailcap, 'r')
except IOError: except IOError:
continue continue
morecaps = readmailcapfile(fp) with fp:
fp.close() morecaps = readmailcapfile(fp)
for key, value in morecaps.items(): for key, value in morecaps.items():
if not key in caps: if not key in caps:
caps[key] = value caps[key] = value
......
...@@ -363,9 +363,10 @@ def read_mime_types(file): ...@@ -363,9 +363,10 @@ def read_mime_types(file):
f = open(file) f = open(file)
except IOError: except IOError:
return None return None
db = MimeTypes() with f:
db.readfp(f, True) db = MimeTypes()
return db.types_map[True] db.readfp(f, True)
return db.types_map[True]
def _default_mime_types(): def _default_mime_types():
......
...@@ -76,14 +76,13 @@ class FatalIncludeError(SyntaxError): ...@@ -76,14 +76,13 @@ class FatalIncludeError(SyntaxError):
def default_loader(href, parse, encoding=None): def default_loader(href, parse, encoding=None):
if parse == "xml": if parse == "xml":
file = open(href, 'rb') with open(href, 'rb') as file:
data = ElementTree.parse(file).getroot() data = ElementTree.parse(file).getroot()
else: else:
if not encoding: if not encoding:
encoding = 'UTF-8' encoding = 'UTF-8'
file = open(href, 'r', encoding=encoding) with open(href, 'r', encoding=encoding) as file:
data = file.read() data = file.read()
file.close()
return data 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