Skip to content

Commit 93a4ffb

Browse files
committed
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
1 parent c6372e4 commit 93a4ffb

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

scorep/__main__.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ def _usage(outfile):
1717
cuda_support = None
1818
opencl_support = None
1919

20-
2120
def _err_exit(msg):
22-
sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
21+
sys.stderr.write("%s: %s\n" % ("scorep", msg))
2322
sys.exit(1)
2423

2524

@@ -62,7 +61,8 @@ def set_init_environment(mpi):
6261
os.environ["SCOREP_PYTHON_BINDINGS_INITALISED"] = "true"
6362

6463

65-
def main(argv=None):
64+
def scorep_main(argv=None):
65+
global target_code
6666
if argv is None:
6767
argv = sys.argv
6868
try:
@@ -131,22 +131,34 @@ def main(argv=None):
131131
try:
132132
with open(progname) as fp:
133133
code = compile(fp.read(), progname, 'exec')
134+
target_code = code
134135
# try to emulate __main__ namespace as much as possible
135136
globs = {
136137
'__file__': progname,
137138
'__name__': '__main__',
138139
'__package__': None,
139140
'__cached__': None,
140141
}
142+
141143
global_trace.runctx(code, globs, globs)
142144
except OSError as err:
143145
_err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
144146
except SystemExit:
145147
pass
146148

149+
def main(argv=None):
150+
import traceback
151+
call_stack = traceback.extract_stack()
152+
call_stack_array = traceback.format_list(call_stack)
153+
call_stack_string = ""
154+
for elem in call_stack_array[:-1]:
155+
call_stack_string+=elem
156+
_err_exit("Someone called scorep.__main__.main(argv).\n"
157+
"This is not supposed to happen, but might be triggered, if your application calls \"_sys.modules['__main__'].main\".\n"
158+
"This python stacktrace might be helpfull to find the reason:\n%s" % call_stack_string)
147159

148160
if __name__ == '__main__':
149-
main()
161+
scorep_main()
150162

151163
else:
152164
'''

test/test.py

+32
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,38 @@ def test_mpi(self):
116116
#self.assertEqual(out.stderr.decode("utf-8"), expected_std_err)
117117
#self.assertEqual(out.stdout.decode("utf-8"), "hello world\n")
118118

119+
def test_call_main(self):
120+
env = self.env
121+
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_call_main"
122+
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
123+
out = subprocess.run(["python3",
124+
"-m",
125+
"scorep",
126+
"test_call_main.py"],
127+
stdout=subprocess.PIPE,
128+
stderr=subprocess.PIPE,
129+
env=env)
130+
expected_std_err = """scorep: Someone called scorep.__main__.main(argv).
131+
This is not supposed to happen, but might be triggered, if your application calls "_sys.modules['__main__'].main".
132+
This python stacktrace might be helpfull to find the reason:
133+
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
134+
"__main__", mod_spec)
135+
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
136+
exec(code, run_globals)
137+
File "/home/gocht/virtenv/test_scorep_python_3.6/lib/python3.6/site-packages/scorep/__main__.py", line 161, in <module>
138+
scorep_main()
139+
File "/home/gocht/virtenv/test_scorep_python_3.6/lib/python3.6/site-packages/scorep/__main__.py", line 143, in scorep_main
140+
global_trace.runctx(code, globs, globs)
141+
File "/home/gocht/virtenv/test_scorep_python_3.6/lib/python3.6/site-packages/scorep/trace.py", line 57, in runctx
142+
exec(cmd, globals, locals)
143+
File "test_call_main.py", line 6, in <module>
144+
sys.modules['__main__'].main(sys.argv)
145+
146+
"""
147+
expected_std_out = ""
148+
self.assertEqual(out.stderr.decode("utf-8"), expected_std_err)
149+
self.assertEqual(out.stdout.decode("utf-8"), expected_std_out)
150+
119151
def tearDown(self):
120152
shutil.rmtree(
121153
self.env["SCOREP_EXPERIMENT_DIRECTORY"],

test/test_call_main.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
def main(argv=None):
4+
print("successfully called main")
5+
6+
sys.modules['__main__'].main(sys.argv)

test/test_nosleep.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def pointless_sleep():
5-
time.sleep(60)
5+
time.sleep(5)
66

77

88
def baz():

test/test_sleep.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def pointless_sleep():
5-
time.sleep(60)
5+
time.sleep(5)
66

77

88
def baz():

0 commit comments

Comments
 (0)