From 93a4ffbe52a28ad49d4acffa9a0397bbda520a06 Mon Sep 17 00:00:00 2001 From: Andreas Gocht Date: Wed, 19 Sep 2018 18:31:17 +0200 Subject: [PATCH 1/3] Add error message if #14 happens Add a suitable error messages if sys.modules['__main__'].main is called This should help to identify the problem --- scorep/__main__.py | 20 ++++++++++++++++---- test/test.py | 32 ++++++++++++++++++++++++++++++++ test/test_call_main.py | 6 ++++++ test/test_nosleep.py | 2 +- test/test_sleep.py | 2 +- 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 test/test_call_main.py diff --git a/scorep/__main__.py b/scorep/__main__.py index fbafde9..8e671e5 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/test/test.py b/test/test.py index aebe966..24f8cc3 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(): From 1f8f79657b78fa6014af66f654a74e8326488488 Mon Sep 17 00:00:00 2001 From: Andreas Gocht Date: Wed, 19 Sep 2018 18:33:30 +0200 Subject: [PATCH 2/3] remove _ --- scorep/__main__.py | 2 +- test/test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scorep/__main__.py b/scorep/__main__.py index 8e671e5..a7ea410 100644 --- a/scorep/__main__.py +++ b/scorep/__main__.py @@ -154,7 +154,7 @@ def main(argv=None): 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 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__': diff --git a/test/test.py b/test/test.py index 24f8cc3..6eb3177 100755 --- a/test/test.py +++ b/test/test.py @@ -128,7 +128,7 @@ def test_call_main(self): 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 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) From 2389c4e56e8fb1b58817ac9b9817058ce5e38c5c Mon Sep 17 00:00:00 2001 From: Andreas Gocht Date: Wed, 19 Sep 2018 18:39:14 +0200 Subject: [PATCH 3/3] fix python2 error message erro --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)