MakeHeader.py: Fix for non-utf8 environments (#770)

Header creation fails with non-utf8 locale and python3.
Simply set LC_ALL="C" and use python3 to reproduce the issue.

env LC_ALL="C" ./scripts/MakeHeader.py MetersPanel.c
Traceback (most recent call last):
  File "./scripts/MakeHeader.py", line 32, in <module>
    for line in file.readlines():
  File "/usr/lib64/python3.5/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 956: ordinal not in range(128)

This changes is python2 and python3 compatible

cStringIO.StringIO module is removed because it is not able to accept unicode strings
https://docs.python.org/2/library/stringio.html#cStringIO.StringIO
This commit is contained in:
volkov-am 2018-04-06 03:55:51 +05:00 committed by Hisham Muhammad
parent 505fa6b517
commit 791aae87c4
1 changed files with 6 additions and 9 deletions

View File

@ -1,8 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, string import os, sys, string, io
try:
from cStringIO import StringIO
except ImportError:
try: try:
from StringIO import StringIO from StringIO import StringIO
except ImportError: except ImportError:
@ -17,7 +14,7 @@ COPYDEFINE=5
state = ANY state = ANY
static = 0 static = 0
file = open(sys.argv[1]) file = io.open(sys.argv[1], "r", encoding="utf-8")
name = sys.argv[1][:-2] name = sys.argv[1][:-2]
out = StringIO() out = StringIO()
@ -104,12 +101,12 @@ out.write( "#endif\n" )
# This prevents a lot of recompilation during development # This prevents a lot of recompilation during development
out.seek(0) out.seek(0)
try: try:
with open(name + ".h", "r") as orig: with io.open(name + ".h", "r", encoding="utf-8") as orig:
origcontents = orig.readlines() origcontents = orig.readlines()
except: except:
origcontents = "" origcontents = ""
if origcontents != out.readlines(): if origcontents != out.readlines():
with open(name + ".h", "w") as new: with io.open(name + ".h", "w", encoding="utf-8") as new:
print("Writing "+name+".h") print("Writing "+name+".h")
new.write(out.getvalue()) new.write(out.getvalue())
out.close() out.close()