]> granicus.if.org Git - python/commitdiff
Fixes #10639: reindent.py should not convert newlines
authorJason R. Coombs <jaraco@jaraco.com>
Tue, 26 Jul 2011 15:18:40 +0000 (11:18 -0400)
committerJason R. Coombs <jaraco@jaraco.com>
Tue, 26 Jul 2011 15:18:40 +0000 (11:18 -0400)
reindent.py now will use the newline detected in the original file and will report an error if mixed newlines are encountered.

Misc/NEWS
Tools/scripts/reindent.py

index 662eedc8d8ae8d961c484f5e98daa52e90b09b67..d91a9932054ba604f34629047957c4ff90891d71 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -310,6 +310,12 @@ Build
   "make install" creates symlinks in --prefix bin for the "-32" files in the
   framework bin directory like the installer does.
 
+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
 -----
 
index bb4152076b7584d6906b7c7322709b5b909a9f4e..b18993b0c25b1c6946c734f9f1b458dda3ddcffe 100755 (executable)
@@ -35,7 +35,7 @@ tabnanny.py, reindent should do a good job.
 
 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.
 """
 
@@ -109,7 +109,7 @@ def check(file):
 
     if verbose:
         print("checking", file, "...", end=' ')
-    with  open(file, 'rb') as f:
+    with open(file, 'rb') as f:
         encoding, _ = tokenize.detect_encoding(f.readline)
     try:
         with open(file, encoding=encoding) as f:
@@ -118,6 +118,11 @@ def check(file):
         errprint("%s: I/O Error: %s" % (file, str(msg)))
         return
 
+    newline = r.newlines
+    if isinstance(newline, tuple):
+        errprint("%s: mixed newlines detected; cannot process file" % file)
+        return
+
     if r.run():
         if verbose:
             print("changed.")
@@ -129,7 +134,7 @@ def check(file):
                 shutil.copyfile(file, bak)
                 if verbose:
                     print("backed up", file, "to", bak)
-            with open(file, "w", encoding=encoding) as f:
+            with open(file, "w", encoding=encoding, newline=newline) as f:
                 r.write(f)
             if verbose:
                 print("wrote new", file)
@@ -177,6 +182,10 @@ class Reindenter:
         # 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):
         tokens = tokenize.generate_tokens(self.getline)
         for _token in tokens: