Use open built-in to open files

* This makes Python 3 not raise UnicodeDecodeError when reading files
This commit is contained in:
Sviatoslav Sydorenko 2016-12-29 18:53:57 +02:00
parent 9573c13884
commit 51f8cc8ff0
6 changed files with 9 additions and 14 deletions

View file

@ -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

View file

@ -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

View file

@ -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))

View file

@ -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:

View file

@ -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)

View file

@ -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