Skip to content

Commit 995b203

Browse files
Kretzer6019sarahmonod
authored andcommitted
Handle the NativeReportingMode.LAST differently
1 parent 355cd7a commit 995b203

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/pystack/traceback_formatter.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,22 @@ def format_thread(thread: PyThread, native_mode: NativeReportingMode) -> Iterabl
8484
yield from format_frame(current_frame)
8585
current_frame = current_frame.next
8686
else:
87-
yield from _format_merged_stacks(thread, current_frame)
87+
yield from _format_merged_stacks(
88+
thread, current_frame, native_mode == NativeReportingMode.LAST
89+
)
8890
yield ""
8991

9092

9193
def _format_merged_stacks(
92-
thread: PyThread, current_frame: Optional[PyFrame]
94+
thread: PyThread,
95+
current_frame: Optional[PyFrame],
96+
native_last: bool = False,
9397
) -> Iterable[str]:
98+
c_frames_list: list[str] = []
9499
for frame in thread.native_frames:
95100
if frame_type(frame, thread.python_version) == NativeFrame.FrameType.EVAL:
96101
assert current_frame is not None
102+
c_frames_list = []
97103
yield from format_frame(current_frame)
98104
current_frame = current_frame.next
99105
while current_frame and not current_frame.is_entry:
@@ -104,12 +110,18 @@ def _format_merged_stacks(
104110
continue
105111
elif frame_type(frame, thread.python_version) == NativeFrame.FrameType.OTHER:
106112
function = colored(frame.symbol, "yellow")
107-
yield (
113+
formatted_c_frame = (
108114
f' {colored("(C)", "blue")} File "{frame.path}",'
109115
f" line {frame.linenumber},"
110116
f" in {function} ({colored(frame.library, attrs=['faint'])})"
111117
)
118+
if native_last:
119+
c_frames_list.append(formatted_c_frame)
120+
else:
121+
yield formatted_c_frame
112122
else: # pragma: no cover
113123
raise ValueError(
114124
f"Invalid frame type: {frame_type(frame, thread.python_version)}"
115125
)
126+
for c_frame in c_frames_list:
127+
yield c_frame

0 commit comments

Comments
 (0)