[Python-checkins] cpython (2.7): Issue #15539: Fix backup file creation in pindent.py on Windows

serhiy.storchaka python-checkins at python.org
Fri Jan 11 21:23:43 CET 2013


http://hg.python.org/cpython/rev/2d0c5f97af48
changeset:   81423:2d0c5f97af48
branch:      2.7
parent:      81420:0d7a8a4d6f30
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Jan 11 22:16:15 2013 +0200
summary:
  Issue #15539: Fix backup file creation in pindent.py on Windows

files:
  Lib/test/test_tools.py   |   1 +
  Tools/scripts/pindent.py |  34 ++++++++++++++++++---------
  2 files changed, 23 insertions(+), 12 deletions(-)


diff --git a/Lib/test/test_tools.py b/Lib/test/test_tools.py
--- a/Lib/test/test_tools.py
+++ b/Lib/test/test_tools.py
@@ -57,6 +57,7 @@
         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:
diff --git a/Tools/scripts/pindent.py b/Tools/scripts/pindent.py
--- a/Tools/scripts/pindent.py
+++ b/Tools/scripts/pindent.py
@@ -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 @@
     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 @@
     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 @@
     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 @@
     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

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


More information about the Python-checkins mailing list