Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1829bb4

Browse files
Issue #15539: Fix a backup file creation in pindent.py on Windows.
2 parents cba18fe + b4fb2e2 commit 1829bb4

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

Lib/test/test_tools.py

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

6161
def test_selftest(self):
62+
self.maxDiff = None
6263
with temp_dir() as directory:
6364
data_path = os.path.join(directory, '_test.py')
6465
with open(self.script) as f:

Tools/scripts/pindent.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,17 +370,31 @@ def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
370370
return output.getvalue()
371371
# end def reformat_string
372372

373+
def make_backup(filename):
374+
import os, os.path
375+
backup = filename + '~'
376+
if os.path.lexists(backup):
377+
try:
378+
os.remove(backup)
379+
except OSError:
380+
print("Can't remove backup %r" % (backup,), file=sys.stderr)
381+
# end try
382+
# end if
383+
try:
384+
os.rename(filename, backup)
385+
except OSError:
386+
print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
387+
# end try
388+
# end def make_backup
389+
373390
def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
374391
with open(filename, 'r') as f:
375392
source = f.read()
376393
# end with
377394
result = complete_string(source, stepsize, tabsize, expandtabs)
378395
if source == result: return 0
379396
# end if
380-
import os
381-
try: os.rename(filename, filename + '~')
382-
except OSError: pass
383-
# end try
397+
make_backup(filename)
384398
with open(filename, 'w') as f:
385399
f.write(result)
386400
# end with
@@ -394,10 +408,7 @@ def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = E
394408
result = delete_string(source, stepsize, tabsize, expandtabs)
395409
if source == result: return 0
396410
# end if
397-
import os
398-
try: os.rename(filename, filename + '~')
399-
except OSError: pass
400-
# end try
411+
make_backup(filename)
401412
with open(filename, 'w') as f:
402413
f.write(result)
403414
# end with
@@ -411,10 +422,7 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
411422
result = reformat_string(source, stepsize, tabsize, expandtabs)
412423
if source == result: return 0
413424
# end if
414-
import os
415-
try: os.rename(filename, filename + '~')
416-
except OSError: pass
417-
# end try
425+
make_backup(filename)
418426
with open(filename, 'w') as f:
419427
f.write(result)
420428
# end with

0 commit comments

Comments
 (0)