bpo-6699: IDLE: Warn the user if a file will be overwritten when saving

Co-Authored-By: Guilherme Polo <ggpolo@gmail.com>
Co-Authored-By: Priya Pappachan <priyapappachan010@gmail.com>
This commit is contained in:
Zackery Spytz 2019-12-12 03:18:31 -07:00
parent 7772b1af5e
commit fe3e6d1b76
2 changed files with 21 additions and 0 deletions

View File

@ -148,14 +148,20 @@ class IOBinding:
self.filename_change_hook = hook self.filename_change_hook = hook
filename = None filename = None
file_timestamp = None
dirname = None dirname = None
def set_filename(self, filename): def set_filename(self, filename):
if filename and os.path.isdir(filename): if filename and os.path.isdir(filename):
self.filename = None self.filename = None
self.file_timestamp = None
self.dirname = filename self.dirname = filename
else: else:
self.filename = filename self.filename = filename
if filename is not None:
self.file_timestamp = os.stat(filename).st_mtime
else:
self.file_timestamp = None
self.dirname = None self.dirname = None
self.set_saved(1) self.set_saved(1)
if self.filename_change_hook: if self.filename_change_hook:
@ -339,7 +345,21 @@ class IOBinding:
if not self.filename: if not self.filename:
self.save_as(event) self.save_as(event)
else: else:
# Check the time of most recent content modification so the
# user doesn't accidentally overwrite a newer version of the file.
if self.file_timestamp != os.stat(self.filename).st_mtime:
confirm = tkMessageBox.askokcancel(
title="File has changed",
message=(
"The file has changed on disk since reading it!\n\n"
"Do you really want to overwrite it?"),
default=tkMessageBox.CANCEL,
parent=self.text)
if not confirm:
return
if self.writefile(self.filename): if self.writefile(self.filename):
self.file_timestamp = os.stat(self.filename).st_mtime
self.set_saved(True) self.set_saved(True)
try: try:
self.editwin.store_file_breaks() self.editwin.store_file_breaks()

View File

@ -0,0 +1 @@
Warn the user if a file will be overwritten when saving.