Skip to content

Commit c5a677d

Browse files
authored
bpo-45231: update_file.py preserves end of line (GH-28411)
The update_file.py tool now preserves the end of line of the updated file. Fix the "make regen-frozen" command: it no longer changes the end of line of PCbuild/ files on Unix. Git changes the end of line depending on the platform.
1 parent 778b075 commit c5a677d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Tools/scripts/update_file.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ def updating_file_with_tmpfile(filename, tmpfile=None):
2828
elif os.path.isdir(tmpfile):
2929
tmpfile = os.path.join(tmpfile, filename + '.tmp')
3030

31-
with open(tmpfile, 'w') as outfile:
31+
with open(filename, 'rb') as infile:
32+
line = infile.readline()
33+
34+
if line.endswith(b'\r\n'):
35+
newline = "\r\n"
36+
elif line.endswith(b'\r'):
37+
newline = "\r"
38+
elif line.endswith(b'\n'):
39+
newline = "\n"
40+
else:
41+
raise ValueError(f"unknown end of line: {filename}: {line!a}")
42+
43+
with open(tmpfile, 'w', newline=newline) as outfile:
3244
with open(filename) as infile:
3345
yield infile, outfile
3446
update_file_with_tmpfile(filename, tmpfile)

0 commit comments

Comments
 (0)