From fe3e6d1b76c8d8a9602a51af502036a641d493df Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Thu, 12 Dec 2019 03:18:31 -0700 Subject: [PATCH] bpo-6699: IDLE: Warn the user if a file will be overwritten when saving Co-Authored-By: Guilherme Polo Co-Authored-By: Priya Pappachan --- Lib/idlelib/iomenu.py | 20 +++++++++++++++++++ .../2019-12-12-03-18-02.bpo-6699.1CqJFG.rst | 1 + 2 files changed, 21 insertions(+) create mode 100644 Misc/NEWS.d/next/IDLE/2019-12-12-03-18-02.bpo-6699.1CqJFG.rst diff --git a/Lib/idlelib/iomenu.py b/Lib/idlelib/iomenu.py index 4b2833b8ca5..832a32cf366 100644 --- a/Lib/idlelib/iomenu.py +++ b/Lib/idlelib/iomenu.py @@ -148,14 +148,20 @@ class IOBinding: 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: @@ -339,7 +345,21 @@ 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. + 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): + self.file_timestamp = os.stat(self.filename).st_mtime self.set_saved(True) try: self.editwin.store_file_breaks() 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 00000000000..e7fb9bf1b3b --- /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.