Skip to content

Commit d24c7d3

Browse files
committed
Restored the frame hiding trick
Turns out it this is still needed on PyPy.
1 parent 8ffc1b1 commit d24c7d3

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

trio/_core/_run.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ def _public(fn):
6363
_r = random.Random()
6464

6565

66+
# On CPython, Context.run() is implemented in C and doesn't show up in
67+
# tracebacks. On PyPy, it is implemented in Python and adds 1 frame to tracebacks.
68+
def _count_context_run_tb_frames():
69+
def function_with_unique_name_xyzzy():
70+
1 / 0
71+
72+
ctx = copy_context()
73+
try:
74+
ctx.run(function_with_unique_name_xyzzy)
75+
except ZeroDivisionError as exc:
76+
tb = exc.__traceback__
77+
# Skip the frame where we caught it
78+
tb = tb.tb_next
79+
count = 0
80+
while tb.tb_frame.f_code.co_name != "function_with_unique_name_xyzzy":
81+
tb = tb.tb_next
82+
count += 1
83+
return count
84+
85+
86+
CONTEXT_RUN_TB_FRAMES = _count_context_run_tb_frames()
87+
88+
6689
@attr.s(frozen=True, slots=True)
6790
class SystemClock:
6891
# Add a large random offset to our clock to ensure that if people
@@ -2151,6 +2174,8 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
21512174
# catching it, and then in addition we remove however many
21522175
# more Context.run adds.
21532176
tb = task_exc.__traceback__.tb_next
2177+
for _ in range(CONTEXT_RUN_TB_FRAMES):
2178+
tb = tb.tb_next
21542179
final_outcome = Error(task_exc.with_traceback(tb))
21552180
# Remove local refs so that e.g. cancelled coroutine locals
21562181
# are not kept alive by this frame until another exception

0 commit comments

Comments
 (0)