Skip to content

Commit 14ec2b6

Browse files
committed
fixed a gnarly and subtle bug triggered by:
def f(p, k): def g(): print(k) if k == 0: f(g, 1) f(None, 0)
1 parent 15e9365 commit 14ec2b6

6 files changed

Lines changed: 2390 additions & 169 deletions

File tree

‎v3/pg_logger.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,30 @@ def create_encoded_stack_entry(cur_frame):
845845
# print a(20)
846846
# foo(10)
847847
chosen_parent_frame = None
848-
for (my_frame, my_lineno) in self.stack:
848+
# SUPER hacky but seems to work -- use reversed(self.stack)
849+
# because we want to traverse starting from the TOP of the stack
850+
# (most recent frame) and find the first frame containing
851+
# a constant code object that matches v.__code__ or v.func_code
852+
#
853+
# required for this example from Berkeley CS61a:
854+
#
855+
# def f(p, k):
856+
# def g():
857+
# print(k)
858+
# if k == 0:
859+
# f(g, 1)
860+
# f(None, 0)
861+
#
862+
# there are two calls to f, each of which defines a
863+
# closure g that should point to the respective frame.
864+
#
865+
# note that for the second call to f, the parent of the
866+
# g defined in there should be that frame, which is at
867+
# the TOP of the stack. this reversed() hack does the
868+
# right thing. note that if you don't traverse the stack
869+
# backwards, then you will mistakenly get the parent as
870+
# the FIRST f frame (bottom of the stack).
871+
for (my_frame, my_lineno) in reversed(self.stack):
849872
if chosen_parent_frame:
850873
break
851874

0 commit comments

Comments
 (0)