Browse Source

py3port: traceback_run fixes

MMGen 6 years ago
parent
commit
0482594e7a
2 changed files with 39 additions and 22 deletions
  1. 3 2
      mmgen/util.py
  2. 36 20
      scripts/traceback_run.py

+ 3 - 2
mmgen/util.py

@@ -75,9 +75,10 @@ def pformat(d):
 def pmsg(*args):
 def pmsg(*args):
 	if not args: return
 	if not args: return
 	msg(pformat(args if len(args) > 1 else args[0]))
 	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)
 	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):
 def set_for_type(val,refval,desc,invert_bool=False,src=None):
 	src_str = (''," in '{}'".format(src))[bool(src)]
 	src_str = (''," in '{}'".format(src))[bool(src)]

+ 36 - 20
scripts/traceback_run.py

@@ -1,38 +1,54 @@
 #!/usr/bin/env python3
 #!/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()
 	exc = l.pop()
 	if exc[:11] == 'SystemExit:': l.pop()
 	if exc[:11] == 'SystemExit:': l.pop()
+
 	def red(s):    return '{e}[31;1m{}{e}[0m'.format(s,e='\033')
 	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')
 	def yellow(s): return '{e}[33;1m{}{e}[0m'.format(s,e='\033')
 	sys.stdout.write('{}{}'.format(yellow(''.join(l)),red(exc)))
 	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:
 try:
 	sys.argv.pop(0)
 	sys.argv.pop(0)
-	exec(tb_source)
+	traceback_run_execed_file = sys.argv[0]
+	exec(open(sys.argv[0]).read())
 except SystemExit as e:
 except SystemExit as e:
 	if e.code != 0:
 	if e.code != 0:
-		process_exception()
+		traceback_run_process_exception()
 	sys.exit(e.code)
 	sys.exit(e.code)
 except Exception as e:
 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)
 	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()))