diff --git a/css/tools/w3ctestlib/Indexer.py b/css/tools/w3ctestlib/Indexer.py index 3b71f5bab748ba..f7e2eb2a173182 100644 --- a/css/tools/w3ctestlib/Indexer.py +++ b/css/tools/w3ctestlib/Indexer.py @@ -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, diff --git a/css/tools/w3ctestlib/Sources.py b/css/tools/w3ctestlib/Sources.py index 1528cbb8341422..fa704bf6dbc4a2 100644 --- a/css/tools/w3ctestlib/Sources.py +++ b/css/tools/w3ctestlib/Sources.py @@ -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 @@ -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 @@ -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 @@ -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}' @@ -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