Skip to content

Commit 91b0bc2

Browse files
Issue #20331: Fixed possible FD leaks in various modules:
http.server, imghdr, mailcap, mimetypes, xml.etree.
1 parent 9332096 commit 91b0bc2

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

Lib/http/server.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,10 @@ def do_GET(self):
670670
"""Serve a GET request."""
671671
f = self.send_head()
672672
if f:
673-
self.copyfile(f, self.wfile)
674-
f.close()
673+
try:
674+
self.copyfile(f, self.wfile)
675+
finally:
676+
f.close()
675677

676678
def do_HEAD(self):
677679
"""Serve a HEAD request."""
@@ -712,13 +714,17 @@ def send_head(self):
712714
except IOError:
713715
self.send_error(404, "File not found")
714716
return None
715-
self.send_response(200)
716-
self.send_header("Content-type", ctype)
717-
fs = os.fstat(f.fileno())
718-
self.send_header("Content-Length", str(fs[6]))
719-
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
720-
self.end_headers()
721-
return f
717+
try:
718+
self.send_response(200)
719+
self.send_header("Content-type", ctype)
720+
fs = os.fstat(f.fileno())
721+
self.send_header("Content-Length", str(fs[6]))
722+
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
723+
self.end_headers()
724+
return f
725+
except:
726+
f.close()
727+
raise
722728

723729
def list_directory(self, path):
724730
"""Helper to produce a directory listing (absent index.html).

Lib/imghdr.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
#-------------------------#
88

99
def what(file, h=None):
10-
if h is None:
11-
if isinstance(file, str):
12-
f = open(file, 'rb')
13-
h = f.read(32)
14-
else:
15-
location = file.tell()
16-
h = file.read(32)
17-
file.seek(location)
18-
f = None
19-
else:
20-
f = None
10+
f = None
2111
try:
12+
if h is None:
13+
if isinstance(file, str):
14+
f = open(file, 'rb')
15+
h = f.read(32)
16+
else:
17+
location = file.tell()
18+
h = file.read(32)
19+
file.seek(location)
2220
for tf in tests:
2321
res = tf(h, f)
2422
if res:

Lib/mailcap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def getcaps():
2222
fp = open(mailcap, 'r')
2323
except IOError:
2424
continue
25-
morecaps = readmailcapfile(fp)
26-
fp.close()
25+
with fp:
26+
morecaps = readmailcapfile(fp)
2727
for key, value in morecaps.items():
2828
if not key in caps:
2929
caps[key] = value

Lib/mimetypes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,10 @@ def read_mime_types(file):
363363
f = open(file)
364364
except IOError:
365365
return None
366-
db = MimeTypes()
367-
db.readfp(f, True)
368-
return db.types_map[True]
366+
with f:
367+
db = MimeTypes()
368+
db.readfp(f, True)
369+
return db.types_map[True]
369370

370371

371372
def _default_mime_types():

Lib/xml/etree/ElementInclude.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@ class FatalIncludeError(SyntaxError):
7676

7777
def default_loader(href, parse, encoding=None):
7878
if parse == "xml":
79-
file = open(href, 'rb')
80-
data = ElementTree.parse(file).getroot()
79+
with open(href, 'rb') as file:
80+
data = ElementTree.parse(file).getroot()
8181
else:
8282
if not encoding:
8383
encoding = 'UTF-8'
84-
file = open(href, 'r', encoding=encoding)
85-
data = file.read()
86-
file.close()
84+
with open(href, 'r', encoding=encoding) as file:
85+
data = file.read()
8786
return data
8887

8988
##

0 commit comments

Comments
 (0)