Browse Source

exec_wrapper.py, test_init.py: minor cleanups

The MMGen Project 1 year ago
parent
commit
6a84ade2c7
3 changed files with 51 additions and 40 deletions
  1. 31 36
      scripts/exec_wrapper.py
  2. 19 1
      test/include/test_init.py
  3. 1 3
      test/test.py

+ 31 - 36
scripts/exec_wrapper.py

@@ -18,15 +18,6 @@ def exec_wrapper_init():
 	else:
 		exec_wrapper_sys.path.pop(0)
 
-	if 'TMUX' in exec_wrapper_os.environ:
-		del exec_wrapper_os.environ['TMUX']
-
-	if exec_wrapper_os.getenv('EXEC_WRAPPER_TRACEBACK'):
-		try:
-			exec_wrapper_os.unlink('test.py.err')
-		except:
-			pass
-
 	exec_wrapper_os.environ['MMGEN_EXEC_WRAPPER'] = '1'
 
 def exec_wrapper_write_traceback(e,exit_val):
@@ -36,6 +27,7 @@ def exec_wrapper_write_traceback(e,exit_val):
 	exc_line = (
 		repr(e) if type(e).__name__ in ('MMGenError','MMGenSystemExit') else
 		'{}: {}'.format( type(e).__name__, e ))
+
 	c = exec_wrapper_get_colors()
 
 	if os.getenv('EXEC_WRAPPER_TRACEBACK'):
@@ -89,40 +81,40 @@ def exec_wrapper_end_msg():
 			exec_wrapper_time.time() - exec_wrapper_tstart )))
 
 def exec_wrapper_tracemalloc_setup():
-	if exec_wrapper_os.getenv('MMGEN_TRACEMALLOC'):
-		exec_wrapper_os.environ['PYTHONTRACEMALLOC'] = '1'
-		import tracemalloc
-		tracemalloc.start()
-		exec_wrapper_sys.stderr.write("INFO → Appending memory allocation stats to 'tracemalloc.log'\n")
+	exec_wrapper_os.environ['PYTHONTRACEMALLOC'] = '1'
+	import tracemalloc
+	tracemalloc.start()
+	exec_wrapper_sys.stderr.write("INFO → Appending memory allocation stats to 'tracemalloc.log'\n")
 
 def exec_wrapper_tracemalloc_log():
-	if exec_wrapper_os.getenv('MMGEN_TRACEMALLOC'):
-		import tracemalloc,re
-		snapshot = tracemalloc.take_snapshot()
-		stats = snapshot.statistics('lineno')
-		depth = 100
-		col1w = 100
-		with open('tracemalloc.log','a') as fp:
-			fp.write('##### TOP {} {} #####\n'.format(depth,' '.join(exec_wrapper_sys.argv)))
-			for stat in stats[:depth]:
-				frame = stat.traceback[0]
-				fn = re.sub(r'.*\/site-packages\/|.*\/mmgen\/test\/overlay\/tree\/','',frame.filename)
-				fn = re.sub(r'.*\/mmgen\/test\/','test/',fn)
-				fp.write('{f:{w}} {s:>8.2f} KiB\n'.format(
-					f = f'{fn}:{frame.lineno}:',
-					s = stat.size/1024,
-					w = col1w ))
-			fp.write('{f:{w}} {s:8.2f} KiB\n\n'.format(
-				f = 'TOTAL {}:'.format(' '.join(exec_wrapper_sys.argv))[:col1w],
-				s = sum(stat.size for stat in stats) / 1024,
+	import tracemalloc,re
+	snapshot = tracemalloc.take_snapshot()
+	stats = snapshot.statistics('lineno')
+	depth = 100
+	col1w = 100
+	with open('tracemalloc.log','a') as fp:
+		fp.write('##### TOP {} {} #####\n'.format(depth,' '.join(exec_wrapper_sys.argv)))
+		for stat in stats[:depth]:
+			frame = stat.traceback[0]
+			fn = re.sub(r'.*\/site-packages\/|.*\/mmgen\/test\/overlay\/tree\/','',frame.filename)
+			fn = re.sub(r'.*\/mmgen\/test\/','test/',fn)
+			fp.write('{f:{w}} {s:>8.2f} KiB\n'.format(
+				f = f'{fn}:{frame.lineno}:',
+				s = stat.size/1024,
 				w = col1w ))
+		fp.write('{f:{w}} {s:8.2f} KiB\n\n'.format(
+			f = 'TOTAL {}:'.format(' '.join(exec_wrapper_sys.argv))[:col1w],
+			s = sum(stat.size for stat in stats) / 1024,
+			w = col1w ))
 
 import sys as exec_wrapper_sys
 import os as exec_wrapper_os
 import time as exec_wrapper_time
 
 exec_wrapper_init() # sets sys.path[0], runs overlay_setup()
-exec_wrapper_tracemalloc_setup()
+
+if exec_wrapper_os.getenv('MMGEN_TRACEMALLOC'):
+	exec_wrapper_tracemalloc_setup()
 
 # import mmgen mods only after overlay setup!
 from mmgen.devinit import init_dev as exec_wrapper_init_dev
@@ -139,7 +131,8 @@ except SystemExit as e:
 	if e.code != 0:
 		exec_wrapper_write_traceback(e,e.code)
 	else:
-		exec_wrapper_tracemalloc_log()
+		if exec_wrapper_os.getenv('MMGEN_TRACEMALLOC'):
+			exec_wrapper_tracemalloc_log()
 		exec_wrapper_end_msg()
 	exec_wrapper_sys.exit(e.code)
 except Exception as e:
@@ -147,5 +140,7 @@ except Exception as e:
 	exec_wrapper_write_traceback(e,exit_val)
 	exec_wrapper_sys.exit(exit_val)
 
-exec_wrapper_tracemalloc_log()
+if exec_wrapper_os.getenv('MMGEN_TRACEMALLOC'):
+	exec_wrapper_tracemalloc_log()
+
 exec_wrapper_end_msg()

+ 19 - 1
test/include/test_init.py

@@ -1,8 +1,26 @@
+#!/usr/bin/env python3
+#
+# mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
+# Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
+# Licensed under the GNU General Public License, Version 3:
+#   https://www.gnu.org/licenses
+# Public project repositories:
+#   https://github.com/mmgen/mmgen
+#   https://gitlab.com/mmgen/mmgen
+
+"""
+test.include.test_init: Initialization module for test scripts
+"""
+
 import sys,os
 os.environ['MMGEN_TEST_SUITE'] = '1'
 repo_root = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.pardir)))
-os.environ['PYTHONPATH'] = repo_root
 os.chdir(repo_root)
 sys.path[0] = repo_root
+
 from test.overlay import overlay_setup
 overlay_setup(repo_root)
+os.environ['PYTHONPATH'] = repo_root
+
+if 'TMUX' in os.environ:
+	del os.environ['TMUX']

+ 1 - 3
test/test.py

@@ -232,9 +232,7 @@ if cfg.profile:
 	cfg.names = True
 
 if cfg.exact_output:
-	def noop(s):
-		pass
-	qmsg = qmsg_r = msg = noop
+	qmsg = qmsg_r = lambda s: None
 else:
 	qmsg = cfg._util.qmsg
 	qmsg_r = cfg._util.qmsg_r