py3port: traceback_run fixes
This commit is contained in:
parent
f58b40c42c
commit
0482594e7a
2 changed files with 39 additions and 22 deletions
|
|
@ -75,9 +75,10 @@ def pformat(d):
|
|||
def pmsg(*args):
|
||||
if not args: return
|
||||
msg(pformat(args if len(args) > 1 else args[0]))
|
||||
def pdie(*args):
|
||||
def pdie(*args,exit_val=1):
|
||||
if not args: sys.exit(1)
|
||||
die(1,(pformat(args if len(args) > 1 else args[0])))
|
||||
die(exit_val,(pformat(args if len(args) > 1 else args[0])))
|
||||
def pdie2(*args): pdie(*args,exit_val=0)
|
||||
|
||||
def set_for_type(val,refval,desc,invert_bool=False,src=None):
|
||||
src_str = (''," in '{}'".format(src))[bool(src)]
|
||||
|
|
|
|||
|
|
@ -1,38 +1,54 @@
|
|||
#!/usr/bin/env python3
|
||||
import sys,traceback,os
|
||||
sys.path.insert(0,'.')
|
||||
|
||||
if 'TMUX' in os.environ: del os.environ['TMUX']
|
||||
os.environ['MMGEN_TRACEBACK'] = '1'
|
||||
# Import as few modules and define as few names as possible at global level before exec'ing the
|
||||
# file, as all names will be seen by the exec'ed file. To prevent name collisions, all names
|
||||
# defined here should begin with 'traceback_run_'
|
||||
|
||||
tb_source = open(sys.argv[1]).read()
|
||||
tb_file = os.path.join(os.environ['PWD'],'my.err')
|
||||
import sys
|
||||
|
||||
try: os.unlink(os.path.join(repo_root,tb_file))
|
||||
except: pass
|
||||
def traceback_run_init():
|
||||
import os
|
||||
sys.path.insert(0,'.')
|
||||
|
||||
if 'TMUX' in os.environ: del os.environ['TMUX']
|
||||
os.environ['MMGEN_TRACEBACK'] = '1'
|
||||
|
||||
of = os.path.join(os.environ['PWD'],'my.err')
|
||||
try: os.unlink(of)
|
||||
except: pass
|
||||
|
||||
return of
|
||||
|
||||
def traceback_run_process_exception():
|
||||
import traceback,re
|
||||
l = traceback.format_exception(*sys.exc_info()) # returns a list
|
||||
|
||||
for n in range(len(l)):
|
||||
l[n] = re.sub('File "<string>"','File "{}"'.format(traceback_run_execed_file),l[n],count=1)
|
||||
|
||||
def process_exception():
|
||||
l = traceback.format_exception(*sys.exc_info())
|
||||
l_save = l[:]
|
||||
exc = l.pop()
|
||||
if exc[:11] == 'SystemExit:': l.pop()
|
||||
|
||||
def red(s): return '{e}[31;1m{}{e}[0m'.format(s,e='\033')
|
||||
def yellow(s): return '{e}[33;1m{}{e}[0m'.format(s,e='\033')
|
||||
sys.stdout.write('{}{}'.format(yellow(''.join(l)),red(exc)))
|
||||
with open(tb_file,'w') as f:
|
||||
f.write(''.join(l_save))
|
||||
|
||||
open(traceback_run_outfile,'w').write(''.join(l+[exc]))
|
||||
|
||||
traceback_run_outfile = traceback_run_init()
|
||||
|
||||
try:
|
||||
sys.argv.pop(0)
|
||||
exec(tb_source)
|
||||
traceback_run_execed_file = sys.argv[0]
|
||||
exec(open(sys.argv[0]).read())
|
||||
except SystemExit as e:
|
||||
if e.code != 0:
|
||||
process_exception()
|
||||
traceback_run_process_exception()
|
||||
sys.exit(e.code)
|
||||
except Exception as e:
|
||||
process_exception()
|
||||
traceback_run_process_exception()
|
||||
sys.exit(e.mmcode if hasattr(e,'mmcode') else e.code if hasattr(e,'code') else 1)
|
||||
else:
|
||||
print('else: '+repr(sys.exc_info()))
|
||||
finally:
|
||||
print('finally: '+repr(sys.exc_info()))
|
||||
# else:
|
||||
# print('else: '+repr(sys.exc_info()))
|
||||
# finally:
|
||||
# print('finally: '+repr(sys.exc_info()))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue