diff --git a/scorep/__main__.py b/scorep/__main__.py index fbafde9..a7ea410 100644 --- a/scorep/__main__.py +++ b/scorep/__main__.py @@ -17,9 +17,8 @@ def _usage(outfile): cuda_support = None opencl_support = None - def _err_exit(msg): - sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) + sys.stderr.write("%s: %s\n" % ("scorep", msg)) sys.exit(1) @@ -62,7 +61,8 @@ def set_init_environment(mpi): os.environ["SCOREP_PYTHON_BINDINGS_INITALISED"] = "true" -def main(argv=None): +def scorep_main(argv=None): + global target_code if argv is None: argv = sys.argv try: @@ -131,6 +131,7 @@ def main(argv=None): try: with open(progname) as fp: code = compile(fp.read(), progname, 'exec') + target_code = code # try to emulate __main__ namespace as much as possible globs = { '__file__': progname, @@ -138,15 +139,26 @@ def main(argv=None): '__package__': None, '__cached__': None, } + global_trace.runctx(code, globs, globs) except OSError as err: _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err)) except SystemExit: pass +def main(argv=None): + import traceback + call_stack = traceback.extract_stack() + call_stack_array = traceback.format_list(call_stack) + call_stack_string = "" + for elem in call_stack_array[:-1]: + call_stack_string+=elem + _err_exit("Someone called scorep.__main__.main(argv).\n" + "This is not supposed to happen, but might be triggered, if your application calls \"sys.modules['__main__'].main\".\n" + "This python stacktrace might be helpfull to find the reason:\n%s" % call_stack_string) if __name__ == '__main__': - main() + scorep_main() else: ''' diff --git a/setup.py b/setup.py index a72c4d7..74906ab 100644 --- a/setup.py +++ b/setup.py @@ -248,7 +248,7 @@ def build_vampir_groups_writer(): print("Download and build vampir gouprs writer") if ret_val != 0: - print("Error building vampir groups writer:\n{}".format(message)) + print("Error building vampir groups writer:\n{}".format(message.encode('utf-8'))) print("Continuing without") else: additonal_libs.append(message) diff --git a/test/test.py b/test/test.py index aebe966..6eb3177 100755 --- a/test/test.py +++ b/test/test.py @@ -116,6 +116,38 @@ def test_mpi(self): #self.assertEqual(out.stderr.decode("utf-8"), expected_std_err) #self.assertEqual(out.stdout.decode("utf-8"), "hello world\n") + def test_call_main(self): + env = self.env + env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_call_main" + trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2" + out = subprocess.run(["python3", + "-m", + "scorep", + "test_call_main.py"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=env) + expected_std_err = """scorep: Someone called scorep.__main__.main(argv). +This is not supposed to happen, but might be triggered, if your application calls "sys.modules['__main__'].main". +This python stacktrace might be helpfull to find the reason: + File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main + "__main__", mod_spec) + File "/usr/lib/python3.6/runpy.py", line 85, in _run_code + exec(code, run_globals) + File "/home/gocht/virtenv/test_scorep_python_3.6/lib/python3.6/site-packages/scorep/__main__.py", line 161, in + scorep_main() + File "/home/gocht/virtenv/test_scorep_python_3.6/lib/python3.6/site-packages/scorep/__main__.py", line 143, in scorep_main + global_trace.runctx(code, globs, globs) + File "/home/gocht/virtenv/test_scorep_python_3.6/lib/python3.6/site-packages/scorep/trace.py", line 57, in runctx + exec(cmd, globals, locals) + File "test_call_main.py", line 6, in + sys.modules['__main__'].main(sys.argv) + +""" + expected_std_out = "" + self.assertEqual(out.stderr.decode("utf-8"), expected_std_err) + self.assertEqual(out.stdout.decode("utf-8"), expected_std_out) + def tearDown(self): shutil.rmtree( self.env["SCOREP_EXPERIMENT_DIRECTORY"], diff --git a/test/test_call_main.py b/test/test_call_main.py new file mode 100644 index 0000000..7630e34 --- /dev/null +++ b/test/test_call_main.py @@ -0,0 +1,6 @@ +import sys + +def main(argv=None): + print("successfully called main") + +sys.modules['__main__'].main(sys.argv) diff --git a/test/test_nosleep.py b/test/test_nosleep.py index daabfec..df19a51 100644 --- a/test/test_nosleep.py +++ b/test/test_nosleep.py @@ -2,7 +2,7 @@ def pointless_sleep(): - time.sleep(60) + time.sleep(5) def baz(): diff --git a/test/test_sleep.py b/test/test_sleep.py index b84f992..29937cf 100644 --- a/test/test_sleep.py +++ b/test/test_sleep.py @@ -2,7 +2,7 @@ def pointless_sleep(): - time.sleep(60) + time.sleep(5) def baz():