Skip to content

Commit

Permalink
[Test/Python] Fix warnings about unclosed files
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Oct 7, 2015
1 parent 5811035 commit 3914ede
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
7 changes: 4 additions & 3 deletions interfaces/cython/cantera/ck2cti.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,8 +1876,8 @@ def writeCTI(self, header=None, name='gas', transportModel='Mix',

lines.append('')

f = open(outName, 'w')
f.write('\n'.join(lines))
with open(outName, 'w') as f:
f.write('\n'.join(lines))

def showHelp(self):
print("""
Expand Down Expand Up @@ -1954,7 +1954,8 @@ def convertMech(self, inputFile, thermoFile=None,
if transportFile:
if not os.path.exists(transportFile):
raise IOError('Missing transport file: {0!r}'.format(transportFile))
lines = [strip_nonascii(line) for line in open(transportFile, 'rU')]
with open(transportFile, 'rU') as f:
lines = [strip_nonascii(line) for line in f]
self.parseTransportData(lines, transportFile, 1)

# Transport validation: make sure all species have transport data
Expand Down
6 changes: 4 additions & 2 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ def test_empty_reaction_section(self):
def test_reaction_comments1(self):
convertMech('../data/pdep-test.inp',
outName='pdep_test.cti', quiet=True)
text = open('pdep_test.cti').read()
with open('pdep_test.cti') as f:
text = f.read()
self.assertIn('Generic mechanism header', text)
self.assertIn('Single PLOG reaction', text)
self.assertIn('PLOG with duplicate rates and negative A-factors', text)
Expand All @@ -351,7 +352,8 @@ def test_reaction_comments2(self):
convertMech('../data/explicit-third-bodies.inp',
thermoFile='../data/dummy-thermo.dat',
outName='explicit_third_bodies.cti', quiet=True)
text = open('explicit_third_bodies.cti').read()
with open('explicit_third_bodies.cti') as f:
text = f.read()
self.assertIn('An end of line comment', text)
self.assertIn('A comment after the last reaction', text)

Expand Down
6 changes: 4 additions & 2 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,15 @@ def test_listFromFile(self):
self.assertEqual(eq1, eq2)

def test_listFromCti(self):
R = ct.Reaction.listFromCti(open('../../build/data/h2o2.cti').read())
with open('../../build/data/h2o2.cti') as f:
R = ct.Reaction.listFromCti(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in self.gas.reactions()]
self.assertEqual(eq1, eq2)

def test_listFromXml(self):
R = ct.Reaction.listFromCti(open('../../build/data/h2o2.xml').read())
with open('../../build/data/h2o2.xml') as f:
R = ct.Reaction.listFromCti(f.read())
eq1 = [r.equation for r in R]
eq2 = [r.equation for r in self.gas.reactions()]
self.assertEqual(eq1, eq2)
Expand Down
6 changes: 4 additions & 2 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,15 @@ def test_listFromFile_xml(self):
set(self.gas.species_names))

def test_listFromCti(self):
S = ct.Species.listFromCti(open('../../build/data/h2o2.cti').read())
with open('../../build/data/h2o2.cti') as f:
S = ct.Species.listFromCti(f.read())

self.assertEqual({sp.name for sp in S},
set(self.gas.species_names))

def test_listFromXml(self):
S = ct.Species.listFromXml(open('../../build/data/h2o2.xml').read())
with open('../../build/data/h2o2.xml') as f:
S = ct.Species.listFromXml(f.read())

self.assertEqual({sp.name for sp in S},
set(self.gas.species_names))
Expand Down

0 comments on commit 3914ede

Please sign in to comment.