[Python-checkins] bpo-45231: update_file.py preserves end of line (GH-28411)

vstinner webhook-mailer at python.org
Fri Sep 17 14:12:30 EDT 2021


https://github.com/python/cpython/commit/c5a677da9e7b2b2aa43b0b6dfb3813c0212379c0
commit: c5a677da9e7b2b2aa43b0b6dfb3813c0212379c0
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-09-17T20:12:25+02:00
summary:

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.

files:
M Tools/scripts/update_file.py

diff --git a/Tools/scripts/update_file.py b/Tools/scripts/update_file.py
index cfc4e2b1ab12a..5e22486ec09e3 100644
--- a/Tools/scripts/update_file.py
+++ b/Tools/scripts/update_file.py
@@ -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)



More information about the Python-checkins mailing list