Forráskód Böngészése

wxGUI/forms: fix UnicodeDecodeError on loading file when default and file encoding differs

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@70789 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 8 éve
szülő
commit
4d866756f7
1 módosított fájl, 11 hozzáadás és 3 törlés
  1. 11 3
      gui/wxpython/gui_core/forms.py

+ 11 - 3
gui/wxpython/gui_core/forms.py

@@ -1840,9 +1840,17 @@ class CmdPanel(wx.Panel):
                                            style=wx.TE_MULTILINE,
                                            style=wx.TE_MULTILINE,
                                            size=(-1, 75))
                                            size=(-1, 75))
                         if p.get('value', '') and os.path.isfile(p['value']):
                         if p.get('value', '') and os.path.isfile(p['value']):
-                            f = open(p['value'])
-                            ifbb.SetValue(''.join(f.readlines()))
-                            f.close()
+                            ifbb.Clear()
+                            enc = locale.getdefaultlocale()[1]
+                            with codecs.open(p['value'], encoding=enc, errors='ignore') as f:
+                                nonascii = bytearray(range(0x80, 0x100))
+                                for line in f.readlines():
+                                    try:
+                                        ifbb.AppendText(line)
+                                    except UnicodeDecodeError:
+                                        # remove non-ascii characters on encoding mismatch (file vs OS)
+                                        ifbb.AppendText(line.translate(None, nonascii))
+                                ifbb.SetInsertionPoint(0)
 
 
                         ifbb.Bind(wx.EVT_TEXT, self.OnFileText)
                         ifbb.Bind(wx.EVT_TEXT, self.OnFileText)