Fix resource warnings · lircs/pre-commit-hooks@5dc306b · GitHub
Skip to content

Commit 5dc306b

Browse files
committed
Fix resource warnings
1 parent a193eab commit 5dc306b

7 files changed

Lines changed: 14 additions & 7 deletions

File tree

pre_commit_hooks/autopep8_wrapper.py

Lines changed: 2 additions & 1 deletion

pre_commit_hooks/check_ast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def check_ast(argv=None):
1818
for filename in args.filenames:
1919

2020
try:
21-
ast.parse(open(filename, 'rb').read(), filename=filename)
21+
with open(filename, 'rb') as f:
22+
ast.parse(f.read(), filename=filename)
2223
except SyntaxError:
2324
print('{}: failed parsing with {} {}:'.format(
2425
filename,

pre_commit_hooks/check_builtin_literals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def visit_Call(self, node):
4747

4848

4949
def check_file_for_builtin_type_constructors(filename, ignore=None, allow_dict_kwargs=True):
50-
tree = ast.parse(open(filename, 'rb').read(), filename=filename)
50+
with open(filename, 'rb') as f:
51+
tree = ast.parse(f.read(), filename=filename)
5152
visitor = BuiltinTypeVisitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
5253
visitor.visit(tree)
5354
return visitor.builtin_type_calls

pre_commit_hooks/check_docstring_first.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def main(argv=None):
5858
retv = 0
5959

6060
for filename in args.filenames:
61-
contents = io.open(filename, encoding='UTF-8').read()
61+
with io.open(filename, encoding='UTF-8') as f:
62+
contents = f.read()
6263
retv |= check_docstring_first(contents, filename=filename)
6364

6465
return retv

pre_commit_hooks/debug_statement_hook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def visit_Call(self, node):
3636

3737
def check_file(filename):
3838
try:
39-
ast_obj = ast.parse(open(filename, 'rb').read(), filename=filename)
39+
with open(filename, 'rb') as f:
40+
ast_obj = ast.parse(f.read(), filename=filename)
4041
except SyntaxError:
4142
print('{} - Could not parse ast'.format(filename))
4243
print()

pre_commit_hooks/string_fixer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def get_line_offsets_by_line_no(src):
3232

3333

3434
def fix_strings(filename):
35-
contents = io.open(filename, encoding='UTF-8').read()
35+
with io.open(filename, encoding='UTF-8') as f:
36+
contents = f.read()
3637
line_offsets = get_line_offsets_by_line_no(contents)
3738

3839
# Basically a mutable string

tests/check_builtin_literals_test.py

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)