Skip to content
Browse files

fix for profile functions in a trace (and other cases where the frame…

… changes without hte depth changing)
  • Loading branch information...
1 parent 7f629cd commit 195c56ea99272324ffb6fb33ae96d3239663c10f @alex committed Mar 4, 2012
Showing with 39 additions and 8 deletions.
  1. +30 −1 client/tests/test_hook.py
  2. +9 −7 client/tracebin/traces.py
View
31 client/tests/test_hook.py
@@ -214,4 +214,33 @@ def main():
assert call.func_name == "main"
[subcall] = call.subcalls
- assert subcall.func_name == "f"
+ assert subcall.func_name == "f"
+
+ def test_trace_profilehook(self):
+ def profile(frame, event, arg):
+ pass
+ def sub(x, y):
+ return x - y
+ def main():
+ i = 1500
+ orig_profile = sys.getprofile()
+ sys.setprofile(profile)
+ try:
+ while i > 0:
+ i = sub(i, 1)
+ finally:
+ sys.setprofile(orig_profile)
+
+ with tracebin.record() as recorder:
+ main()
+
+ [trace] = recorder.traces
+ [_, _, loop] = trace.sections
+
+ py_profile_chunk = loop.chunks[9]
+ assert py_profile_chunk.sourcelines == [
+ """ def profile(frame, event, arg):\n""",
+ """ pass\n""",
+ ]
+ op_profile_chunk = loop.chunks[10]
+ assert op_profile_chunk.get_op_names() == ["debug_merge_point", "debug_merge_point"]
View
16 client/tracebin/traces.py
@@ -32,23 +32,23 @@ def __init__(self, greenkey, ops, asm_start, asm_len):
@classmethod
def split_section(cls, ops):
chunks = []
- cls._split_section(ops, i=0, call_depth=0, chunks=chunks)
+ cls._split_section(ops, i=0, call_id=0, chunks=chunks)
return chunks
@classmethod
- def _split_section(cls, ops, i, call_depth, chunks):
+ def _split_section(cls, ops, i, call_id, chunks):
start_idx = i
current_line = None
while i < len(ops):
op = ops[i]
if op.name == "debug_merge_point":
- if op.call_depth > call_depth:
+ if op.call_id > call_id:
chunks.append(
ResOpChunk(ops[start_idx:i])
)
- i = start_idx = cls._split_section(ops, i, op.call_depth, chunks)
- elif op.call_depth < call_depth:
+ i = start_idx = cls._split_section(ops, i, op.call_id, chunks)
+ elif op.call_id < call_id:
chunks.append(
ResOpChunk(ops[start_idx:i])
)
@@ -65,7 +65,7 @@ def _split_section(cls, ops, i, call_depth, chunks):
)
lines_end = py_op.lineno - startline
if current_line is None:
- source = sourcecode[:lines_end+1]
+ source = sourcecode[:lines_end + 1]
linenos = range(startline, py_op.lineno + 1)
else:
source = [sourcecode[lines_end]]
@@ -74,7 +74,9 @@ def _split_section(cls, ops, i, call_depth, chunks):
current_line = py_op.lineno
start_idx = i
i += 1
- chunks.append(ResOpChunk(ops[start_idx:]))
+ if start_idx < len(ops):
+ chunks.append(ResOpChunk(ops[start_idx:]))
+ return i
def visit(self, visitor):
return visitor.visit_python_trace(self)

0 comments on commit 195c56e

Please sign in to comment.
Something went wrong with that request. Please try again.