Skip to content

Commit

Permalink
Fix ResourceWarning: unclosed file
Browse files Browse the repository at this point in the history
Signed-off-by: Mickaël Schoentgen <contact@tiger-222.fr>
  • Loading branch information
BoboTiG authored and ericwb committed Jan 7, 2019
1 parent 3747dc8 commit 682c38b
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 74 deletions.
3 changes: 2 additions & 1 deletion bandit/core/config.py
Expand Up @@ -47,7 +47,8 @@ def __init__(self, config_file=None):
config_file)

try:
self._config = yaml.safe_load(f)
with f:
self._config = yaml.safe_load(f)
self.validate(config_file)
except yaml.YAMLError as err:
LOG.error(err)
Expand Down
25 changes: 10 additions & 15 deletions examples/hardcoded-tmp.py
@@ -1,21 +1,16 @@
f = open('/tmp/abc', 'w')
f.write('def')
f.close()
with open('/tmp/abc', 'w') as f:
f.write('def')

# ok
f = open('/abc/tmp', 'w')
f.write('def')
f.close()
with open('/abc/tmp', 'w') as f:
f.write('def')

f = open('/var/tmp/123', 'w')
f.write('def')
f.close()
with open('/var/tmp/123', 'w') as f:
f.write('def')

f = open('/dev/shm/unit/test', 'w')
f.write('def')
f.close()
with open('/dev/shm/unit/test', 'w') as f:
f.write('def')

# Negative test
f = open('/foo/bar', 'w')
f.write('def')
f.close()
with open('/foo/bar', 'w') as f:
f.write('def')
12 changes: 6 additions & 6 deletions tests/unit/core/test_manager.py
Expand Up @@ -157,9 +157,9 @@ def test_output_results_invalid_format(self):
conf_level = constants.LOW
output_filename = os.path.join(temp_directory, "_temp_output")
output_format = "invalid"
tmp_file = open(output_filename, 'w')
self.manager.output_results(lines, sev_level, conf_level, tmp_file,
output_format)
with open(output_filename, 'w') as tmp_file:
self.manager.output_results(lines, sev_level, conf_level,
tmp_file, output_format)
self.assertTrue(os.path.isfile(output_filename))

def test_output_results_valid_format(self):
Expand All @@ -170,9 +170,9 @@ def test_output_results_valid_format(self):
conf_level = constants.LOW
output_filename = os.path.join(temp_directory, "_temp_output.txt")
output_format = "txt"
tmp_file = open(output_filename, 'w')
self.manager.output_results(lines, sev_level, conf_level, tmp_file,
output_format)
with open(output_filename, 'w') as tmp_file:
self.manager.output_results(lines, sev_level, conf_level,
tmp_file, output_format)
self.assertTrue(os.path.isfile(output_filename))

@mock.patch('os.path.isdir')
Expand Down
14 changes: 6 additions & 8 deletions tests/unit/core/test_util.py
Expand Up @@ -28,8 +28,7 @@

def _touch(path):
'''Create an empty file at ``path``.'''
newf = open(path, 'w')
newf.close()
open(path, 'w').close()


class UtilTests(testtools.TestCase):
Expand Down Expand Up @@ -225,10 +224,10 @@ def test_get_call_name3(self):
# self.assertEqual(name, 'a.list[0]')

def test_linerange(self):
self.test_file = open("./examples/jinja2_templating.py")
self.tree = ast.parse(self.test_file.read())
with open("./examples/jinja2_templating.py") as test_file:
tree = ast.parse(test_file.read())
# Check linerange returns corrent number of lines
line = self.tree.body[8]
line = tree.body[8]
lrange = b_utils.linerange(line)

# line 9 should be three lines long
Expand Down Expand Up @@ -287,9 +286,8 @@ def test_parse_ini_file(self):

with tempfile.NamedTemporaryFile('r+') as t:
for test in tests:
f = open(t.name, 'w')
f.write(test['content'])
f.close()
with open(t.name, 'w') as f:
f.write(test['content'])

self.assertEqual(b_utils.parse_ini_file(t.name),
test['expected'])
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/formatters/test_csv.py
Expand Up @@ -48,9 +48,9 @@ def setUp(self):
self.manager.results.append(self.issue)

def test_report(self):
tmp_file = open(self.tmp_fname, 'w')
b_csv.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)
with open(self.tmp_fname, 'w') as tmp_file:
b_csv.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)

with open(self.tmp_fname) as f:
reader = csv.DictReader(f)
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/formatters/test_html.py
Expand Up @@ -41,9 +41,9 @@ def setUp(self):
def test_report_with_skipped(self):
self.manager.skipped = [('abc.py', 'File is bad')]

tmp_file = open(self.tmp_fname, 'w')
b_html.report(
self.manager, tmp_file, bandit.LOW, bandit.LOW)
with open(self.tmp_fname, 'w') as tmp_file:
b_html.report(
self.manager, tmp_file, bandit.LOW, bandit.LOW)

with open(self.tmp_fname) as f:
soup = bs4.BeautifulSoup(f.read(), 'html.parser')
Expand Down Expand Up @@ -78,9 +78,9 @@ def test_report_contents(self, get_issue_list, get_code):
[(issue_a, [issue_x, issue_y]),
(issue_b, [issue_x]), (issue_c, [issue_y])])

