Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions scorep/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -131,22 +131,34 @@ 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,
'__name__': '__main__',
'__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:
'''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <module>
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 <module>
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"],
Expand Down
6 changes: 6 additions & 0 deletions test/test_call_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys

def main(argv=None):
print("successfully called main")

sys.modules['__main__'].main(sys.argv)
2 changes: 1 addition & 1 deletion test/test_nosleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def pointless_sleep():
time.sleep(60)
time.sleep(5)


def baz():
Expand Down
2 changes: 1 addition & 1 deletion test/test_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def pointless_sleep():
time.sleep(60)
time.sleep(5)


def baz():
Expand Down