[Python-checkins] r74440 - python/branches/tk_and_idle_maintenance/Lib/idlelib/IOBinding.py

guilherme.polo python-checkins at python.org
Fri Aug 14 02:00:58 CEST 2009


Author: guilherme.polo
Date: Fri Aug 14 02:00:58 2009
New Revision: 74440

Log:
Patch from issue 6699: "Warn user about overwriting a file that has a newer version on the filesystem".

Modified:
   python/branches/tk_and_idle_maintenance/Lib/idlelib/IOBinding.py

Modified: python/branches/tk_and_idle_maintenance/Lib/idlelib/IOBinding.py
==============================================================================
--- python/branches/tk_and_idle_maintenance/Lib/idlelib/IOBinding.py	(original)
+++ python/branches/tk_and_idle_maintenance/Lib/idlelib/IOBinding.py	Fri Aug 14 02:00:58 2009
@@ -182,14 +182,20 @@
         self.filename_change_hook = hook
 
     filename = None
+    file_timestamp = None
     dirname = None
 
     def set_filename(self, filename):
         if filename and os.path.isdir(filename):
             self.filename = None
+            self.file_timestamp = None
             self.dirname = filename
         else:
             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.set_saved(1)
             if self.filename_change_hook:
@@ -338,7 +344,24 @@
         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.
+            if self.file_timestamp != os.stat(self.filename).st_mtime:
+                dlg = tkMessageBox.Message(
+                        master=self.text,
+                        title="File has changed",
+                        message=(
+                            "The file has changed since reading it!\n\n"
+                            "Do you really want to overwrite it?"),
+                        default=tkMessageBox.NO,
+                        icon=tkMessageBox.WARNING,
+                        type=tkMessageBox.YESNO)
+                res = dlg.show()
+                if not self.text.tk.getboolean(str(res)):
+                    return
+
             if self.writefile(self.filename):
+                self.file_timestamp = os.stat(self.filename).st_mtime
                 self.set_saved(1)
                 try:
                     self.editwin.store_file_breaks()


More information about the Python-checkins mailing list