Skip to content

Commit 998b5e1

Browse files
committed
RECURSIVELY dive inside of objects reachable from top of stack
in order to find lambdas and other nested functions ... subtle!
1 parent 7a4722d commit 998b5e1

9 files changed

Lines changed: 6393 additions & 22 deletions

‎v3/pg_encoder.py‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@
6868
long = None # Avoid NameError when evaluating "long"
6969

7070

71+
def is_class(dat):
72+
"""Return whether dat is a class."""
73+
if is_python3:
74+
return isinstance(dat, type)
75+
else:
76+
return type(dat) in (types.ClassType, types.TypeType)
77+
78+
79+
def is_instance(dat):
80+
"""Return whether dat is an instance of a class."""
81+
if is_python3:
82+
return isinstance(type(dat), type) and not isinstance(dat, type)
83+
else:
84+
# ugh, classRE match is a bit of a hack :(
85+
return type(dat) == types.InstanceType or classRE.match(str(type(dat)))
86+
87+
7188
def get_name(obj):
7289
"""Return the name of an object."""
7390
return obj.__name__ if hasattr(obj, '__name__') else get_name(type(obj))
@@ -182,7 +199,7 @@ def encode(self, dat):
182199
elif typ is types.BuiltinFunctionType:
183200
pretty_name = get_name(dat) + '(...)'
184201
new_obj.extend(['FUNCTION', pretty_name, None])
185-
elif self.is_class(dat) or self.is_instance(dat):
202+
elif is_class(dat) or is_instance(dat):
186203
self.encode_class_or_instance(dat, new_obj)
187204
elif typ is types.ModuleType:
188205
new_obj.extend(['module', dat.__name__])
@@ -199,26 +216,9 @@ def encode(self, dat):
199216
return ret
200217

201218

202-
def is_class(self, dat):
203-
"""Return whether dat is a class."""
204-
if is_python3:
205-
return isinstance(dat, type)
206-
else:
207-
return type(dat) in (types.ClassType, types.TypeType)
208-
209-
210-
def is_instance(self, dat):
211-
"""Return whether dat is an instance of a class."""
212-
if is_python3:
213-
return isinstance(type(dat), type) and not isinstance(dat, type)
214-
else:
215-
# ugh, classRE match is a bit of a hack :(
216-
return type(dat) == types.InstanceType or classRE.match(str(type(dat)))
217-
218-
219219
def encode_class_or_instance(self, dat, new_obj):
220220
"""Encode dat as a class or instance."""
221-
if self.is_instance(dat):
221+
if is_instance(dat):
222222
class_name = get_name(dat.__class__)
223223
new_obj.extend(['INSTANCE', class_name])
224224
# don't traverse inside modules, or else risk EXPLODING the visualization

‎v3/pg_logger.py‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,58 @@ def filter_var_dict(d):
134134
return ret
135135

136136

137+
# yield all function objects locally-reachable from frame,
138+
# making sure to traverse inside all compound objects ...
139+
def visit_all_locally_reachable_function_objs(frame):
140+
for (k, v) in get_user_locals(frame).items():
141+
for e in visit_function_obj(v, set()):
142+
if e: # only non-null if it's a function object
143+
assert type(e) in (types.FunctionType, types.MethodType)
144+
yield e
145+
146+
147+
# TODO: this might be slow if we're traversing inside lots of objects:
148+
def visit_function_obj(v, ids_seen_set):
149+
v_id = id(v)
150+
151+
# to prevent infinite loop
152+
if v_id in ids_seen_set:
153+
yield None
154+
else:
155+
ids_seen_set.add(v_id)
156+
157+
typ = type(v)
158+
159+
# simple base case
160+
if typ in (types.FunctionType, types.MethodType):
161+
yield v
162+
163+
# recursive cases
164+
elif typ in (list, tuple, set):
165+
for child in v:
166+
for child_res in visit_function_obj(child, ids_seen_set):
167+
yield child_res
168+
169+
elif typ == dict or pg_encoder.is_class(v) or pg_encoder.is_instance(v):
170+
contents_dict = None
171+
172+
if typ == dict:
173+
contents_dict = v
174+
# warning: some classes or instances don't have __dict__ attributes
175+
elif hasattr(v, '__dict__'):
176+
contents_dict = v.__dict__
177+
178+
if contents_dict:
179+
for (key_child, val_child) in contents_dict.items():
180+
for key_child_res in visit_function_obj(key_child, ids_seen_set):
181+
yield key_child_res
182+
for val_child_res in visit_function_obj(val_child, ids_seen_set):
183+
yield val_child_res
184+
185+
# degenerate base case
186+
yield None
187+
188+
137189
class PGLogger(bdb.Bdb):
138190

139191
def __init__(self, cumulative_mode, finalizer_func):
@@ -474,9 +526,8 @@ def create_encoded_stack_entry(cur_frame):
474526
# look for whether a nested function has been defined during
475527
# this particular call:
476528
if i > 1: # i == 1 implies that there's only a global scope visible
477-
for (k, v) in get_user_locals(top_frame).items():
478-
if (type(v) in (types.FunctionType, types.MethodType) and \
479-
v not in self.closures and \
529+
for v in visit_all_locally_reachable_function_objs(top_frame):
530+
if (v not in self.closures and \
480531
v not in self.globally_defined_funcs):
481532

482533
# Look for the presence of the code object (v.func_code

0 commit comments

Comments
 (0)