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

Skip to content

Commit ba4207f

Browse files
committed
Python: CG trace: sort output before writing/printing
Allows comparing output of one run with another
1 parent e687395 commit ba4207f

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def canonic_filename(filename):
5454
return canonic
5555

5656

57-
@dataclasses.dataclass(frozen=True)
57+
@dataclasses.dataclass(frozen=True, eq=True, order=True)
5858
class Call():
5959
"""A call
6060
"""
@@ -77,16 +77,16 @@ def from_frame(cls, frame):
7777
)
7878

7979

80-
@dataclasses.dataclass(frozen=True)
80+
@dataclasses.dataclass(frozen=True, eq=True, order=True)
8181
class Callee():
8282
"""A callee (Function/Lambda/???)
8383
8484
should (hopefully) be uniquely identified by its name and location (filename+line
8585
number)
8686
"""
87-
funcname: str
8887
filename: str
8988
linenum: int
89+
funcname: str
9090

9191
@classmethod
9292
def from_frame(cls, frame):
@@ -145,7 +145,7 @@ class CSVExporter(Exporter):
145145
def export(recorded_calls, outfile_path):
146146
with open(outfile_path, 'w', newline='') as csv_file:
147147
writer = None
148-
for (call, callee) in recorded_calls:
148+
for (call, callee) in sorted(recorded_calls):
149149
data = {
150150
**Exporter.dataclass_to_dict(call),
151151
**Exporter.dataclass_to_dict(callee)
@@ -170,7 +170,7 @@ def export(recorded_calls, outfile_path):
170170

171171
root = ET.Element('root')
172172

173-
for (call, callee) in recorded_calls:
173+
for (call, callee) in sorted(recorded_calls):
174174
data = {
175175
**Exporter.dataclass_to_dict(call),
176176
**Exporter.dataclass_to_dict(callee)
@@ -234,7 +234,7 @@ def export(recorded_calls, outfile_path):
234234
elif opts.xml:
235235
XMLExporter.export(cgt.recorded_calls, opts.xml)
236236
else:
237-
for (call, callee) in cgt.recorded_calls:
237+
for (call, callee) in sorted(cgt.recorded_calls):
238238
print(f'{call} -> {callee}')
239239

240240
print('--- captured stdout ---')
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<root>
2-
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="7" call_inst_index="18" callee_funcname="foo" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" />
3-
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="8" call_inst_index="24" callee_funcname="bar" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" />
4-
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="30" callee_funcname="foo" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" />
5-
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="36" callee_funcname="bar" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" />
2+
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="7" call_inst_index="18" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" callee_funcname="foo" />
3+
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="8" call_inst_index="24" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" callee_funcname="bar" />
4+
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="30" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" callee_funcname="foo" />
5+
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="36" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" callee_funcname="bar" />
66
</root>

0 commit comments

Comments
 (0)