[alacarte] Fix crashes when writing out files



commit f7835d7dcd37b10f25e208581ec52919cf68e732
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Nov 16 23:22:14 2012 -0500

    Fix crashes when writing out files
    
    XML files as well as key files should always be in UTF-8 encodings.

 Alacarte/MainWindow.py |    5 ++++-
 Alacarte/MenuEditor.py |    9 +++++----
 2 files changed, 9 insertions(+), 5 deletions(-)
---
diff --git a/Alacarte/MainWindow.py b/Alacarte/MainWindow.py
index 01c6a6c..ffd3dcc 100644
--- a/Alacarte/MainWindow.py
+++ b/Alacarte/MainWindow.py
@@ -19,6 +19,7 @@
 
 from gi.repository import Gtk, GObject, Gio, GdkPixbuf, Gdk, GMenu, GLib
 import cgi
+import codecs
 import os
 import gettext
 import subprocess
@@ -319,7 +320,9 @@ class MainWindow(object):
 
         if not os.path.isfile(file_path):
             data = open(item.get_desktop_file_path()).read()
-            open(file_path, 'w').write(data)
+            fd = codecs.open(file_path, 'w', 'utf8')
+            fd.write(data)
+            fd.close()
 
         if file_path not in self.edit_pool:
             self.edit_pool.append(file_path)
diff --git a/Alacarte/MenuEditor.py b/Alacarte/MenuEditor.py
index 8d992ca..8f4fbaf 100644
--- a/Alacarte/MenuEditor.py
+++ b/Alacarte/MenuEditor.py
@@ -16,6 +16,7 @@
 #   License along with this library; if not, write to the Free Software
 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+import codecs
 import os
 import xml.dom.minidom
 import xml.parsers.expat
@@ -48,7 +49,7 @@ class MenuEditor(object):
         self.load()
 
     def save(self):
-        fd = open(self.path, 'w')
+        fd = codecs.open(self.path, 'w', 'utf8')
         fd.write(self.dom.toprettyxml())
         fd.close()
 
@@ -259,7 +260,7 @@ class MenuEditor(object):
 
         contents, length = keyfile.to_data()
 
-        f = open(out_path, 'w')
+        f = codecs.open(out_path, 'w', 'utf8')
         f.write(contents)
         f.close()
 
@@ -400,7 +401,7 @@ class MenuEditor(object):
 
         contents, length = keyfile.to_data()
 
-        f = open(os.path.join(util.getUserItemPath(), file_id), 'w')
+        f = codecs.open(os.path.join(util.getUserItemPath(), file_id), 'w', 'utf8')
         f.write(contents)
         f.close()
         return file_id
@@ -421,7 +422,7 @@ class MenuEditor(object):
 
         contents, length = keyfile.to_data()
 
-        f = open(os.path.join(util.getUserDirectoryPath(), file_id), 'w')
+        f = codecs.open(os.path.join(util.getUserDirectoryPath(), file_id), 'w', 'utf8')
         f.write(contents)
         f.close()
         return file_id



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]