Skip to content

Commit

Permalink
Close files opened in w3ctestlib (#16059)
Browse files Browse the repository at this point in the history
This causes problems with PyPy as without refcounting it doesn't
close files so quickly, and you run out of file descriptors on macOS.
  • Loading branch information
gsnedders authored Mar 26, 2019
1 parent 9925422 commit 5225e2c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
5 changes: 2 additions & 3 deletions css/tools/w3ctestlib/Indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ def indexGroup(self, group):

def __writeTemplate(self, template, data, outfile):
o = self.tt.process(template, data)
f = open(outfile, 'w')
f.write(o.encode('utf-8'))
f.close()
with open(outfile, 'w') as f:
f.write(o.encode('utf-8'))

def writeOverview(self, destDir, errorOut=sys.stderr, addTests=[]):
"""Write format-agnostic pages such as test suite overview pages,
Expand Down
74 changes: 38 additions & 36 deletions css/tools/w3ctestlib/Sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ def relativeURL(self, other):
def data(self):
"""Return file contents as a byte string."""
if (self._data is None):
self._data = open(self.sourcepath, 'r').read()
with open(self.sourcepath, 'r') as f:
self._data = f.read()
if (self._data.startswith(codecs.BOM_UTF8)):
self.encoding = 'utf-8-sig' # XXX look for other unicode BOMs
return self._data
Expand Down Expand Up @@ -580,8 +581,8 @@ def adjustReferences(source):
def write(self, format):
"""Writes FileSource.data() out to `self.relpath` through Format `format`."""
data = self.data()
f = open(format.dest(self.relpath), 'w')
f.write(data)
with open(format.dest(self.relpath), 'w') as f:
f.write(data)
if (self.metaSource):
self.metaSource.write(format) # XXX need to get output path from format, but not let it choose actual format

Expand Down Expand Up @@ -818,7 +819,8 @@ def data(self):
"""Merge contents of all config files represented by this source."""
data = ''
for src in self.sourcepath:
data += open(src).read()
with open(src) as f:
data += f.read()
data += '\n'
return data

Expand Down Expand Up @@ -868,35 +870,36 @@ def __iter__(self):
for src in self.sourcepath:
relbase = basepath(self.relpath)
srcbase = basepath(src)
for line in open(src):
strip = self.baseRE.search(line)
if strip:
striplist.append(strip.group(1))
line = self.stripRE.sub('', line)
m = self.parseRE.search(line)
if m:
record = ((join(srcbase, m.group(2)), join(srcbase, m.group(3))), \
(join(relbase, m.group(2)), join(relbase, m.group(3))), \
m.group(1))
# for strip in striplist:
# strip relrecord
if not exists(record[0][0]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest test file %s does not exist." \
% (src, record[0][0]))
elif not exists(record[0][1]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest reference file %s does not exist." \
% (src, record[0][1]))
elif not isPathInsideBase(record[1][0]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest test replath %s not within relpath root." \
% (src, record[1][0]))
elif not isPathInsideBase(record[1][1]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest test replath %s not within relpath root." \
% (src, record[1][1]))
yield record
with open(src) as f:
for line in f:
strip = self.baseRE.search(line)
if strip:
striplist.append(strip.group(1))
line = self.stripRE.sub('', line)
m = self.parseRE.search(line)
if m:
record = ((join(srcbase, m.group(2)), join(srcbase, m.group(3))), \
(join(relbase, m.group(2)), join(relbase, m.group(3))), \
m.group(1))
# for strip in striplist:
# strip relrecord
if not exists(record[0][0]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest test file %s does not exist." \
% (src, record[0][0]))
elif not exists(record[0][1]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest reference file %s does not exist." \
% (src, record[0][1]))
elif not isPathInsideBase(record[1][0]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest test replath %s not within relpath root." \
% (src, record[1][0]))
elif not isPathInsideBase(record[1][1]):
raise ReftestFilepathError("Manifest Error in %s: "
"Reftest test replath %s not within relpath root." \
% (src, record[1][1]))
yield record

import Utils # set up XML catalog
xhtmlns = '{http://www.w3.org/1999/xhtml}'
Expand Down Expand Up @@ -1026,9 +1029,8 @@ def write(self, format, output=None):
output = self.unicode()

# write
f = open(format.dest(self.relpath), 'w')
f.write(output.encode(self.encoding, 'xmlcharrefreplace'))
f.close()
with open(format.dest(self.relpath), 'w') as f:
f.write(output.encode(self.encoding, 'xmlcharrefreplace'))

def compact(self):
self.tree = None
Expand Down

0 comments on commit 5225e2c

Please sign in to comment.