Return True if this frame should be traced, False if tracing should be blocked.
(code, absolute_filename)
| 29 | |
| 30 | |
| 31 | def default_should_trace_hook(code, absolute_filename): |
| 32 | """ |
| 33 | Return True if this frame should be traced, False if tracing should be blocked. |
| 34 | """ |
| 35 | # First, check whether this code object has a cached value |
| 36 | ignored_lines = _filename_to_ignored_lines.get(absolute_filename) |
| 37 | if ignored_lines is None: |
| 38 | # Now, look up that line of code and check for a @DontTrace |
| 39 | # preceding or on the same line as the method. |
| 40 | # E.g.: |
| 41 | # #@DontTrace |
| 42 | # def test(): |
| 43 | # pass |
| 44 | # ... or ... |
| 45 | # def test(): #@DontTrace |
| 46 | # pass |
| 47 | ignored_lines = {} |
| 48 | lines = linecache.getlines(absolute_filename) |
| 49 | for i_line, line in enumerate(lines): |
| 50 | j = line.find("#") |
| 51 | if j >= 0: |
| 52 | comment = line[j:] |
| 53 | if DONT_TRACE_TAG in comment: |
| 54 | ignored_lines[i_line] = 1 |
| 55 | |
| 56 | # Note: when it's found in the comment, mark it up and down for the decorator lines found. |
| 57 | k = i_line - 1 |
| 58 | while k >= 0: |
| 59 | if RE_DECORATOR.match(lines[k]): |
| 60 | ignored_lines[k] = 1 |
| 61 | k -= 1 |
| 62 | else: |
| 63 | break |
| 64 | |
| 65 | k = i_line + 1 |
| 66 | while k <= len(lines): |
| 67 | if RE_DECORATOR.match(lines[k]): |
| 68 | ignored_lines[k] = 1 |
| 69 | k += 1 |
| 70 | else: |
| 71 | break |
| 72 | |
| 73 | _filename_to_ignored_lines[absolute_filename] = ignored_lines |
| 74 | |
| 75 | func_line = code.co_firstlineno - 1 # co_firstlineno is 1-based, so -1 is needed |
| 76 | return not ( |
| 77 | func_line - 1 in ignored_lines # -1 to get line before method |
| 78 | or func_line in ignored_lines |
| 79 | ) # method line |
| 80 | |
| 81 | |
| 82 | should_trace_hook = None |