Skip to content

Commit dfae912

Browse files
Issue #15539: Fix backup file creation in pindent.py on Windows
1 parent 8b41c2e commit dfae912

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

Lib/test/test_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def lstriplines(self, data):
5757
return '\n'.join(line.lstrip() for line in data.splitlines()) + '\n'
5858

5959
def test_selftest(self):
60+
self.maxDiff = None
6061
with temp_dir() as directory:
6162
data_path = os.path.join(directory, '_test.py')
6263
with open(self.script) as f:

Tools/scripts/pindent.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
# - realign comments
7777
# - optionally do much more thorough reformatting, a la C indent
7878

79+
from __future__ import print_function
80+
7981
# Defaults
8082
STEPSIZE = 8
8183
TABSIZE = 8
@@ -370,17 +372,31 @@ def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
370372
return output.getvalue()
371373
# end def reformat_string
372374

375+
def make_backup(filename):
376+
import os, os.path
377+
backup = filename + '~'
378+
if os.path.lexists(backup):
379+
try:
380+
os.remove(backup)
381+
except os.error:
382+
print("Can't remove backup %r" % (backup,), file=sys.stderr)
383+
# end try
384+
# end if
385+
try:
386+
os.rename(filename, backup)
387+
except os.error:
388+
print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
389+
# end try
390+
# end def make_backup
391+
373392
def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
374393
with open(filename, 'r') as f:
375394
source = f.read()
376395
# end with
377396
result = complete_string(source, stepsize, tabsize, expandtabs)
378397
if source == result: return 0
379398
# end if
380-
import os
381-
try: os.rename(filename, filename + '~')
382-
except os.error: pass
383-
# end try
399+
make_backup(filename)
384400
with open(filename, 'w') as f:
385401
f.write(result)
386402
# end with
@@ -394,10 +410,7 @@ def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = E
394410
result = delete_string(source, stepsize, tabsize, expandtabs)
395411
if source == result: return 0
396412
# end if
397-
import os
398-
try: os.rename(filename, filename + '~')
399-
except os.error: pass
400-
# end try
413+
make_backup(filename)
401414
with open(filename, 'w') as f:
402415
f.write(result)
403416
# end with
@@ -411,10 +424,7 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
411424
result = reformat_string(source, stepsize, tabsize, expandtabs)
412425
if source == result: return 0
413426
# end if
414-
import os
415-
try: os.rename(filename, filename + '~')
416-
except os.error: pass
417-
# end try
427+
make_backup(filename)
418428
with open(filename, 'w') as f:
419429
f.write(result)
420430
# end with

0 commit comments

Comments
 (0)