|
56 | 56 |
|
57 | 57 | BREAKPOINT_STR = '#break' |
58 | 58 |
|
| 59 | +CLASS_RE = re.compile('class\s+') |
| 60 | + |
59 | 61 |
|
60 | 62 | # simple sandboxing scheme: |
61 | 63 | # |
@@ -303,6 +305,11 @@ def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalize |
303 | 305 | # if this is true, don't put any more stuff into self.trace |
304 | 306 | self.done = False |
305 | 307 |
|
| 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 | + |
306 | 313 | #http://stackoverflow.com/questions/2112396/in-python-in-google-app-engine-how-do-you-capture-output-produced-by-the-print |
307 | 314 | self.GAE_STDOUT = sys.stdout |
308 | 315 |
|
@@ -418,6 +425,10 @@ def setup(self, f, t): |
418 | 425 | self.stack, self.curindex = self.get_stack(f, t) |
419 | 426 | self.curframe = self.stack[self.curindex][0] |
420 | 427 |
|
| 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 | + |
421 | 432 |
|
422 | 433 | # Override Bdb methods |
423 | 434 |
|
@@ -479,6 +490,8 @@ def user_exception(self, frame, exc_info): |
479 | 490 | else: |
480 | 491 | self.interaction(frame, exc_traceback, 'exception') |
481 | 492 |
|
| 493 | + def get_script_line(self, n): |
| 494 | + return self.executed_script_lines[n-1] |
482 | 495 |
|
483 | 496 | # General interaction function |
484 | 497 |
|
@@ -539,10 +552,26 @@ def interaction(self, frame, traceback, event_type): |
539 | 552 | ''' |
540 | 553 |
|
541 | 554 |
|
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 | + |
546 | 575 |
|
547 | 576 | self.encoder.reset_heap() # VERY VERY VERY IMPORTANT, |
548 | 577 | # or else we won't properly capture heap object mutations in the trace! |
@@ -892,8 +921,9 @@ def create_encoded_stack_entry(cur_frame): |
892 | 921 |
|
893 | 922 | def _runscript(self, script_str): |
894 | 923 | self.executed_script = script_str |
| 924 | + self.executed_script_lines = self.executed_script.splitlines() |
895 | 925 |
|
896 | | - for (i, line) in enumerate(self.executed_script.splitlines()): |
| 926 | + for (i, line) in enumerate(self.executed_script_lines): |
897 | 927 | line_no = i + 1 |
898 | 928 | if line.endswith(BREAKPOINT_STR): |
899 | 929 | self.breakpoints.append(line_no) |
|
0 commit comments