@@ -128,7 +128,7 @@ def setJS(jsStr):
128128# especially getting setHTML, setCSS, and setJS to work in the imported
129129# modules.)
130130CUSTOM_MODULE_IMPORTS = () # ignore these for now
131- # ('callback_module',
131+ # ('callback_module',
132132# 'ttt_module',
133133# 'html_module',
134134# 'htmlexample_module',
@@ -496,6 +496,10 @@ def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalize
496496 # Value: parent frame
497497 self .closures = {}
498498
499+ # Key: code object for a lambda
500+ # Value: parent frame
501+ self .lambda_closures = {}
502+
499503 # set of function objects that were defined in the global scope
500504 self .globally_defined_funcs = set ()
501505
@@ -539,9 +543,12 @@ def get_frame_id(self, cur_frame):
539543
540544 # Returns the (lexical) parent of a function value.
541545 def get_parent_of_function (self , val ):
542- if val not in self .closures :
546+ if val in self .closures :
547+ return self .get_frame_id (self .closures [val ])
548+ elif val in self .lambda_closures :
549+ return self .get_frame_id (self .lambda_closures [val ])
550+ else :
543551 return None
544- return self .get_frame_id (self .closures [val ])
545552
546553
547554 # Returns the (lexical) parent frame of the function that was called
@@ -556,6 +563,7 @@ def get_parent_of_function(self, val):
556563 # to make an educated guess based on the contents of local
557564 # variables inherited from possible parent frame candidates.
558565 def get_parent_frame (self , frame ):
566+ #print >> sys.stderr, 'get_parent_frame: frame.f_code', frame.f_code
559567 for (func_obj , parent_frame ) in self .closures .items ():
560568 # ok, there's a possible match, but let's compare the
561569 # local variables in parent_frame to those of frame
@@ -576,6 +584,11 @@ def get_parent_frame(self, frame):
576584 if all_matched :
577585 return parent_frame
578586
587+ for (lambda_code_obj , parent_frame ) in self .lambda_closures .items ():
588+ if lambda_code_obj == frame .f_code :
589+ # TODO: should we do more verification like above?!?
590+ return parent_frame
591+
579592 return None
580593
581594
@@ -689,7 +702,7 @@ def interaction(self, frame, traceback, event_type):
689702 print >> sys.stderr, '=== STACK ===', 'curindex:', self.curindex
690703 for (e,ln) in self.stack:
691704 print >> sys.stderr, e.f_code.co_name + ' ' + e.f_code.co_filename + ' ' + str(ln)
692- print >> sys.stderr, "top_frame", top_frame.f_code.co_name
705+ print >> sys.stderr, "top_frame", top_frame.f_code.co_name, top_frame.f_code
693706 '''
694707
695708
@@ -793,6 +806,7 @@ def interaction(self, frame, traceback, event_type):
793806
794807 # returns a dict with keys: function name, frame id, id of parent frame, encoded_locals dict
795808 def create_encoded_stack_entry (cur_frame ):
809+ #print >> sys.stderr, '- create_encoded_stack_entry', cur_frame, self.closures, self.lambda_closures
796810 ret = {}
797811
798812
@@ -956,6 +970,21 @@ def create_encoded_stack_entry(cur_frame):
956970 self .parent_frames_set .add (chosen_parent_frame ) # unequivocally add to this set!!!
957971 if not chosen_parent_frame in self .zombie_frames :
958972 self .zombie_frames .append (chosen_parent_frame )
973+ else :
974+ # look for code objects of lambdas defined within this
975+ # function, which comes up in cases like line 2 of:
976+ # def x(y):
977+ # (lambda z: lambda w: z+y)(y)
978+ #
979+ # x(42)
980+ if top_frame .f_code .co_consts :
981+ for e in top_frame .f_code .co_consts :
982+ if type (e ) == types .CodeType and e .co_name == '<lambda>' :
983+ # TODO: what if it's already in lambda_closures?
984+ self .lambda_closures [e ] = top_frame
985+ self .parent_frames_set .add (top_frame ) # copy-paste from above
986+ if not top_frame in self .zombie_frames :
987+ self .zombie_frames .append (top_frame )
959988 else :
960989 # if there is only a global scope visible ...
961990 for (k , v ) in get_user_globals (top_frame ).items ():
0 commit comments