More Unicode vs str fixes

This commit is contained in:
Sei Lisa 2019-01-16 20:24:31 +01:00
parent 173a12be15
commit b5092c0be2

31
main.py
View file

@ -432,11 +432,6 @@ def main(argv):
libdata = None libdata = None
for opt, arg in opts: for opt, arg in opts:
if type(opt) is unicode:
opt = opt.encode('utf8')
if type(arg) is unicode:
arg = arg.encode('utf8')
if opt in ('-O', '--optimizer-options'): if opt in ('-O', '--optimizer-options'):
optchanges = arg.lower().split(',') optchanges = arg.lower().split(',')
for chg in optchanges: for chg in optchanges:
@ -453,7 +448,7 @@ def main(argv):
chgfix = '+' + chgfix chgfix = '+' + chgfix
if chgfix[1:] not in validoptions: if chgfix[1:] not in validoptions:
Usage(argv[0], 'optimizer-options') Usage(argv[0], 'optimizer-options')
sys.stderr.write(u"\nError: Unrecognized" werr(u"\nError: Unrecognized"
u" optimizer option: %s\n" % chg.decode('utf8')) u" optimizer option: %s\n" % chg.decode('utf8'))
return 1 return 1
if chgfix[0] == '-': if chgfix[0] == '-':
@ -487,9 +482,9 @@ def main(argv):
supported = ('ext', 'mcpp', 'gcpp', 'none') supported = ('ext', 'mcpp', 'gcpp', 'none')
if preproc not in supported: if preproc not in supported:
Usage(argv[0]) Usage(argv[0])
sys.stderr.write(u"\nUnknown --preproc option: '%s'." werr(u"\nUnknown --preproc option: '%s'."
u" Only '%s' supported.\n" u" Only '%s' supported.\n"
% (preproc, u"', '".join(supported))) % (preproc, str2u("', '".join(supported))))
return 1 return 1
if preproc == 'gcpp': if preproc == 'gcpp':
@ -577,7 +572,7 @@ def main(argv):
f = open(fname, 'r') f = open(fname, 'r')
except IOError as e: except IOError as e:
if e.errno == 2: if e.errno == 2:
sys.stderr.write('Error: File not found: %s\n' % fname) werr(u"Error: File not found: %s\n" % str2u(fname))
return 2 return 2
raise raise
try: try:
@ -593,7 +588,7 @@ def main(argv):
import time import time
tmp = time.time() tmp = time.time()
script_timestamp = time.strftime( script_timestamp = time.strftime(
'// Generated on %Y-%m-%dT%H:%M:%S.{0:06d}Z\n' b'// Generated on %Y-%m-%dT%H:%M:%S.{0:06d}Z\n'
.format(int(tmp % 1 * 1000000)), time.gmtime(tmp)) .format(int(tmp % 1 * 1000000)), time.gmtime(tmp))
del tmp del tmp
@ -660,7 +655,7 @@ def main(argv):
# As a special treatment for mcpp, we force it to output its # As a special treatment for mcpp, we force it to output its
# macros so we can read if USE_xxx are defined. With GCC that # macros so we can read if USE_xxx are defined. With GCC that
# is achieved with -dN, but mcpp has no command line option. # is achieved with -dN, but mcpp has no command line option.
script += '\n#pragma MCPP put_defines\n' script += b'\n#pragma MCPP put_defines\n'
# Invoke the external preprocessor # Invoke the external preprocessor
import subprocess import subprocess
@ -680,13 +675,13 @@ def main(argv):
# - Comments preceding the directive should not cause problems. # - Comments preceding the directive should not cause problems.
# e.g.: /* test */ #directive # e.g.: /* test */ #directive
# - #directive within a comment or string should be ignored. # - #directive within a comment or string should be ignored.
for x in re.findall(r'(?:(?<=\n)|^)\s*#\s*define\s+(' for x in re.findall(br'(?:(?<=\n)|^)\s*#\s*define\s+('
r'USE_SWITCHES' br'USE_SWITCHES'
r'|USE_LAZY_LISTS' br'|USE_LAZY_LISTS'
r')(?:$|[^A-Za-z0-9_])', script, re.S): br')(?:$|[^A-Za-z0-9_])', script, re.S):
if x == 'USE_SWITCHES': if x == b'USE_SWITCHES':
options.add('enableswitch') options.add('enableswitch')
elif x == 'USE_LAZY_LISTS': elif x == b'USE_LAZY_LISTS':
options.add('lazylists') options.add('lazylists')
if not preshow: if not preshow:
@ -727,7 +722,7 @@ def main(argv):
except Exception as e: except Exception as e:
if raise_exception: if raise_exception:
raise raise
sys.stderr.write(e.__class__.__name__ + ': ' + str(e) + '\n') werr(e.__class__.__name__ + ': ' + str(e) + '\n')
return 1 return 1
if __name__ == '__main__': if __name__ == '__main__':