From 3914ede44a8a9966ea9ffe545ed9dcb5f89f8c8c Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 7 Oct 2015 12:55:47 -0400 Subject: [PATCH] [Test/Python] Fix warnings about unclosed files --- interfaces/cython/cantera/ck2cti.py | 7 ++++--- interfaces/cython/cantera/test/test_convert.py | 6 ++++-- interfaces/cython/cantera/test/test_kinetics.py | 6 ++++-- interfaces/cython/cantera/test/test_thermo.py | 6 ++++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/interfaces/cython/cantera/ck2cti.py b/interfaces/cython/cantera/ck2cti.py index bb5890374..10bb004e9 100644 --- a/interfaces/cython/cantera/ck2cti.py +++ b/interfaces/cython/cantera/ck2cti.py @@ -1876,8 +1876,8 @@ class Parser(object): 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(""" @@ -1954,7 +1954,8 @@ duplicate transport data) to be ignored. 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 diff --git a/interfaces/cython/cantera/test/test_convert.py b/interfaces/cython/cantera/test/test_convert.py index bb5b7a55e..be9c4d88d 100644 --- a/interfaces/cython/cantera/test/test_convert.py +++ b/interfaces/cython/cantera/test/test_convert.py @@ -342,7 +342,8 @@ class chemkinConverterTest(utilities.CanteraTest): 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) @@ -351,7 +352,8 @@ class chemkinConverterTest(utilities.CanteraTest): 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) diff --git a/interfaces/cython/cantera/test/test_kinetics.py b/interfaces/cython/cantera/test/test_kinetics.py index 75944337b..3289722e0 100644 --- a/interfaces/cython/cantera/test/test_kinetics.py +++ b/interfaces/cython/cantera/test/test_kinetics.py @@ -633,13 +633,15 @@ class TestReaction(utilities.CanteraTest): 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) diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 384c5d618..21ddef68c 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -858,13 +858,15 @@ class TestSpecies(utilities.CanteraTest): 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))