Cleanup unclosed files handles in templating and tests

This commit is contained in:
Ryan Petrello
2015-12-11 08:58:06 -08:00
parent 4c4f616b44
commit 459d820f26
4 changed files with 29 additions and 20 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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()

View File

@@ -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'