[GRASS-SVN] r73326 - in grass/trunk: gui/wxpython/core lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 14 12:29:52 PDT 2018


Author: annakrat
Date: 2018-09-14 12:29:52 -0700 (Fri, 14 Sep 2018)
New Revision: 73326

Modified:
   grass/trunk/gui/wxpython/core/render.py
   grass/trunk/lib/python/script/utils.py
Log:
add function for safe passing text into environment under both Python 2 and 3 on Windows

Modified: grass/trunk/gui/wxpython/core/render.py
===================================================================
--- grass/trunk/gui/wxpython/core/render.py	2018-09-14 18:07:28 UTC (rev 73325)
+++ grass/trunk/gui/wxpython/core/render.py	2018-09-14 19:29:52 UTC (rev 73326)
@@ -33,7 +33,7 @@
 import wx
 
 from grass.script import core as grass
-from grass.script.utils import try_remove
+from grass.script.utils import try_remove, text_to_string
 from grass.script.task import cmdlist_to_tuple, cmdtuple_to_list
 from grass.pydispatch.signal import Signal
 from grass.exceptions import CalledModuleError
@@ -391,7 +391,7 @@
         if self.layer.GetType() in ('vector', 'thememap'):
             if os.path.isfile(self.layer._legrow):
                 os.remove(self.layer._legrow)
-            env_cmd['GRASS_LEGEND_FILE'] = self.layer._legrow
+            env_cmd['GRASS_LEGEND_FILE'] = text_to_string(self.layer._legrow)
 
         cmd_render = copy.deepcopy(cmd)
         cmd_render[1]['quiet'] = True  # be quiet
@@ -464,7 +464,7 @@
                             "GRASS_RENDER_FILE_COMPRESSION": "0",
                             "GRASS_RENDER_TRUECOLOR": "TRUE",
                             "GRASS_RENDER_TRANSPARENT": "TRUE",
-                            "GRASS_LEGEND_FILE": self.Map.legfile
+                            "GRASS_LEGEND_FILE": text_to_string(self.Map.legfile)
                             }
 
         self._init()

Modified: grass/trunk/lib/python/script/utils.py
===================================================================
--- grass/trunk/lib/python/script/utils.py	2018-09-14 18:07:28 UTC (rev 73325)
+++ grass/trunk/lib/python/script/utils.py	2018-09-14 19:29:52 UTC (rev 73326)
@@ -235,6 +235,18 @@
         return bytes(string)
 
 
+def text_to_string(text, encoding=None):
+    """Convert text to str. Useful when passing text into environments,
+       in Python 2 it needs to be bytes on Windows, in Python 3 in needs unicode.
+    """
+    if sys.version[0] == '2':
+        # Python 2
+        return encode(text, encoding=encoding)
+    else:
+        # Python 3
+        return decode(text, encoding=encoding)
+
+
 def parse_key_val(s, sep='=', dflt=None, val_type=None, vsep=None):
     """Parse a string into a dictionary, where entries are separated
     by newlines and the key and value are separated by `sep` (default: `=`)



More information about the grass-commit mailing list