Bläddra i källkod

add function for safe passing text into environment under both Python 2 and 3 on Windows

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73326 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 6 år sedan
förälder
incheckning
491ac942c0
2 ändrade filer med 15 tillägg och 3 borttagningar
  1. 3 3
      gui/wxpython/core/render.py
  2. 12 0
      lib/python/script/utils.py

+ 3 - 3
gui/wxpython/core/render.py

@@ -33,7 +33,7 @@ import time
 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 @@ class RenderLayerMgr(wx.EvtHandler):
         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 @@ class RenderMapMgr(wx.EvtHandler):
                             "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()

+ 12 - 0
lib/python/script/utils.py

@@ -235,6 +235,18 @@ def encode(string, encoding=None):
         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: `=`)