]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45231: update_file.py preserves end of line (GH-28411)
authorVictor Stinner <vstinner@python.org>
Fri, 17 Sep 2021 18:12:25 +0000 (20:12 +0200)
committerGitHub <noreply@github.com>
Fri, 17 Sep 2021 18:12:25 +0000 (20:12 +0200)
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.

Tools/scripts/update_file.py

index cfc4e2b1ab12a319ea659f5b5ce7fd0778fbfeb9..5e22486ec09e3432a4de3d6315a46540a2c6c188 100644 (file)
@@ -28,7 +28,19 @@ def updating_file_with_tmpfile(filename, tmpfile=None):
     elif os.path.isdir(tmpfile):
         tmpfile = os.path.join(tmpfile, filename + '.tmp')
 
-    with open(tmpfile, 'w') as outfile:
+    with open(filename, 'rb') as infile:
+        line = infile.readline()
+
+    if line.endswith(b'\r\n'):
+        newline = "\r\n"
+    elif line.endswith(b'\r'):
+        newline = "\r"
+    elif line.endswith(b'\n'):
+        newline = "\n"
+    else:
+        raise ValueError(f"unknown end of line: {filename}: {line!a}")
+
+    with open(tmpfile, 'w', newline=newline) as outfile:
         with open(filename) as infile:
             yield infile, outfile
     update_file_with_tmpfile(filename, tmpfile)