Skip to content

Commit 9303ad7

Browse files
committed
Always use with to open files #280
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 9a8cda7 commit 9303ad7

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/attributecode/model.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ def _validate(self, *args, **kwargs):
514514
try:
515515
# TODO: we have lots the location by replacing it with a text
516516
location = add_unc(location)
517-
text = codecs.open(location, encoding='utf-8', errors='ignore').read()
517+
with codecs.open(location, encoding='utf-8', errors='ignore') as txt:
518+
text = txt.read()
518519
self.value[path] = text
519520
except Exception as e:
520521
# only keep the first 100 char of the exception
@@ -964,7 +965,8 @@ def load(self, location):
964965
errors = []
965966
try:
966967
loc = add_unc(loc)
967-
input_text = codecs.open(loc, encoding='utf-8').read()
968+
with codecs.open(loc, encoding='utf-8') as txt:
969+
input_text = txt.read()
968970
"""
969971
The running_inventory defines if the current process is 'inventory' or not.
970972
This is used for the validation of the about_resource_path.

tests/test_attrib.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ def test_check_template(self):
3232
assert attrib.check_template('template_string') == None
3333
assert attrib.check_template('{{template_string') == (1,
3434
"unexpected end of template, expected 'end of print statement'.",)
35-
template = open(get_test_loc('attrib_gen/test.template')).read()
35+
with open(get_test_loc('attrib_gen/test.template')) as tmpl:
36+
template = tmpl.read()
3637
assert attrib.check_template(template) == None
3738

3839
def test_check_template_default_is_valid(self):
39-
template = open(attrib.default_template).read()
40+
with open(attrib.default_template) as tmpl:
41+
template = tmpl.read()
4042
assert attrib.check_template(template) == None
4143

4244
def test_generate(self):
4345
expected = (u'Apache HTTP Server: 2.4.3\n'
4446
u'resource: httpd-2.4.3.tar.gz\n')
4547
test_file = get_test_loc('attrib_gen/attrib.ABOUT')
46-
template = open(get_test_loc('attrib_gen/test.template')).read()
48+
with open(get_test_loc('attrib_gen/test.template')) as tmpl:
49+
template = tmpl.read()
4750
_errors, abouts = model.collect_inventory(test_file)
4851
result = attrib.generate(abouts, template)
4952
self.assertEqual(expected, result)
@@ -52,6 +55,7 @@ def test_generate_from_file_with_default_template(self):
5255
test_file = get_test_loc('attrib_gen/attrib.ABOUT')
5356
_errors, abouts = model.collect_inventory(test_file)
5457
result = attrib.generate_from_file(abouts)
55-
expected = unicode(open(get_test_loc('attrib_gen/expected_default_attrib.html')).read())
58+
with open(get_test_loc('attrib_gen/expected_default_attrib.html')) as exp:
59+
expected = unicode(exp.read())
5660
self.assertEqual([x.rstrip() for x in expected.splitlines()],
5761
[x.rstrip() for x in result.splitlines()])

0 commit comments

Comments
 (0)