[Python-bugs-list] [ python-Bugs-541730 ] IDLE can clobber files

noreply@sourceforge.net noreply@sourceforge.net
Sun, 14 Apr 2002 17:20:22 -0700


Bugs item #541730, was opened at 2002-04-09 16:14
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=541730&group_id=5470

Category: IDLE
Group: Python 2.2
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Ben Darnell (bgdarnel)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: IDLE can clobber files

Initial Comment:
I just lost some work today when IDLE failed to save my
file.  The file was truncated to 0 bytes, so it wasn't
recoverable by the recycle bin or anything like that. 
I was running Python 2.2 on Win2kPro.  There was no
error message, although IDLE seemed to realize that the
save failed - the asterisk remained in the title bar. 
I tried saving to a different name, but that didn't
work either (although a zero-byte file was created with
the new name).  I know this isn't enough for you to go
on to fix the bug; if I encounter the error again I
will inspect the situation more closely.  

In order to prevent data loss like this in the future,
I'd like to see IDLE make a backup of files before it
saves over them.  I submit the following (untested) patch:

--- IOBinding.py.orig   Tue Apr  9 15:56:00 2002
+++ IOBinding.py        Tue Apr  9 15:59:07 2002
@@ -148,6 +148,11 @@
 
     def writefile(self, filename):
         self.fixlastline()
+       try:
+           if os.path.exists(filename):
+               os.rename(filename, filename+'~')
+       except IOError:
+           pass
         try:
             f = open(filename, "w")
             chars = self.text.get("1.0", "end-1c")


----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-04-14 20:20

Message:
Logged In: YES 
user_id=6380

I've fixed this using your later suggestion. We really
should do something better, but that requires more thought
and decisions, so I'm putting that off until the next
serious IDLE overhaul.

----------------------------------------------------------------------

Comment By: Ben Darnell (bgdarnel)
Date: 2002-04-10 11:33

Message:
Logged In: YES 
user_id=280

On further consideration, it's not the lack of backups that 
causes the problem, it's the fact that an exception in 
self.text.get can wipe out the file.  Moving the 
self.text.get call to before the file is opened would make 
it safe.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=541730&group_id=5470