diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2019-03-06 01:55:14 -0800 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2019-03-06 01:55:14 -0800 |
commit | 83c3b63b15b478889a498c5d8fd19f22f8402f6e (patch) | |
tree | d9cd6bec39280448ad5d0c6cca4dc608a6df33a3 | |
parent | 9f89ff822f791a4df015da33036902ecd911be65 (diff) | |
download | isort-83c3b63b15b478889a498c5d8fd19f22f8402f6e.tar.gz |
Allow fallback to OS encoding
-rw-r--r-- | isort/isort.py | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/isort/isort.py b/isort/isort.py index bf18b249..10ab30ef 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -106,13 +106,25 @@ class SortImports(object): file_contents = file_to_import_sort.read() self.file_path = file_path self.file_encoding = file_encoding + encoding_success = True except UnicodeDecodeError: - file_contents = None - self.skipped = True - if self.config['verbose']: - print("WARNING: {} was skipped as it " - "couldn't be opened with the given {} encoding".format(file_path, - self.file_encoding)) + encoding_success = False + + if not encoding_success: + with io.open(file_path, new_line='') as file_to_import_sort: + try: + file_contents = file_to_import_sort.read() + self.file_path = file_path + self.file_encoding = file_to_import_sort.encoding + except UnicodeDecodeError: + encoding_success = False + file_contents = None + self.skipped = True + if self.config['verbose']: + print("WARNING: {} was skipped as it couldn't be opened with the given " + "{} encoding or {} fallback encoding".format(file_path, + self.file_encoding, + file_to_import_sort.encoding)) if file_contents is None or ("isort:" + "skip_file") in file_contents: self.skipped = True |