]> granicus.if.org Git - python/commitdiff
Provisional fix for writefile() [SF bug # 541730].
authorGuido van Rossum <guido@python.org>
Mon, 15 Apr 2002 00:19:12 +0000 (00:19 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 15 Apr 2002 00:19:12 +0000 (00:19 +0000)
The problem was that an exception can occur in the text.get() call or
in the write() call, when the text buffer contains non-ASCII
characters.  This causes the previous contents of the file to be lost.

The provisional fix is to call str(self.text.get(...)) *before*
opening the file, so that if the exception occurs, we never open the
file.

Two orthogonal better solutions have to wait for policy decisions:

1. We could try to encode the data as Latin-1 or as UTF-8; but that
   would require IDLE to grow a notion of file encoding which requires
   more thought.

2. We could make backups before overwriting a file.  This requires
   more thought because it needs to be fast and cross-platform and
   configurable.

Tools/idle/IOBinding.py

index dfea0b6618b170fb58d57ce7ffe2fbd046a810f3..db9fbd36afa300227854b2c6c175308b79cd9cd8 100644 (file)
@@ -148,9 +148,9 @@ class IOBinding:
 
     def writefile(self, filename):
         self.fixlastline()
+        chars = str(self.text.get("1.0", "end-1c"))
         try:
             f = open(filename, "w")
-            chars = self.text.get("1.0", "end-1c")
             f.write(chars)
             f.close()
             ## print "saved to", `filename`