[GRASS-SVN] r71035 - grass/branches/releasebranch_7_2/gui/wxpython/gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Sat May 6 02:06:25 PDT 2017


Author: martinl
Date: 2017-05-06 02:06:25 -0700 (Sat, 06 May 2017)
New Revision: 71035

Modified:
   grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py
Log:
wxGUI/forms: fix UnicodeDecodeError? on loading file when default and file encoding differs
             (merge r70789 from trunk)


Modified: grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py
===================================================================
--- grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py	2017-05-05 21:09:59 UTC (rev 71034)
+++ grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py	2017-05-06 09:06:25 UTC (rev 71035)
@@ -1822,9 +1822,17 @@
                                            style=wx.TE_MULTILINE,
                                            size=(-1, 75))
                         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)
 



More information about the grass-commit mailing list