Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/idlelib/iomenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def set_filename_change_hook(self, hook):
self.filename_change_hook = hook

filename = None
file_timestamp = None
dirname = None

def set_filename(self, filename):
Expand Down Expand Up @@ -127,6 +128,7 @@ def loadfile(self, filename):
chars = f.read()
fileencoding = f.encoding
eol_convention = f.newlines
file_timestamp = os.stat(filename).st_mtime
converted = False
except (UnicodeDecodeError, SyntaxError):
# Wait for the editor window to appear
Expand All @@ -142,6 +144,7 @@ def loadfile(self, filename):
chars = f.read()
fileencoding = f.encoding
eol_convention = f.newlines
file_timestamp = os.stat(filename).st_mtime
converted = True
except OSError as err:
messagebox.showerror("I/O Error", str(err), parent=self.text)
Expand Down Expand Up @@ -170,6 +173,7 @@ def loadfile(self, filename):
self.text.insert("1.0", chars)
self.reset_undo()
self.set_filename(filename)
self.file_timestamp = file_timestamp
if converted:
# We need to save the conversion results first
# before being able to execute the code
Expand Down Expand Up @@ -206,7 +210,26 @@ def save(self, event):
if not self.filename:
self.save_as(event)
else:
# Check the time of most recent content modification so the
# user doesn't accidentally overwrite a newer version of the file.
try:
file_timestamp = os.stat(self.filename).st_mtime
except OSError:
pass
else:
if self.file_timestamp != file_timestamp:
confirm = messagebox.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=messagebox.CANCEL,
parent=self.text)
if not confirm:
return

if self.writefile(self.filename):
self.file_timestamp = os.stat(self.filename).st_mtime
self.set_saved(True)
try:
self.editwin.store_file_breaks()
Expand All @@ -219,6 +242,7 @@ def save_as(self, event):
filename = self.asksavefile()
if filename:
if self.writefile(filename):
self.file_timestamp = os.stat(filename).st_mtime
self.set_filename(filename)
self.set_saved(1)
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warn the user if a file will be overwritten when saving.
Loading