Skip to content

Commit

Permalink
Make sure to write new_text on rewriting newlines
Browse files Browse the repository at this point in the history
Thanks @takluyver for review
  • Loading branch information
techtonik committed Oct 19, 2015
1 parent cb502ae commit 7787615
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions libmodernize/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class LFPreservingRefactoringTool(StdoutRefactoringTool):
def write_file(self, new_text, filename, old_text, encoding):
# detect linefeeds
lineends = {'\n':0, '\r\n':0, '\r':0}
lines = []
for line in open(filename, 'rb'):
if line.endswith('\r\n'):
lineends['\r\n'] += 1
elif line.endswith('\n'):
lineends['\n'] += 1
elif line.endswith('\r'):
lineends['\r'] += 1
lines.append(line.rstrip('\r\n'))
super(LFPreservingRefactoringTool, self).write_file(
new_text, filename, old_text, encoding)
# detect if line ends are consistent in source file
Expand All @@ -41,8 +39,8 @@ def write_file(self, new_text, filename, old_text, encoding):
newline = [x for x in lineends if lineends[x] != 0][0]
if os.linesep != newline:
with open(filename, 'wb') as f:
for line in lines:
f.write(line)
for line in new_text.splitlines():
f.write(line + newline)
self.log_debug('fixed %s linefeeds back to %s',
filename, newline)

Expand Down

1 comment on commit 7787615

@daira
Copy link
Contributor

@daira daira commented on 7787615 Oct 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I didn't see the effect of this patch in the github code comparison. That can happen sometimes if a branch is force-pushed, I think.

Please sign in to comment.