[Python-checkins] cpython (2.7): Fixes #10639: reindent.py should not convert newlines

jason.coombs python-checkins at python.org
Wed Jul 27 20:41:09 CEST 2011


http://hg.python.org/cpython/rev/f9cf55bbe9b9
changeset:   71536:f9cf55bbe9b9
branch:      2.7
parent:      71533:2229b0422369
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Wed Jul 27 14:05:37 2011 -0400
summary:
  Fixes #10639: reindent.py should not convert newlines
Backport of changeset 070dc6e359fb, reindent.py now will use the newline detected in the original file and will report an error if mixed newlines are encountered.

files:
  Misc/NEWS                 |   6 ++++++
  Tools/scripts/reindent.py |  17 ++++++++++++++---
  2 files changed, 20 insertions(+), 3 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -143,6 +143,12 @@
   functions (*BSD and OS X).  Also add new stat file flags for OS X
   (UF_HIDDEN and UF_COMPRESSED).
 
+Tools/Demos
+-----------
+
+- Issue #10639: reindent.py no longer converts newlines and will raise
+  an error if attempting to convert a file with mixed newlines.
+
 Tests
 -----
 
diff --git a/Tools/scripts/reindent.py b/Tools/scripts/reindent.py
--- a/Tools/scripts/reindent.py
+++ b/Tools/scripts/reindent.py
@@ -35,7 +35,7 @@
 
 The backup file is a copy of the one that is being reindented. The ".bak"
 file is generated with shutil.copy(), but some corner cases regarding
-user/group and permissions could leave the backup file more readable that
+user/group and permissions could leave the backup file more readable than
 you'd prefer. You can always use the --nobackup option to prevent this.
 """
 
@@ -44,6 +44,7 @@
 import tokenize
 import os, shutil
 import sys
+import io
 
 verbose    = 0
 recurse    = 0
@@ -108,13 +109,19 @@
     if verbose:
         print "checking", file, "...",
     try:
-        f = open(file)
+        f = io.open(file)
     except IOError, msg:
         errprint("%s: I/O Error: %s" % (file, str(msg)))
         return
 
     r = Reindenter(f)
     f.close()
+
+    newline = r.newlines
+    if isinstance(newline, tuple):
+        errprint("%s: mixed newlines detected; cannot process file" % file)
+        return
+
     if r.run():
         if verbose:
             print "changed."
@@ -126,7 +133,7 @@
                 shutil.copyfile(file, bak)
                 if verbose:
                     print "backed up", file, "to", bak
-            f = open(file, "w")
+            f = io.open(file, "w", newline=newline)
             r.write(f)
             f.close()
             if verbose:
@@ -173,6 +180,10 @@
         # indeed, they're our headache!
         self.stats = []
 
+        # Save the newlines found in the file so they can be used to
+        #  create output without mutating the newlines.
+        self.newlines = f.newlines
+
     def run(self):
         tokenize.tokenize(self.getline, self.tokeneater)
         # Remove trailing empty lines.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list