summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2014-04-17 00:40:06 -0400
committerTimothy Crosley <timothy.crosley@gmail.com>2014-04-17 00:40:06 -0400
commit81206a2485f686dbb4504f6bcb3e6dfaf29c7955 (patch)
tree2404d03c3798348dfa9ab5750db430cb6ff29cf6
parent7b231919b70e1508c196cfac0d505079bc197071 (diff)
downloadisort-81206a2485f686dbb4504f6bcb3e6dfaf29c7955.tar.gz
Strip top file comments before checking syntax to avoid issue #141
-rw-r--r--isort/isort.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/isort/isort.py b/isort/isort.py
index e428f2c4..121a3253 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -125,12 +125,12 @@ class SortImports(object):
self.output = "\n".join(self.out_lines)
if self.config.get('atomic', False):
try:
- compile(self.output, self.file_path, 'exec', 0, 1)
+ compile(self._strip_top_comments(self.out_lines), self.file_path, 'exec', 0, 1)
except SyntaxError:
self.output = file_contents
self.incorrectly_sorted = True
try:
- compile(file_contents, self.file_path, 'exec', 0, 1)
+ compile(self._strip_top_comments(self.in_lines), self.file_path, 'exec', 0, 1)
print("ERROR: {0} isort would have introduced syntax errors, please report to the project!". \
format(self.file_path), file=stderr)
except SyntaxError:
@@ -155,6 +155,14 @@ class SortImports(object):
with codecs.open(self.file_path, encoding='utf-8', mode='w') as output_file:
output_file.write(self.output)
+ @staticmethod
+ def _strip_top_comments(lines):
+ """Strips # comments that exist at the top of the given lines"""
+ lines = copy.copy(lines)
+ while lines and lines[0].startswith("#"):
+ lines = lines[1:]
+ return "\n".join(lines)
+
def _should_skip(self, filename):
"""Returns True if the file should be skipped based on the loaded settings."""
if filename in self.config['skip']: