-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_util.py
47 lines (33 loc) · 1.07 KB
/
run_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class NBXInteract(Exception):
pass
def nbx_interact():
"""
Utility func that will raise a special exception that causes partialrun to
copy the frames local variables into global. Useful for dumping out
function variables so you can debug in ipython without pdb.
"""
# TODO add a check to see if we are running in a compatible env.
raise NBXInteract()
RUN_AS_MAIN_CODES = set()
def run_as_main(func):
"""
Decorate a func as pseudo main.
1. Marks the function as being the frame where locals are globalized from.
NOTES:
Marking code by just tagging it might not be enough. Like what if
something ends up being recursive?
"""
code = func.__code__
# TODO weakref this?
RUN_AS_MAIN_CODES.add(code)
return func
def is_code_run_as_main(code):
return code in RUN_AS_MAIN_CODES
def find_run_as_main(f):
while True:
if f.f_code and is_code_run_as_main(f.f_code):
return f
# there is no more frames
if not f.f_back:
return False
f = f.f_back