From 51f8cc8ff0a6180a6c43d3b1409f868d612536a9 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Thu, 29 Dec 2016 18:53:57 +0200 Subject: [PATCH] Use open built-in to open files * This makes Python 3 not raise UnicodeDecodeError when reading files --- pre_commit_hooks/autopep8_wrapper.py | 5 ++--- pre_commit_hooks/check_docstring_first.py | 2 +- pre_commit_hooks/check_xml.py | 3 +-- pre_commit_hooks/string_fixer.py | 4 ++-- testing/util.py | 3 +-- tests/readme_test.py | 6 ++---- 6 files changed, 9 insertions(+), 14 deletions(-) diff --git a/pre_commit_hooks/autopep8_wrapper.py b/pre_commit_hooks/autopep8_wrapper.py index f6f55fb..aa42ec4 100644 --- a/pre_commit_hooks/autopep8_wrapper.py +++ b/pre_commit_hooks/autopep8_wrapper.py @@ -2,7 +2,6 @@ from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals -import io import sys import autopep8 @@ -14,12 +13,12 @@ def main(argv=None): retv = 0 for filename in args.files: - original_contents = io.open(filename).read() + original_contents = open(filename).read() new_contents = autopep8.fix_code(original_contents, args) if original_contents != new_contents: print('Fixing {0}'.format(filename)) retv = 1 - with io.open(filename, 'w') as output_file: + with open(filename, 'w') as output_file: output_file.write(new_contents) return retv diff --git a/pre_commit_hooks/check_docstring_first.py b/pre_commit_hooks/check_docstring_first.py index da5425d..9743312 100644 --- a/pre_commit_hooks/check_docstring_first.py +++ b/pre_commit_hooks/check_docstring_first.py @@ -57,7 +57,7 @@ def main(argv=None): retv = 0 for filename in args.filenames: - contents = io.open(filename).read() + contents = open(filename).read() retv |= check_docstring_first(contents, filename=filename) return retv diff --git a/pre_commit_hooks/check_xml.py b/pre_commit_hooks/check_xml.py index 926f6e9..05edad1 100644 --- a/pre_commit_hooks/check_xml.py +++ b/pre_commit_hooks/check_xml.py @@ -3,7 +3,6 @@ from __future__ import print_function from __future__ import unicode_literals import argparse -import io import sys import xml.sax @@ -16,7 +15,7 @@ def check_xml(argv=None): retval = 0 for filename in args.filenames: try: - with io.open(filename, 'rb') as xml_file: + with open(filename, 'rb') as xml_file: xml.sax.parse(xml_file, xml.sax.ContentHandler()) except xml.sax.SAXException as exc: print('{0}: Failed to xml parse ({1})'.format(filename, exc)) diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py index 9ef7a37..30fd6a2 100644 --- a/pre_commit_hooks/string_fixer.py +++ b/pre_commit_hooks/string_fixer.py @@ -32,7 +32,7 @@ def get_line_offsets_by_line_no(src): def fix_strings(filename): - contents = io.open(filename).read() + contents = open(filename).read() line_offsets = get_line_offsets_by_line_no(contents) # Basically a mutable string @@ -52,7 +52,7 @@ def fix_strings(filename): new_contents = ''.join(splitcontents) if contents != new_contents: - with io.open(filename, 'w') as write_handle: + with open(filename, 'w') as write_handle: write_handle.write(new_contents) return 1 else: diff --git a/testing/util.py b/testing/util.py index 837a3cb..ad179f6 100644 --- a/testing/util.py +++ b/testing/util.py @@ -1,7 +1,6 @@ from __future__ import absolute_import from __future__ import unicode_literals -import io import os.path @@ -14,5 +13,5 @@ def get_resource_path(path): def write_file(filename, contents): """Hax because coveragepy chokes on nested context managers.""" - with io.open(filename, 'w', newline='') as file_obj: + with open(filename, 'w', newline='') as file_obj: file_obj.write(contents) diff --git a/tests/readme_test.py b/tests/readme_test.py index d479d42..b729eee 100644 --- a/tests/readme_test.py +++ b/tests/readme_test.py @@ -1,13 +1,11 @@ from __future__ import absolute_import from __future__ import unicode_literals -import io - import yaml def test_readme_contains_all_hooks(): - readme_contents = io.open('README.md').read() - hooks = yaml.load(io.open('hooks.yaml').read()) + readme_contents = open('README.md').read() + hooks = yaml.load(open('hooks.yaml').read()) for hook in hooks: assert '`{0}`'.format(hook['id']) in readme_contents