Add command line option -y/--python-exception, revert previous commit.

By default, catch exceptions and report them without a stack trace. When this option is specified, raise an actual Python exception (should show a strack trace).

Note: diff for this patch is best seen with -b (ignore-whitespace-change).
This commit is contained in:
Sei Lisa 2017-01-12 01:55:52 +01:00
parent 26b49b0807
commit e5ffe56fec

29
main.py
View file

@ -174,6 +174,7 @@ Usage: {progname}
[-o|--output=<filename>] output to file rather than stdout
[-H|--header] add the script as a comment in Firestorm format
[-T|--timestamp] add a timestamp as a comment at the beginning
[-y|--python-exceptions] when an exception is raised, show a stack trace
[-p|--preproc=mode] run external preprocessor (see below for modes)
(resets the preprocessor command line so far)
[-P|--prearg=<arg>] add parameter to preprocessor's command line
@ -337,9 +338,9 @@ def main(argv):
% (b"', '".join(options - validoptions)).decode('utf8'))
try:
opts, args = getopt.gnu_getopt(argv[1:], 'hO:o:p:P:HT',
opts, args = getopt.gnu_getopt(argv[1:], 'hO:o:p:P:HTy',
('optimizer-options=', 'help', 'version', 'output=', 'header',
'timestamp',
'timestamp','python-exceptions',
'preproc=', 'precmd=', 'prearg=', 'prenodef', 'preshow',
'avid=', 'avname=', 'assetid=', 'shortname='))
except getopt.GetoptError as e:
@ -359,6 +360,7 @@ def main(argv):
script_timestamp = ''
mcpp_mode = False
preshow = False
raise_exception = False
for opt, arg in opts:
if type(opt) is unicode:
@ -402,6 +404,9 @@ def main(argv):
elif opt in ('-o', '--output'):
outfile = arg
elif opt in ('-y', '--python-exceptions'):
raise_exception = True
elif opt in ('-p', '--preproc'):
preproc = arg.lower()
supported = ('ext', 'mcpp', 'gcpp', 'none')
@ -470,6 +475,8 @@ def main(argv):
shortname = arg
del opts
try:
if 'lso' in options:
lslopt.lslcommon.LSO = True
options.remove('lso')
@ -548,12 +555,12 @@ def main(argv):
if preproc != 'none':
# At this point, for the external preprocessor to work we need the
# script as a byte array, not as unicode, but it should be valid UTF-8.
# script as a byte array, not as unicode, but it should be UTF-8.
script = PreparePreproc(script)
if mcpp_mode:
# 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 is achieved
# with -dN but with mcpp there's no command line option.
# 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
# is achieved with -dN, but mcpp has no command line option.
script += '\n#pragma MCPP put_defines\n'
# Invoke the external preprocessor
@ -568,8 +575,8 @@ def main(argv):
del p, status
# This method is very imperfect, in several senses. However, since
# it's applied to the output of the preprocessor, all of the concerns
# should be addressed:
# it's applied to the output of the preprocessor, all of the
# concerns should be addressed:
# - \s includes \n, but \n should not be allowed.
# - Comments preceding the directive should not cause problems.
# e.g.: /* test */ #directive
@ -613,6 +620,12 @@ def main(argv):
outf.close()
return 0
except Exception as e:
if raise_exception:
raise
sys.stderr.write(e.__class__.__name__ + ': ' + str(e) + '\n')
return 1
if __name__ == '__main__':
ret = main(sys.argv)
if ret: