mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
Issue #15539: Fix a backup file creation in pindent.py on Windows.
This commit is contained in:
parent
5e12bb728f
commit
a3a01b6ac3
2 changed files with 21 additions and 12 deletions
|
@ -58,6 +58,7 @@ class PindentTests(unittest.TestCase):
|
||||||
return '\n'.join(line.lstrip() for line in data.splitlines()) + '\n'
|
return '\n'.join(line.lstrip() for line in data.splitlines()) + '\n'
|
||||||
|
|
||||||
def test_selftest(self):
|
def test_selftest(self):
|
||||||
|
self.maxDiff = None
|
||||||
with temp_dir() as directory:
|
with temp_dir() as directory:
|
||||||
data_path = os.path.join(directory, '_test.py')
|
data_path = os.path.join(directory, '_test.py')
|
||||||
with open(self.script) as f:
|
with open(self.script) as f:
|
||||||
|
|
|
@ -370,6 +370,23 @@ def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
|
||||||
return output.getvalue()
|
return output.getvalue()
|
||||||
# end def reformat_string
|
# 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):
|
def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
|
||||||
with open(filename, 'r') as f:
|
with open(filename, 'r') as f:
|
||||||
source = f.read()
|
source = f.read()
|
||||||
|
@ -377,10 +394,7 @@ def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
|
||||||
result = complete_string(source, stepsize, tabsize, expandtabs)
|
result = complete_string(source, stepsize, tabsize, expandtabs)
|
||||||
if source == result: return 0
|
if source == result: return 0
|
||||||
# end if
|
# end if
|
||||||
import os
|
make_backup(filename)
|
||||||
try: os.rename(filename, filename + '~')
|
|
||||||
except os.error: pass
|
|
||||||
# end try
|
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(result)
|
f.write(result)
|
||||||
# end with
|
# end with
|
||||||
|
@ -394,10 +408,7 @@ def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = E
|
||||||
result = delete_string(source, stepsize, tabsize, expandtabs)
|
result = delete_string(source, stepsize, tabsize, expandtabs)
|
||||||
if source == result: return 0
|
if source == result: return 0
|
||||||
# end if
|
# end if
|
||||||
import os
|
make_backup(filename)
|
||||||
try: os.rename(filename, filename + '~')
|
|
||||||
except os.error: pass
|
|
||||||
# end try
|
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(result)
|
f.write(result)
|
||||||
# end with
|
# end with
|
||||||
|
@ -411,10 +422,7 @@ def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs =
|
||||||
result = reformat_string(source, stepsize, tabsize, expandtabs)
|
result = reformat_string(source, stepsize, tabsize, expandtabs)
|
||||||
if source == result: return 0
|
if source == result: return 0
|
||||||
# end if
|
# end if
|
||||||
import os
|
make_backup(filename)
|
||||||
try: os.rename(filename, filename + '~')
|
|
||||||
except os.error: pass
|
|
||||||
# end try
|
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(result)
|
f.write(result)
|
||||||
# end with
|
# end with
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue