瀏覽代碼

parse non-utf8 files on macOS (#385)

addresses https://trac.osgeo.org/grass/ticket/3883
nilason 5 年之前
父節點
當前提交
ee755166d8
共有 1 個文件被更改,包括 19 次插入2 次删除
  1. 19 2
      lib/python/ctypes/ctypesgencore/parser/preprocessor.py

+ 19 - 2
lib/python/ctypes/ctypesgencore/parser/preprocessor.py

@@ -169,8 +169,25 @@ class PreprocessorParser(object):
                               shell=True,
                               shell=True,
                               universal_newlines=True,
                               universal_newlines=True,
                               stdout=subprocess.PIPE,
                               stdout=subprocess.PIPE,
-                              stderr=subprocess.PIPE)
-        ppout, pperr = pp.communicate()
+                              stderr=subprocess.PIPE)        
+        try:
+            ppout, pperr = pp.communicate()
+        except UnicodeError:
+            # Fix for https://trac.osgeo.org/grass/ticket/3883,
+            # handling file(s) encoded with mac_roman
+            if sys.platform == 'darwin':
+                pp = subprocess.Popen(cmd,
+                                      shell=True,
+                                      universal_newlines=False, #read as binary
+                                      stdout=subprocess.PIPE,
+                                      stderr=subprocess.PIPE)
+                ppout, pperr = pp.communicate()
+
+                data = ppout.decode('utf8', errors='replace')
+                ppout = data.replace('\r\n', '\n').replace('\r', '\n')
+                pperr = pperr.decode('utf8', errors='replace')
+            else:
+                raise UnicodeError
 
 
         for line in pperr.split("\n"):
         for line in pperr.split("\n"):
             if line:
             if line: