@@ -77,6 +77,20 @@ def extract_location(location):
7777 return {"lineno" : lineno or 0 , "end_lineno" : lineno or 0 , "column" : 0 , "end_column" : 0 }
7878
7979
80+ def frame_to_dict (frame ):
81+ """Convert a FrameInfo to a dict."""
82+ loc = extract_location (frame .location )
83+ return {
84+ "filename" : frame .filename ,
85+ "funcname" : frame .funcname ,
86+ "lineno" : loc ["lineno" ],
87+ "end_lineno" : loc ["end_lineno" ],
88+ "column" : loc ["column" ],
89+ "end_column" : loc ["end_column" ],
90+ "opcode" : frame .opcode ,
91+ }
92+
93+
8094class RawCollector :
8195 """Collector that captures all raw data grouped by thread."""
8296
@@ -91,20 +105,7 @@ def collect(self, stack_frames, timestamps_us):
91105 count = len (timestamps_us )
92106 for interp in stack_frames :
93107 for thread in interp .threads :
94- frames = []
95- for frame in thread .frame_info :
96- loc = extract_location (frame .location )
97- frames .append (
98- {
99- "filename" : frame .filename ,
100- "funcname" : frame .funcname ,
101- "lineno" : loc ["lineno" ],
102- "end_lineno" : loc ["end_lineno" ],
103- "column" : loc ["column" ],
104- "end_column" : loc ["end_column" ],
105- "opcode" : frame .opcode ,
106- }
107- )
108+ frames = [frame_to_dict (f ) for f in thread .frame_info ]
108109 key = (interp .interpreter_id , thread .thread_id )
109110 sample = {"status" : thread .status , "frames" : frames }
110111 for _ in range (count ):
@@ -121,20 +122,7 @@ def samples_to_by_thread(samples):
121122 for sample in samples :
122123 for interp in sample :
123124 for thread in interp .threads :
124- frames = []
125- for frame in thread .frame_info :
126- loc = extract_location (frame .location )
127- frames .append (
128- {
129- "filename" : frame .filename ,
130- "funcname" : frame .funcname ,
131- "lineno" : loc ["lineno" ],
132- "end_lineno" : loc ["end_lineno" ],
133- "column" : loc ["column" ],
134- "end_column" : loc ["end_column" ],
135- "opcode" : frame .opcode ,
136- }
137- )
125+ frames = [frame_to_dict (f ) for f in thread .frame_info ]
138126 key = (interp .interpreter_id , thread .thread_id )
139127 by_thread [key ].append (
140128 {
@@ -220,53 +208,15 @@ def assert_samples_equal(self, expected_samples, collector):
220208 for j , (exp_frame , act_frame ) in enumerate (
221209 zip (exp ["frames" ], act ["frames" ])
222210 ):
223- self .assertEqual (
224- exp_frame ["filename" ],
225- act_frame ["filename" ],
226- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
227- f"frame { j } : filename mismatch" ,
228- )
229- self .assertEqual (
230- exp_frame ["funcname" ],
231- act_frame ["funcname" ],
232- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
233- f"frame { j } : funcname mismatch" ,
234- )
235- self .assertEqual (
236- exp_frame ["lineno" ],
237- act_frame ["lineno" ],
238- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
239- f"frame { j } : lineno mismatch "
240- f"(expected { exp_frame ['lineno' ]} , got { act_frame ['lineno' ]} )" ,
241- )
242- self .assertEqual (
243- exp_frame ["end_lineno" ],
244- act_frame ["end_lineno" ],
245- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
246- f"frame { j } : end_lineno mismatch "
247- f"(expected { exp_frame ['end_lineno' ]} , got { act_frame ['end_lineno' ]} )" ,
248- )
249- self .assertEqual (
250- exp_frame ["column" ],
251- act_frame ["column" ],
252- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
253- f"frame { j } : column mismatch "
254- f"(expected { exp_frame ['column' ]} , got { act_frame ['column' ]} )" ,
255- )
256- self .assertEqual (
257- exp_frame ["end_column" ],
258- act_frame ["end_column" ],
259- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
260- f"frame { j } : end_column mismatch "
261- f"(expected { exp_frame ['end_column' ]} , got { act_frame ['end_column' ]} )" ,
262- )
263- self .assertEqual (
264- exp_frame ["opcode" ],
265- act_frame ["opcode" ],
266- f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
267- f"frame { j } : opcode mismatch "
268- f"(expected { exp_frame ['opcode' ]} , got { act_frame ['opcode' ]} )" ,
269- )
211+ for field in ("filename" , "funcname" , "lineno" , "end_lineno" ,
212+ "column" , "end_column" , "opcode" ):
213+ self .assertEqual (
214+ exp_frame [field ],
215+ act_frame [field ],
216+ f"Thread ({ interp_id } , { thread_id } ), sample { i } , "
217+ f"frame { j } : { field } mismatch "
218+ f"(expected { exp_frame [field ]!r} , got { act_frame [field ]!r} )" ,
219+ )
270220
271221
272222class TestBinaryRoundTrip (BinaryFormatTestBase ):
0 commit comments