浏览代码

pythonlib: fix encoding

Anna Petrasova 6 年之前
父节点
当前提交
d9b69900f5
共有 1 个文件被更改,包括 7 次插入10 次删除
  1. 7 10
      lib/python/script/task.py

+ 7 - 10
lib/python/script/task.py

@@ -445,21 +445,18 @@ def convert_xml_to_utf8(xml_text):
 
     # modify: fetch encoding from the interface description text(xml)
     # e.g. <?xml version="1.0" encoding="GBK"?>
-    pattern = re.compile(b'<\?xml[^>]*\Wencoding="([^"]*)"[^>]*\?>')
+    pattern = re.compile('<\?xml[^>]*\Wencoding="([^"]*)"[^>]*\?>')
     m = re.match(pattern, xml_text)
     if m is None:
-        return xml_text
+        return xml_text.encode("utf-8") if xml_text else None
     #
     enc = m.groups()[0]
-    # for Python 3
-    enc_decoded = enc.decode('ascii')
 
     # modify: change the encoding to "utf-8", for correct parsing
-    xml_text_utf8 = xml_text.decode(enc_decoded).encode("utf-8")
-    p = re.compile(b'encoding="' + enc + b'"', re.IGNORECASE)
-    xml_text_utf8 = p.sub(b'encoding="utf-8"', xml_text_utf8)
+    p = re.compile('encoding="' + enc + '"', re.IGNORECASE)
+    xml_text_utf8 = p.sub('encoding="utf-8"', xml_text)
 
-    return xml_text_utf8
+    return xml_text_utf8.encode("utf-8")
 
 
 def get_interface_description(cmd):
@@ -472,7 +469,7 @@ def get_interface_description(cmd):
     :param cmd: command (name of GRASS module)
     """
     try:
-        p = Popen([encode(cmd), b'--interface-description'], stdout=PIPE,
+        p = Popen([cmd, '--interface-description'], stdout=PIPE,
                   stderr=PIPE)
         cmdout, cmderr = p.communicate()
 
@@ -503,7 +500,7 @@ def get_interface_description(cmd):
         raise ScriptError(_("Unable to fetch interface description for command '<{cmd}>'."
                             "\n\nDetails: <{det}>".format(cmd=cmd, det=e)))
 
-    desc = convert_xml_to_utf8(cmdout)
+    desc = convert_xml_to_utf8(decode(cmdout))
     desc = desc.replace(b'grass-interface.dtd',
                         os.path.join(os.getenv('GISBASE'), 'gui', 'xml',
                                      'grass-interface.dtd').encode('utf-8'))