Skip to content

Commit 7787615

Browse files
committed
Make sure to write new_text on rewriting newlines
Thanks @takluyver for review
1 parent cb502ae commit 7787615

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Diff for: libmodernize/main.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ class LFPreservingRefactoringTool(StdoutRefactoringTool):
2424
def write_file(self, new_text, filename, old_text, encoding):
2525
# detect linefeeds
2626
lineends = {'\n':0, '\r\n':0, '\r':0}
27-
lines = []
2827
for line in open(filename, 'rb'):
2928
if line.endswith('\r\n'):
3029
lineends['\r\n'] += 1
3130
elif line.endswith('\n'):
3231
lineends['\n'] += 1
3332
elif line.endswith('\r'):
3433
lineends['\r'] += 1
35-
lines.append(line.rstrip('\r\n'))
3634
super(LFPreservingRefactoringTool, self).write_file(
3735
new_text, filename, old_text, encoding)
3836
# detect if line ends are consistent in source file
@@ -41,8 +39,8 @@ def write_file(self, new_text, filename, old_text, encoding):
4139
newline = [x for x in lineends if lineends[x] != 0][0]
4240
if os.linesep != newline:
4341
with open(filename, 'wb') as f:
44-
for line in lines:
45-
f.write(line)
42+
for line in new_text.splitlines():
43+
f.write(line + newline)
4644
self.log_debug('fixed %s linefeeds back to %s',
4745
filename, newline)
4846

0 commit comments

Comments
 (0)