Skip to content

Commit e89641f

Browse files
committed
added better support for lambdas, see: cs61a-lambda-lambda.txt
1 parent 1a3177b commit e89641f

4 files changed

Lines changed: 1000 additions & 3 deletions

File tree

‎v3/pg_logger.py‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@ def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalize
495495
# Value: parent frame
496496
self.closures = {}
497497

498+
# Key: code object for a lambda
499+
# Value: parent frame
500+
self.lambda_closures = {}
501+
498502
# set of function objects that were defined in the global scope
499503
self.globally_defined_funcs = set()
500504

@@ -538,9 +542,12 @@ def get_frame_id(self, cur_frame):
538542

539543
# Returns the (lexical) parent of a function value.
540544
def get_parent_of_function(self, val):
541-
if val not in self.closures:
545+
if val in self.closures:
546+
return self.get_frame_id(self.closures[val])
547+
elif val in self.lambda_closures:
548+
return self.get_frame_id(self.lambda_closures[val])
549+
else:
542550
return None
543-
return self.get_frame_id(self.closures[val])
544551

545552

546553
# Returns the (lexical) parent frame of the function that was called
@@ -555,6 +562,7 @@ def get_parent_of_function(self, val):
555562
# to make an educated guess based on the contents of local
556563
# variables inherited from possible parent frame candidates.
557564
def get_parent_frame(self, frame):
565+
#print >> sys.stderr, 'get_parent_frame: frame.f_code', frame.f_code
558566
for (func_obj, parent_frame) in self.closures.items():
559567
# ok, there's a possible match, but let's compare the
560568
# local variables in parent_frame to those of frame
@@ -575,6 +583,11 @@ def get_parent_frame(self, frame):
575583
if all_matched:
576584
return parent_frame
577585

586+
for (lambda_code_obj, parent_frame) in self.lambda_closures.items():
587+
if lambda_code_obj == frame.f_code:
588+
# TODO: should we do more verification like above?!?
589+
return parent_frame
590+
578591
return None
579592

580593

@@ -688,7 +701,7 @@ def interaction(self, frame, traceback, event_type):
688701
print >> sys.stderr, '=== STACK ===', 'curindex:', self.curindex
689702
for (e,ln) in self.stack:
690703
print >> sys.stderr, e.f_code.co_name + ' ' + e.f_code.co_filename + ' ' + str(ln)
691-
print >> sys.stderr, "top_frame", top_frame.f_code.co_name
704+
print >> sys.stderr, "top_frame", top_frame.f_code.co_name, top_frame.f_code
692705
'''
693706

694707

@@ -792,6 +805,7 @@ def interaction(self, frame, traceback, event_type):
792805

793806
# returns a dict with keys: function name, frame id, id of parent frame, encoded_locals dict
794807
def create_encoded_stack_entry(cur_frame):
808+
#print >> sys.stderr, '- create_encoded_stack_entry', cur_frame, self.closures, self.lambda_closures
795809
ret = {}
796810

797811

@@ -955,6 +969,21 @@ def create_encoded_stack_entry(cur_frame):
955969
self.parent_frames_set.add(chosen_parent_frame) # unequivocally add to this set!!!
956970
if not chosen_parent_frame in self.zombie_frames:
957971
self.zombie_frames.append(chosen_parent_frame)
972+
else:
973+
# look for code objects of lambdas defined within this
974+
# function, which comes up in cases like line 2 of:
975+
# def x(y):
976+
# (lambda z: lambda w: z+y)(y)
977+
#
978+
# x(42)
979+
if top_frame.f_code.co_consts:
980+
for e in top_frame.f_code.co_consts:
981+
if type(e) == types.CodeType and e.co_name == '<lambda>':
982+
# TODO: what if it's already in lambda_closures?
983+
self.lambda_closures[e] = top_frame
984+
self.parent_frames_set.add(top_frame) # copy-paste from above
985+
if not top_frame in self.zombie_frames:
986+
self.zombie_frames.append(top_frame)
958987
else:
959988
# if there is only a global scope visible ...
960989
for (k, v) in get_user_globals(top_frame).items():

0 commit comments

Comments
 (0)