Skip to content

Commit 14b6ca5

Browse files
committed
skip fake function calls associated with class defs
1 parent 4f6dd67 commit 14b6ca5

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

‎v3/pg_logger.py‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656

5757
BREAKPOINT_STR = '#break'
5858

59+
CLASS_RE = re.compile('class\s+')
60+
5961

6062
# simple sandboxing scheme:
6163
#
@@ -303,6 +305,11 @@ def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalize
303305
# if this is true, don't put any more stuff into self.trace
304306
self.done = False
305307

308+
# if this is non-null, don't do any more tracing until a
309+
# 'return' instruction with a stack gotten from
310+
# get_stack_code_IDs() that matches wait_for_return_stack
311+
self.wait_for_return_stack = None
312+
306313
#http://stackoverflow.com/questions/2112396/in-python-in-google-app-engine-how-do-you-capture-output-produced-by-the-print
307314
self.GAE_STDOUT = sys.stdout
308315

@@ -418,6 +425,10 @@ def setup(self, f, t):
418425
self.stack, self.curindex = self.get_stack(f, t)
419426
self.curframe = self.stack[self.curindex][0]
420427

428+
# should be a reasonably unique ID to match calls and returns:
429+
def get_stack_code_IDs(self):
430+
return [id(e[0].f_code) for e in self.stack]
431+
421432

422433
# Override Bdb methods
423434

@@ -479,6 +490,8 @@ def user_exception(self, frame, exc_info):
479490
else:
480491
self.interaction(frame, exc_traceback, 'exception')
481492

493+
def get_script_line(self, n):
494+
return self.executed_script_lines[n-1]
482495

483496
# General interaction function
484497

@@ -539,10 +552,26 @@ def interaction(self, frame, traceback, event_type):
539552
'''
540553

541554

542-
# don't trace inside of our __restricted_import__ helper function
543-
# (this check is now subsumed by the above check)
544-
#if top_frame.f_code.co_name == '__restricted_import__':
545-
# return
555+
# don't trace if wait_for_return_stack is non-null ...
556+
if self.wait_for_return_stack:
557+
if event_type == 'return' and \
558+
(self.wait_for_return_stack == self.get_stack_code_IDs()):
559+
self.wait_for_return_stack = None # reset!
560+
return # always bail!
561+
else:
562+
# Skip all "calls" that are actually class definitions, since
563+
# those faux calls produce lots of ugly cruft in the trace.
564+
#
565+
# NB: Only trigger on calls to functions defined in
566+
# user-written code (i.e., co_filename == '<string>'), but that
567+
# should already be ensured by the above check for whether we're
568+
# in user-written code.
569+
if event_type == 'call':
570+
func_line = self.get_script_line(top_frame.f_code.co_firstlineno)
571+
if CLASS_RE.match(func_line.lstrip()): # ignore leading spaces
572+
self.wait_for_return_stack = self.get_stack_code_IDs()
573+
return
574+
546575

547576
self.encoder.reset_heap() # VERY VERY VERY IMPORTANT,
548577
# or else we won't properly capture heap object mutations in the trace!
@@ -892,8 +921,9 @@ def create_encoded_stack_entry(cur_frame):
892921

893922
def _runscript(self, script_str):
894923
self.executed_script = script_str
924+
self.executed_script_lines = self.executed_script.splitlines()
895925

896-
for (i, line) in enumerate(self.executed_script.splitlines()):
926+
for (i, line) in enumerate(self.executed_script_lines):
897927
line_no = i + 1
898928
if line.endswith(BREAKPOINT_STR):
899929
self.breakpoints.append(line_no)

0 commit comments

Comments
 (0)