Fix resource warnings
diff --git a/pre_commit_hooks/autopep8_wrapper.py b/pre_commit_hooks/autopep8_wrapper.py
index b2d2d0c..087fe3f 100644
--- a/pre_commit_hooks/autopep8_wrapper.py
+++ b/pre_commit_hooks/autopep8_wrapper.py
@@ -14,7 +14,8 @@
retv = 0
for filename in args.files:
- original_contents = io.open(filename, encoding='UTF-8').read()
+ with io.open(filename, encoding='UTF-8') as f:
+ original_contents = f.read()
new_contents = autopep8.fix_code(original_contents, args)
if original_contents != new_contents:
print('Fixing {}'.format(filename))
diff --git a/pre_commit_hooks/check_ast.py b/pre_commit_hooks/check_ast.py
index 1090300..ded65e4 100644
--- a/pre_commit_hooks/check_ast.py
+++ b/pre_commit_hooks/check_ast.py
@@ -18,7 +18,8 @@
for filename in args.filenames:
try:
- ast.parse(open(filename, 'rb').read(), filename=filename)
+ with open(filename, 'rb') as f:
+ ast.parse(f.read(), filename=filename)
except SyntaxError:
print('{}: failed parsing with {} {}:'.format(
filename,
diff --git a/pre_commit_hooks/check_builtin_literals.py b/pre_commit_hooks/check_builtin_literals.py
index b7f0c00..4a4b9ce 100644
--- a/pre_commit_hooks/check_builtin_literals.py
+++ b/pre_commit_hooks/check_builtin_literals.py
@@ -47,7 +47,8 @@
def check_file_for_builtin_type_constructors(filename, ignore=None, allow_dict_kwargs=True):
- tree = ast.parse(open(filename, 'rb').read(), filename=filename)
+ with open(filename, 'rb') as f:
+ tree = ast.parse(f.read(), filename=filename)
visitor = BuiltinTypeVisitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
visitor.visit(tree)
return visitor.builtin_type_calls
diff --git a/pre_commit_hooks/check_docstring_first.py b/pre_commit_hooks/check_docstring_first.py
index 0896812..9988378 100644
--- a/pre_commit_hooks/check_docstring_first.py
+++ b/pre_commit_hooks/check_docstring_first.py
@@ -58,7 +58,8 @@
retv = 0
for filename in args.filenames:
- contents = io.open(filename, encoding='UTF-8').read()
+ with io.open(filename, encoding='UTF-8') as f:
+ contents = f.read()
retv |= check_docstring_first(contents, filename=filename)
return retv
diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py
index 81591dd..5d32277 100644
--- a/pre_commit_hooks/debug_statement_hook.py
+++ b/pre_commit_hooks/debug_statement_hook.py
@@ -36,7 +36,8 @@
def check_file(filename):
try:
- ast_obj = ast.parse(open(filename, 'rb').read(), filename=filename)
+ with open(filename, 'rb') as f:
+ ast_obj = ast.parse(f.read(), filename=filename)
except SyntaxError:
print('{} - Could not parse ast'.format(filename))
print()
diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py
index f73f09d..c432682 100644
--- a/pre_commit_hooks/string_fixer.py
+++ b/pre_commit_hooks/string_fixer.py
@@ -32,7 +32,8 @@
def fix_strings(filename):
- contents = io.open(filename, encoding='UTF-8').read()
+ with io.open(filename, encoding='UTF-8') as f:
+ contents = f.read()
line_offsets = get_line_offsets_by_line_no(contents)
# Basically a mutable string