-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
bpo-31344: Per-frame control of trace events #3417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ncoghlan
merged 7 commits into
python:master
from
ncoghlan:bpo-31344-per-opcode-trace-events
Sep 8, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a009866
bpo-31344: Per-frame control of trace events
ncoghlan 75659ff
Add What's New entry for tracing changes
ncoghlan 141c829
Fix NEWS formatting
ncoghlan 8c9fcba
Don't repeat docs in versionchanged note
ncoghlan 5b45396
Fix frame size test case
ncoghlan aa83722
Fix comment formatting
ncoghlan 5fa7de4
Address review comments
ncoghlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,16 +234,29 @@ def generator_example(): | |
|
|
||
|
|
||
| class Tracer: | ||
| def __init__(self): | ||
| def __init__(self, trace_line_events=None, trace_opcode_events=None): | ||
| self.trace_line_events = trace_line_events | ||
| self.trace_opcode_events = trace_opcode_events | ||
| self.events = [] | ||
|
|
||
| def _reconfigure_frame(self, frame): | ||
| if self.trace_line_events is not None: | ||
| frame.f_trace_lines = self.trace_line_events | ||
| if self.trace_opcode_events is not None: | ||
| frame.f_trace_opcodes = self.trace_opcode_events | ||
|
|
||
| def trace(self, frame, event, arg): | ||
| self._reconfigure_frame(frame) | ||
| self.events.append((frame.f_lineno, event)) | ||
| return self.trace | ||
|
|
||
| def traceWithGenexp(self, frame, event, arg): | ||
| self._reconfigure_frame(frame) | ||
| (o for o in [1]) | ||
| self.events.append((frame.f_lineno, event)) | ||
| return self.trace | ||
|
|
||
|
|
||
| class TraceTestCase(unittest.TestCase): | ||
|
|
||
| # Disable gc collection when tracing, otherwise the | ||
|
|
@@ -257,6 +270,11 @@ def tearDown(self): | |
| if self.using_gc: | ||
| gc.enable() | ||
|
|
||
| @staticmethod | ||
| def make_tracer(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment mentioning why is this needed at all. (letting subclasses such as XYZ override it) |
||
| """Helper to allow test subclasses to configure tracers differently""" | ||
| return Tracer() | ||
|
|
||
| def compare_events(self, line_offset, events, expected_events): | ||
| events = [(l - line_offset, e) for (l, e) in events] | ||
| if events != expected_events: | ||
|
|
@@ -266,7 +284,7 @@ def compare_events(self, line_offset, events, expected_events): | |
| [str(x) for x in events]))) | ||
|
|
||
| def run_and_compare(self, func, events): | ||
| tracer = Tracer() | ||
| tracer = self.make_tracer() | ||
| sys.settrace(tracer.trace) | ||
| func() | ||
| sys.settrace(None) | ||
|
|
@@ -277,7 +295,7 @@ def run_test(self, func): | |
| self.run_and_compare(func, func.events) | ||
|
|
||
| def run_test2(self, func): | ||
| tracer = Tracer() | ||
| tracer = self.make_tracer() | ||
| func(tracer.trace) | ||
| sys.settrace(None) | ||
| self.compare_events(func.__code__.co_firstlineno, | ||
|
|
@@ -329,7 +347,7 @@ def test_13_genexp(self): | |
| # and if the traced function contains another generator | ||
| # that is not completely exhausted, the trace stopped. | ||
| # Worse: the 'finally' clause was not invoked. | ||
| tracer = Tracer() | ||
| tracer = self.make_tracer() | ||
| sys.settrace(tracer.traceWithGenexp) | ||
| generator_example() | ||
| sys.settrace(None) | ||
|
|
@@ -398,6 +416,34 @@ def func(): | |
| (1, 'line')]) | ||
|
|
||
|
|
||
| class SkipLineEventsTraceTestCase(TraceTestCase): | ||
| """Repeat the trace tests, but with per-line events skipped""" | ||
|
|
||
| def compare_events(self, line_offset, events, expected_events): | ||
| skip_line_events = [e for e in expected_events if e[1] != 'line'] | ||
| super().compare_events(line_offset, events, skip_line_events) | ||
|
|
||
| @staticmethod | ||
| def make_tracer(): | ||
| return Tracer(trace_line_events=False) | ||
|
|
||
|
|
||
| @support.cpython_only | ||
| class TraceOpcodesTestCase(TraceTestCase): | ||
| """Repeat the trace tests, but with per-opcodes events enabled""" | ||
|
|
||
| def compare_events(self, line_offset, events, expected_events): | ||
| skip_opcode_events = [e for e in events if e[1] != 'opcode'] | ||
| if len(events) > 1: | ||
| self.assertLess(len(skip_opcode_events), len(events), | ||
| msg="No 'opcode' events received by the tracer") | ||
| super().compare_events(line_offset, skip_opcode_events, expected_events) | ||
|
|
||
| @staticmethod | ||
| def make_tracer(): | ||
| return Tracer(trace_opcode_events=True) | ||
|
|
||
|
|
||
| class RaisingTraceFuncTestCase(unittest.TestCase): | ||
| def setUp(self): | ||
| self.addCleanup(sys.settrace, sys.gettrace()) | ||
|
|
@@ -846,6 +892,8 @@ class fake_function: | |
| def test_main(): | ||
| support.run_unittest( | ||
| TraceTestCase, | ||
| SkipLineEventsTraceTestCase, | ||
| TraceOpcodesTestCase, | ||
| RaisingTraceFuncTestCase, | ||
| JumpTestCase | ||
| ) | ||
|
|
||
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/Core and Builtins/2017-09-06-20-25-47.bpo-31344.XpFs-q.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| For finer control of tracing behaviour when testing the interpreter, two new | ||
| frame attributes have been added to control the emission of particular trace | ||
| events: ``f_trace_lines`` (``True`` by default) to turn off per-line trace | ||
| events; and ``f_trace_opcodes`` (``False`` by default) to turn on per-opcode | ||
| trace events. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna guess you learned this the hard way. thanks for the comment! :)