Skip to content

Commit

Permalink
Merge pull request #279 from nvtkaszpir/fix/non-utf8-opens
Browse files Browse the repository at this point in the history
Open files as UTF-8
  • Loading branch information
asottile authored Mar 26, 2018
2 parents 16ff195 + 54c0f8c commit 588232e
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
/venv*
coverage-html
dist
# SublimeText project/workspace files
*.sublime-*
4 changes: 2 additions & 2 deletions pre_commit_hooks/autopep8_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def main(argv=None):

retv = 0
for filename in args.files:
original_contents = io.open(filename).read()
original_contents = io.open(filename, encoding='UTF-8').read()
new_contents = autopep8.fix_code(original_contents, args)
if original_contents != new_contents:
print('Fixing {}'.format(filename))
retv = 1
with io.open(filename, 'w') as output_file:
with io.open(filename, 'w', encoding='UTF-8') as output_file:
output_file.write(new_contents)

return retv
Expand Down
2 changes: 1 addition & 1 deletion pre_commit_hooks/check_docstring_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def main(argv=None):
retv = 0

for filename in args.filenames:
contents = io.open(filename).read()
contents = io.open(filename, encoding='UTF-8').read()
retv |= check_docstring_first(contents, filename=filename)

return retv
4 changes: 2 additions & 2 deletions pre_commit_hooks/string_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_line_offsets_by_line_no(src):


def fix_strings(filename):
contents = io.open(filename).read()
contents = io.open(filename, encoding='UTF-8').read()
line_offsets = get_line_offsets_by_line_no(contents)

# Basically a mutable string
Expand All @@ -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 io.open(filename, 'w', encoding='UTF-8') as write_handle:
write_handle.write(new_contents)
return 1
else:
Expand Down
2 changes: 1 addition & 1 deletion testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,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 io.open(filename, 'w', encoding='UTF-8', newline='') as file_obj:
file_obj.write(contents)
4 changes: 2 additions & 2 deletions tests/meta_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def _assert_parseable_in_old_pre_commit(hooks):


def test_legacy_hooks():
with io.open('hooks.yaml') as legacy_file:
with io.open('hooks.yaml', encoding='UTF-8') as legacy_file:
legacy = yaml.load(legacy_file.read())
with io.open('.pre-commit-hooks.yaml') as hooks_file:
with io.open('.pre-commit-hooks.yaml', encoding='UTF-8') as hooks_file:
hooks = yaml.load(hooks_file.read())

# The same set of hooks should be defined in both files
Expand Down

0 comments on commit 588232e

Please sign in to comment.