https://github.com/python/cpython/commit/2fa0d4bc5ddcc848b800f4d5a5d233983ef98af3
commit: 2fa0d4bc5ddcc848b800f4d5a5d233983ef98af3
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-06-07T00:12:20+03:00
summary:

[3.13] gh-50948: IDLE: Warn if saving a file will overwrite a newer version 
(GH-17578) (GH-151028)

(cherry picked from commit 69851a64076cc240513b834d87d654064f7ac597)

Co-authored-by: Zackery Spytz <[email protected]>
Co-authored-by: Guilherme Polo <[email protected]>
Co-authored-by: Priya Pappachan <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/IDLE/2019-12-12-03-18-02.bpo-6699.1CqJFG.rst
M Lib/idlelib/iomenu.py

diff --git a/Lib/idlelib/iomenu.py b/Lib/idlelib/iomenu.py
index 464126e2df06682..fc502f7fde17808 100644
--- a/Lib/idlelib/iomenu.py
+++ b/Lib/idlelib/iomenu.py
@@ -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):
@@ -127,6 +128,7 @@ def loadfile(self, filename):
                     chars = f.read()
                     fileencoding = f.encoding
                     eol_convention = f.newlines
+                    file_timestamp = self.getmtime(filename)
                     converted = False
             except (UnicodeDecodeError, SyntaxError):
                 # Wait for the editor window to appear
@@ -142,6 +144,7 @@ def loadfile(self, filename):
                     chars = f.read()
                     fileencoding = f.encoding
                     eol_convention = f.newlines
+                    file_timestamp = self.getmtime(filename)
                     converted = True
         except OSError as err:
             messagebox.showerror("I/O Error", str(err), parent=self.text)
@@ -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
@@ -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 = self.getmtime(self.filename)
+            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 "break"
+
             if self.writefile(self.filename):
+                self.file_timestamp = self.getmtime(self.filename)
                 self.set_saved(True)
                 try:
                     self.editwin.store_file_breaks()
@@ -219,6 +242,7 @@ def save_as(self, event):
         filename = self.asksavefile()
         if filename:
             if self.writefile(filename):
+                self.file_timestamp = self.getmtime(filename)
                 self.set_filename(filename)
                 self.set_saved(1)
                 try:
@@ -251,6 +275,9 @@ def writefile(self, filename):
                                    parent=self.text)
             return False
 
+    def getmtime(self, filename):
+        return os.stat(filename).st_mtime
+
     def fixnewlines(self):
         """Return text with os eols.
 
diff --git a/Misc/NEWS.d/next/IDLE/2019-12-12-03-18-02.bpo-6699.1CqJFG.rst 
b/Misc/NEWS.d/next/IDLE/2019-12-12-03-18-02.bpo-6699.1CqJFG.rst
new file mode 100644
index 000000000000000..e7fb9bf1b3bdf6a
--- /dev/null
+++ b/Misc/NEWS.d/next/IDLE/2019-12-12-03-18-02.bpo-6699.1CqJFG.rst
@@ -0,0 +1 @@
+Warn the user if a file will be overwritten when saving.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to