diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2011-07-26 15:57:08 (GMT) |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2011-07-26 15:57:08 (GMT) |
commit | 1322bd4971b9aee0a4a18ae1c12c6257308b4a52 (patch) | |
tree | 8bd8f8404930cc8d07993e490796d041346a35bb /Tools/scripts | |
parent | 83d21930ab45af7a205a888c897636e2cf053da4 (diff) | |
parent | 76748b7033cdd52a95d78d85f2ec15244e5d0fd8 (diff) | |
download | cpython-1322bd4971b9aee0a4a18ae1c12c6257308b4a52.zip cpython-1322bd4971b9aee0a4a18ae1c12c6257308b4a52.tar.gz cpython-1322bd4971b9aee0a4a18ae1c12c6257308b4a52.tar.bz2 |
Merge with 3.2 Issue #10639: reindent.py should not convert newlines.
reindent.py now will use the newline detected in the original file and will report an error if mixed newlines are encountered.
Diffstat (limited to 'Tools/scripts')
-rwxr-xr-x | Tools/scripts/reindent.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/Tools/scripts/reindent.py b/Tools/scripts/reindent.py index bb41520..b18993b 100755 --- a/Tools/scripts/reindent.py +++ b/Tools/scripts/reindent.py @@ -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: |