Skip to content

Commit

Permalink
py2/py3 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
wim glenn committed Jun 5, 2015
1 parent 16a1a15 commit 1057d5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 24 additions & 3 deletions isort/isort.py
Expand Up @@ -28,8 +28,10 @@

import codecs
import copy
import io
import itertools
import os
import re
from collections import namedtuple
from datetime import datetime
from difflib import unified_diff
Expand Down Expand Up @@ -83,6 +85,7 @@ def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, ch
self._section_comments = ["# " + value for key, value in itemsview(self.config) if
key.startswith('import_heading') and value]

self.file_encoding = 'utf-8'
file_name = file_path
self.file_path = file_path or ""
if file_path and not file_contents:
Expand All @@ -94,9 +97,9 @@ def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, ch
file_contents = None
else:
self.file_path = file_path
with open(file_path) as file_to_import_sort:
self.file_encoding = coding_check(file_path)
with codecs.open(file_path, encoding=self.file_encoding) as file_to_import_sort:
file_contents = file_to_import_sort.read()
file_contents = PY2 and file_contents.decode('utf8') or file_contents

if file_contents is None or ("isort:" + "skip_file") in file_contents:
return
Expand Down Expand Up @@ -159,7 +162,7 @@ def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, ch
elif write_to_stdout:
stdout.write(self.output)
elif file_name:
with codecs.open(self.file_path, encoding='utf-8', mode='w') as output_file:
with codecs.open(self.file_path, encoding=self.file_encoding, mode='w') as output_file:
output_file.write(self.output)

def _show_diff(self, file_contents):
Expand Down Expand Up @@ -746,3 +749,21 @@ def _parse(self):
self.comments['above']['from'].setdefault(module, []).insert(0, self.out_lines.pop(-1))
last = self.out_lines and self.out_lines[-1].rstrip() or ""
self.imports[self.place_module(module)][import_type].add(module)


def coding_check(fname, default='utf-8'):

# see https://www.python.org/dev/peps/pep-0263/
pattern = re.compile(br'coding[:=]\s*([-\w.]+)')

with io.open(fname, 'rb') as f:
for line_number, line in enumerate(f, 1):
groups = re.findall(pattern, line)
if groups:
coding = groups[0].decode('ascii')
break
if line_number > 2:
coding = default
break

return coding
4 changes: 2 additions & 2 deletions test_isort.py
Expand Up @@ -1341,8 +1341,8 @@ def test_other_file_encodings():
for encoding in ('latin1', 'utf8'):
tmp_fname = os.path.join(tmp_dir, 'test_{}.py'.format(encoding))
with codecs.open(tmp_fname, mode='w', encoding=encoding) as f:
file_contents = "# coding: {}'\ns = u'\u00E3'".format(encoding)
file_contents = "# coding: {}\ns = u'\u00E3'\n".format(encoding)
f.write(file_contents)
assert SortImports(file_path=tmp_fname).output == file_contents
assert SortImports(file_path=tmp_fname).output == file_contents
finally:
shutil.rmtree(tmp_dir, ignore_errors=True)

0 comments on commit 1057d5e

Please sign in to comment.