tmp_file = open(self.tmp_fname, 'w')
b_html.report(
self.manager, tmp_file, bandit.LOW, bandit.LOW)
with open(self.tmp_fname, 'w') as tmp_file:
b_html.report(
self.manager, tmp_file, bandit.LOW, bandit.LOW)

with open(self.tmp_fname) as f:
soup = bs4.BeautifulSoup(f.read(), 'html.parser')
Expand Down Expand Up @@ -143,9 +143,9 @@ def test_escaping(self, get_issue_list, get_code):

get_issue_list.return_value = {issue_a: [issue_x]}

tmp_file = open(self.tmp_fname, 'w')
b_html.report(
self.manager, tmp_file, bandit.LOW, bandit.LOW)
with open(self.tmp_fname, 'w') as tmp_file:
b_html.report(
self.manager, tmp_file, bandit.LOW, bandit.LOW)

with open(self.tmp_fname) as f:
contents = f.read()
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/formatters/test_json.py
Expand Up @@ -75,9 +75,9 @@ def test_report(self, get_issue_list):
get_issue_list.return_value = collections.OrderedDict(
[(self.issue, self.candidates)])

tmp_file = open(self.tmp_fname, 'w')
b_json.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)
with open(self.tmp_fname, 'w') as tmp_file:
b_json.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)

with open(self.tmp_fname) as f:
data = json.loads(f.read())
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/formatters/test_screen.py
Expand Up @@ -82,9 +82,9 @@ def test_no_issues(self, get_issue_list):

get_issue_list.return_value = collections.OrderedDict()
with mock.patch('bandit.formatters.screen.do_print') as m:
tmp_file = open(self.tmp_fname, 'w')
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
self.assertIn('No issues identified.',
'\n'.join([str(a) for a in m.call_args]))

Expand Down Expand Up @@ -121,9 +121,9 @@ def test_report_nobaseline(self, get_issue_list):
with mock.patch(output_str_fn) as output_str:
output_str.return_value = 'ISSUE_OUTPUT_TEXT'

tmp_file = open(self.tmp_fname, 'w')
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)

calls = [mock.call(issue_a, '', lines=5),
mock.call(issue_b, '', lines=5)]
Expand All @@ -133,9 +133,9 @@ def test_report_nobaseline(self, get_issue_list):
# Validate that we're outputting all of the expected fields and the
# correct values
with mock.patch('bandit.formatters.screen.do_print') as m:
tmp_file = open(self.tmp_fname, 'w')
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)

data = '\n'.join([str(a) for a in m.call_args[0][0]])

Expand Down Expand Up @@ -195,9 +195,9 @@ def test_report_baseline(self, get_issue_list):
with mock.patch(output_str_fn) as output_str:
output_str.return_value = 'ISSUE_OUTPUT_TEXT'

tmp_file = open(self.tmp_fname, 'w')
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)

calls = [mock.call(issue_a, '', lines=5),
mock.call(issue_b, '', show_code=False,
Expand Down
23 changes: 12 additions & 11 deletions tests/unit/formatters/test_text.py
Expand Up @@ -77,8 +77,9 @@ def test_no_issues(self, get_issue_list):
self.manager.out_file = self.tmp_fname

get_issue_list.return_value = collections.OrderedDict()
tmp_file = open(self.tmp_fname, 'w')
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)

with open(self.tmp_fname) as f:
data = f.read()
Expand Down Expand Up @@ -117,9 +118,9 @@ def test_report_nobaseline(self, get_issue_list):
with mock.patch(output_str_fn) as output_str:
output_str.return_value = 'ISSUE_OUTPUT_TEXT'

tmp_file = open(self.tmp_fname, 'w')
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)

calls = [mock.call(issue_a, '', lines=5),
mock.call(issue_b, '', lines=5)]
Expand All @@ -128,9 +129,9 @@ def test_report_nobaseline(self, get_issue_list):

# Validate that we're outputting all of the expected fields and the
# correct values
tmp_file = open(self.tmp_fname, 'w')
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname) as f:
data = f.read()

Expand Down Expand Up @@ -182,9 +183,9 @@ def test_report_baseline(self, get_issue_list):
with mock.patch(output_str_fn) as output_str:
output_str.return_value = 'ISSUE_OUTPUT_TEXT'

tmp_file = open(self.tmp_fname, 'w')
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)
with open(self.tmp_fname, 'w') as tmp_file:
b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW,
lines=5)

calls = [mock.call(issue_a, '', lines=5),
mock.call(issue_b, '', show_code=False,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/formatters/test_xml.py
Expand Up @@ -69,9 +69,9 @@ def _xml_to_dict(self, t):
return d

def test_report(self):
tmp_file = open(self.tmp_fname, 'wb')
b_xml.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)
with open(self.tmp_fname, 'wb') as tmp_file:
b_xml.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)

with open(self.tmp_fname) as f:
data = self._xml_to_dict(ET.XML(f.read()))
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/formatters/test_yaml.py
Expand Up @@ -75,9 +75,9 @@ def test_report(self, get_issue_list):
get_issue_list.return_value = collections.OrderedDict(
[(self.issue, self.candidates)])

tmp_file = open(self.tmp_fname, 'w')
b_json.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)
with open(self.tmp_fname, 'w') as tmp_file:
b_json.report(self.manager, tmp_file, self.issue.severity,
self.issue.confidence)

with open(self.tmp_fname) as f:
data = yaml.load(f.read())
Expand Down

0 comments on commit 682c38b

Please sign in to comment.