-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
39 lines (25 loc) · 922 Bytes
/
Copy pathstack.py
File metadata and controls
39 lines (25 loc) · 922 Bytes
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
import os
from typing import Callable
def print_stack(func: Callable):
if not isinstance(func, Callable):
raise TypeError("Expected an argument of type function")# Get DEBUG flag from environment, default to 0
debug_flag = int(os.getenv('DEBUG', 0))
# Return the original function if the debug is off
if not debug_flag:
return func
depth: int = 0
MAX_DEPTH: int = 500
def wrapper(*args, **kwargs):
nonlocal depth
if depth >= MAX_DEPTH:
raise Exception("Maximum call stack depth exceeded")
indent = " " * depth
print(f"{indent} Entering {func.__name__}")
print(f"{indent} args {args}")
print(f"{indent} kwargs {kwargs}")
depth += 1
result = func(*args, **kwargs)
depth -= 1
print(f"{indent} Exiting {func.__name__}")
return result
return wrapper