From 459d820f26820015608d36abade746ddda0d5ba2 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Fri, 11 Dec 2015 08:58:06 -0800 Subject: [PATCH] Cleanup unclosed files handles in templating and tests --- .travis.yml | 2 +- pecan/templating.py | 3 ++- pecan/tests/middleware/test_static.py | 6 ++++- pecan/tests/test_scaffolds.py | 38 +++++++++++++++------------ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 529f96e..eab4991 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,7 +37,7 @@ env: - TOXENV=wsme-tip install: - - travis_retry pip install tox + - travis_retry pip install "tox < 2.3" script: - travis_wait tox diff --git a/pecan/templating.py b/pecan/templating.py index 81a60a0..7d92447 100644 --- a/pecan/templating.py +++ b/pecan/templating.py @@ -189,7 +189,8 @@ def format_line_context(filename, lineno, context=10): :param context: number of lines of code to display before and after the offending line. ''' - lines = open(filename).readlines() + with open(filename) as f: + lines = f.readlines() lineno = lineno - 1 # files are indexed by 1 not 0 if lineno > 0: diff --git a/pecan/tests/middleware/test_static.py b/pecan/tests/middleware/test_static.py index 9a0c08c..8383d0a 100644 --- a/pecan/tests/middleware/test_static.py +++ b/pecan/tests/middleware/test_static.py @@ -40,6 +40,7 @@ class TestStaticFileMiddleware(PecanTestCase): def test_file_can_be_found(self): result = self._request('/static_fixtures/text.txt') assert isinstance(result, FileWrapper) + result.close() def test_no_file_found_causes_passthrough(self): result = self._request('/static_fixtures/nosuchfile.txt') @@ -47,8 +48,9 @@ class TestStaticFileMiddleware(PecanTestCase): assert result == ['Hello world!\n'] def test_mime_type_works_for_png_files(self): - self._request('/static_fixtures/self.png') + result = self._request('/static_fixtures/self.png') assert self._get_response_header('Content-Type') == 'image/png' + result.close() def test_file_can_be_closed(self): result = self._request('/static_fixtures/text.txt') @@ -57,6 +59,7 @@ class TestStaticFileMiddleware(PecanTestCase): def test_file_can_be_iterated_over(self): result = self._request('/static_fixtures/text.txt') assert len([x for x in result]) + result.close() def test_date_dumping_on_unix_timestamps(self): result = _dump_date(1331755274.59, ' ') @@ -66,3 +69,4 @@ class TestStaticFileMiddleware(PecanTestCase): os.altsep = ':' result = self._request(':static_fixtures:text.txt') assert isinstance(result, FileWrapper) + result.close() diff --git a/pecan/tests/test_scaffolds.py b/pecan/tests/test_scaffolds.py index 669aa7d..d294d5e 100644 --- a/pecan/tests/test_scaffolds.py +++ b/pecan/tests/test_scaffolds.py @@ -61,12 +61,10 @@ class TestScaffoldUtils(PecanTestCase): assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'bar', 'spam.txt' )) - assert open(os.path.join( + with open(os.path.join( self.scaffold_destination, 'someapp', 'foo' - ), 'r').read().strip() == 'YAR' - assert open(os.path.join( - self.scaffold_destination, 'someapp', 'foo' - ), 'r').read().strip() == 'YAR' + ), 'r') as f: + assert f.read().strip() == 'YAR' def test_destination_directory_levels_deep(self): from pecan.scaffolds import copy_dir @@ -86,12 +84,14 @@ class TestScaffoldUtils(PecanTestCase): assert os.path.isfile(os.path.join( self.scaffold_destination, 'some', 'app', 'bar', 'spam.txt') ) - assert open(os.path.join( + with open(os.path.join( self.scaffold_destination, 'some', 'app', 'foo' - ), 'r').read().strip() == 'YAR' - assert open(os.path.join( + ), 'r') as f: + assert f.read().strip() == 'YAR' + with open(os.path.join( self.scaffold_destination, 'some', 'app', 'bar', 'spam.txt' - ), 'r').read().strip() == 'Pecan' + ), 'r') as f: + assert f.read().strip() == 'Pecan' def test_destination_directory_already_exists(self): from pecan.scaffolds import copy_dir @@ -125,12 +125,14 @@ class TestScaffoldUtils(PecanTestCase): assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'bar_thingy', 'spam.txt') ) - assert open(os.path.join( + with open(os.path.join( self.scaffold_destination, 'someapp', 'foo_thingy' - ), 'r').read().strip() == 'YAR' - assert open(os.path.join( + ), 'r') as f: + assert f.read().strip() == 'YAR' + with open(os.path.join( self.scaffold_destination, 'someapp', 'bar_thingy', 'spam.txt' - ), 'r').read().strip() == 'Pecan' + ), 'r') as f: + assert f.read().strip() == 'Pecan' def test_copy_dir_with_file_content_substitution(self): from pecan.scaffolds import copy_dir @@ -152,9 +154,11 @@ class TestScaffoldUtils(PecanTestCase): assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'bar', 'spam.txt') ) - assert open(os.path.join( + with open(os.path.join( self.scaffold_destination, 'someapp', 'foo' - ), 'r').read().strip() == 'YAR thingy' - assert open(os.path.join( + ), 'r') as f: + assert f.read().strip() == 'YAR thingy' + with open(os.path.join( self.scaffold_destination, 'someapp', 'bar', 'spam.txt' - ), 'r').read().strip() == 'Pecan thingy' + ), 'r') as f: + assert f.read().strip() == 'Pecan thingy'