summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2019-03-06 01:55:14 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2019-03-06 02:15:44 -0800
commit89859f152bde78cd1aa0fd86ec5265a76d4beb72 (patch)
tree51126d18ac64e387b4b768e0a1d4c1e8fd615065
parent16daa7bcf90172408f8a8742773364ff2e41f7f3 (diff)
downloadisort-89859f152bde78cd1aa0fd86ec5265a76d4beb72.tar.gz
Allow fallback to OS encoding
-rw-r--r--isort/isort.py24
1 files changed, 18 insertions, 6 deletions
diff --git a/isort/isort.py b/isort/isort.py
index 76eac83d..60992bf7 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -103,13 +103,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