diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2013-10-20 07:58:57 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2013-10-20 07:58:57 -0400 |
commit | 249ad14dfedbe45919b48dbbff445394a64d985c (patch) | |
tree | f88f6356df0087c28e8d18a434e5e9142359352f /tests | |
parent | f0459a54bb3e703691359aa3078a8234173ec361 (diff) | |
download | python-coveragepy-249ad14dfedbe45919b48dbbff445394a64d985c.tar.gz |
with statements: no more finally close
Diffstat (limited to 'tests')
-rw-r--r-- | tests/coveragetest.py | 12 | ||||
-rw-r--r-- | tests/osinfo.py | 5 | ||||
-rw-r--r-- | tests/test_data.py | 10 | ||||
-rw-r--r-- | tests/test_execfile.py | 5 | ||||
-rw-r--r-- | tests/test_process.py | 5 |
5 files changed, 9 insertions, 28 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index f3ca53a..e1c38b2 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -190,11 +190,8 @@ class CoverageTest(TestCase): os.makedirs(dirs) # Create the file. - f = open(filename, 'wb') - try: + with open(filename, 'wb') as f: f.write(to_bytes(text)) - finally: - f.close() return filename @@ -223,17 +220,16 @@ class CoverageTest(TestCase): """ modfile = modname + '.py' - f = open(modfile, 'r') for suff in imp.get_suffixes(): if suff[0] == '.py': break - try: + + with open(modfile, 'r') as f: # pylint: disable=W0631 # (Using possibly undefined loop variable 'suff') mod = imp.load_module(modname, f, modfile, suff) - finally: - f.close() + return mod def start_import_stop(self, cov, modname): diff --git a/tests/osinfo.py b/tests/osinfo.py index acbec23..a123123 100644 --- a/tests/osinfo.py +++ b/tests/osinfo.py @@ -44,11 +44,8 @@ elif sys.platform == 'linux2': """Read the /proc/PID/status file to find memory use.""" try: # get pseudo file /proc/<pid>/status - t = open('/proc/%d/status' % os.getpid()) - try: + with open('/proc/%d/status' % os.getpid()) as t: v = t.read() - finally: - t.close() except IOError: return 0 # non-Linux? # get VmKey line e.g. 'VmRSS: 9999 kB\n ...' diff --git a/tests/test_data.py b/tests/test_data.py index 9c29289..31578f2 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -92,11 +92,8 @@ class DataTest(CoverageTest): covdata.add_line_data(DATA_1) covdata.write() - fdata = open(".coverage", 'rb') - try: + with open(".coverage", 'rb') as fdata: data = pickle.load(fdata) - finally: - fdata.close() lines = data['lines'] self.assertSameElements(lines.keys(), MEASURED_FILES_1) @@ -111,11 +108,8 @@ class DataTest(CoverageTest): covdata.add_arc_data(ARC_DATA_3) covdata.write() - fdata = open(".coverage", 'rb') - try: + with open(".coverage", 'rb') as fdata: data = pickle.load(fdata) - finally: - fdata.close() self.assertSameElements(data['lines'].keys(), []) arcs = data['arcs'] diff --git a/tests/test_execfile.py b/tests/test_execfile.py index 24c521b..ca13d7c 100644 --- a/tests/test_execfile.py +++ b/tests/test_execfile.py @@ -54,11 +54,8 @@ class RunFileTest(CoverageTest): # Make sure we can read any sort of line ending. pylines = """# try newlines|print('Hello, world!')|""".split('|') for nl in ('\n', '\r\n', '\r'): - fpy = open('nl.py', 'wb') - try: + with open('nl.py', 'wb') as fpy: fpy.write(nl.join(pylines).encode('utf-8')) - finally: - fpy.close() run_python_file('nl.py', ['nl.py']) self.assertEqual(self.stdout(), "Hello, world!\n"*3) diff --git a/tests/test_process.py b/tests/test_process.py index 4453fc5..1ab56e8 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -572,16 +572,13 @@ class ProcessStartupTest(CoverageTest): g = glob.glob(os.path.join(d, "*.pth")) if g: pth_path = os.path.join(d, "subcover.pth") - pth = open(pth_path, "w") - try: + with open(pth_path, "w") as pth: try: pth.write(pth_contents) self.pth_path = pth_path break except (IOError, OSError): # pragma: not covered pass - finally: - pth.close() else: # pragma: not covered raise Exception("Couldn't find a place for the .pth file") |