]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-50948: IDLE: Warn if saving a file will overwrite a newer version (GH-17578... 3.14
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 6 Jun 2026 21:12:07 +0000 (23:12 +0200)
committerGitHub <noreply@github.com>
Sat, 6 Jun 2026 21:12:07 +0000 (00:12 +0300)
(cherry picked from commit 69851a64076cc240513b834d87d654064f7ac597)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Co-authored-by: Guilherme Polo <ggpolo@gmail.com>
Co-authored-by: Priya Pappachan <priyapappachan010@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/idlelib/iomenu.py
Misc/NEWS.d/next/IDLE/2019-12-12-03-18-02.bpo-6699.1CqJFG.rst [new file with mode: 0644]

index 464126e2df0668200b80c97cf7d3e71c7a7e7492..fc502f7fde178089dc09206986d4a1ff383c4a6c 100644 (file)
@@ -61,6 +61,7 @@ class IOBinding:
         self.filename_change_hook = hook
 
     filename = None
+    file_timestamp = None
     dirname = None
 
     def set_filename(self, filename):
@@ -127,6 +128,7 @@ class IOBinding:
                     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 @@ class IOBinding:
                     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 @@ class IOBinding:
         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 @@ class IOBinding:
         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 @@ class IOBinding:
         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 @@ class IOBinding:
                                    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 (file)
index 0000000..e7fb9bf
--- /dev/null
@@ -0,0 +1 @@
+Warn the user if a file will be overwritten when saving.