]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #15539: Fix backup file creation in pindent.py on Windows
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 11 Jan 2013 20:16:15 +0000 (22:16 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 11 Jan 2013 20:16:15 +0000 (22:16 +0200)
Lib/test/test_tools.py
Tools/scripts/pindent.py

index 4756e4f6f77622e568c7a75a0c7be9265fac44fc..f60b4c7c91eb03df92bf6fb07d1fdf3176ec6ee8 100644 (file)
@@ -57,6 +57,7 @@ class PindentTests(unittest.TestCase):
         return '\n'.join(line.lstrip() for line in data.splitlines()) + '\n'
 
     def test_selftest(self):
+        self.maxDiff = None
         with temp_dir() as directory:
             data_path = os.path.join(directory, '_test.py')
             with open(self.script) as f:
index 26d81b1340ab8e1758fd374db8920aed964985bc..6e40d60b40d7da0187d59d7981dd7dc34fb9c746 100755 (executable)
@@ -76,6 +76,8 @@
 # - realign comments
 # - optionally do much more thorough reformatting, a la C indent
 
+from __future__ import print_function
+
 # Defaults
 STEPSIZE = 8
 TABSIZE = 8
@@ -370,6 +372,23 @@ def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
     return output.getvalue()
 # end def reformat_string
 
+def make_backup(filename):
+    import os, os.path
+    backup = filename + '~'
+    if os.path.lexists(backup):
+        try:
+            os.remove(backup)
+        except os.error:
+            print("Can't remove backup %r" % (backup,), file=sys.stderr)
+        # end try
+    # end if
+    try:
+        os.rename(filename, backup)
+    except os.error:
+        print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
+    # end try
+# end def make_backup
+
 def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
     with open(filename, 'r') as f:
         source = f.read()
@@ -377,10 +396,7 @@ def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
     result = complete_string(source, stepsize, tabsize, expandtabs)
     if source == result: return 0
     # end if
-    import os
-    try: os.rename(filename, filename + '~')
-    except os.error: pass
-    # end try
+    make_backup(filename)
     with open(filename, 'w') as f:
         f.write(result)
     # end with
@@ -394,10 +410,7 @@ def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = E
     result = delete_string(source, stepsize, tabsize, expandtabs)
     if source == result: return 0
     # end if
-    import os
-    try: os.rename(filename, filename + '~')
-    except os.error: pass
-    # end try
+    make_backup(filename)
     with open(filename, 'w') as f:
         f.write(result)
     # end with
@@ -411,10 +424,7 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
     result = reformat_string(source, stepsize, tabsize, expandtabs)
     if source == result: return 0
     # end if
-    import os
-    try: os.rename(filename, filename + '~')
-    except os.error: pass
-    # end try
+    make_backup(filename)
     with open(filename, 'w') as f:
         f.write(result)
     # end with