Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e687395

Browse files
committed
Python: CG trace: add canonic_filename helper
1 parent 7dd2677 commit e687395

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

python/tools/recorded-call-graph-metrics/cg_trace.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@
3535
# import IPython; IPython.embed(); sys.exit()
3636

3737

38+
_canonic_filename_cache = dict()
39+
def canonic_filename(filename):
40+
"""Return canonical form of filename. (same as Bdb.canonic)
41+
42+
For real filenames, the canonical form is a case-normalized (on
43+
case insensitive filesystems) absolute path. 'Filenames' with
44+
angle brackets, such as "<stdin>", generated in interactive
45+
mode, are returned unchanged.
46+
"""
47+
if filename == "<" + filename[1:-1] + ">":
48+
return filename
49+
canonic = _canonic_filename_cache.get(filename)
50+
if not canonic:
51+
canonic = os.path.abspath(filename)
52+
canonic = os.path.normcase(canonic)
53+
_canonic_filename_cache[filename] = canonic
54+
return canonic
55+
56+
3857
@dataclasses.dataclass(frozen=True)
3958
class Call():
4059
"""A call
@@ -44,15 +63,15 @@ class Call():
4463
inst_index: int
4564

4665
@classmethod
47-
def from_frame(cls, frame, debugger: bdb.Bdb):
66+
def from_frame(cls, frame):
4867
code = frame.f_code
4968

5069
# Uncomment to see the bytecode
5170
# b = dis.Bytecode(frame.f_code, current_offset=frame.f_lasti)
5271
# print(b.dis(), file=sys.__stderr__)
5372

5473
return cls(
55-
filename = debugger.canonic(code.co_filename),
74+
filename = canonic_filename(code.co_filename),
5675
linenum = frame.f_lineno,
5776
inst_index = frame.f_lasti,
5877
)
@@ -70,11 +89,11 @@ class Callee():
7089
linenum: int
7190

7291
@classmethod
73-
def from_frame(cls, frame, debugger: bdb.Bdb):
92+
def from_frame(cls, frame):
7493
code = frame.f_code
7594
return cls(
7695
funcname = code.co_name,
77-
filename = debugger.canonic(code.co_filename),
96+
filename = canonic_filename(code.co_filename),
7897
linenum = frame.f_lineno,
7998
)
8099

@@ -95,8 +114,8 @@ def __init__(self):
95114
super().__init__()
96115

97116
def user_call(self, frame, argument_list):
98-
call = Call.from_frame(frame.f_back, self)
99-
callee = Callee.from_frame(frame, self)
117+
call = Call.from_frame(frame.f_back)
118+
callee = Callee.from_frame(frame)
100119

101120
# _print(f'{call} -> {callee}')
102121
self.recorded_calls.add((call, callee))

0 commit comments

Comments
 (0)