{"action":"created","comment":{"url":"https://api.github.com/repos/stb-tester/stb-tester/pulls/comments/54122339","id":54122339,"diff_hunk":"@@ -211,6 +215,36 @@ def write(self, data):\n                     \"Error processing state change: %s\" % str(e))\n \n \n+def activate(test_script, trace_file=None):\n+    \"\"\"Activate the state-watch tracer.\"\"\"\n+    global _TRACER\n+    _TRACER = new_state_sender(trace_file)  # pylint: disable=W0212\n+\n+    absfilepath, funcname, function = utils.parse_test_uri(test_script)\n+\n+    def tracefunc(frame_, event, _):\n+        if event == \"line\" and frame_.f_code.co_filename == absfilepath:\n+            _TRACER.log_current_line(frame_.f_code.co_filename, frame_.f_lineno)\n+        return tracefunc\n+\n+    sys.settrace(tracefunc)\n+    _TRACER.log_test_starting(\n+        test_script, os.path.relpath(absfilepath), funcname,\n+        function.func_code.co_firstlineno if function is not None else 1)\n+\n+\n+def deactivate():\n+    \"\"\"Deactivate the state-watch tracer.\n+\n+\n+    If the tracer has not been `activate`d, this function is a no-op.\n+    \"\"\"\n+    if _TRACER is not None:\n+        sys.settrace(None)\n+        _TRACER.log_test_ended()\n+        _TRACER.close()","path":"_stbt/state_watch.py","position":42,"original_position":42,"commit_id":"9402ccc2cac720ed6070d1c49314ada57d9fab4a","original_commit_id":"9402ccc2cac720ed6070d1c49314ada57d9fab4a","user":{"login":"wmanley","id":494659,"avatar_url":"https://avatars.githubusercontent.com/u/494659?v=3","gravatar_id":"","url":"https://api.github.com/users/wmanley","html_url":"https://github.com/wmanley","followers_url":"https://api.github.com/users/wmanley/followers","following_url":"https://api.github.com/users/wmanley/following{/other_user}","gists_url":"https://api.github.com/users/wmanley/gists{/gist_id}","starred_url":"https://api.github.com/users/wmanley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wmanley/subscriptions","organizations_url":"https://api.github.com/users/wmanley/orgs","repos_url":"https://api.github.com/users/wmanley/repos","events_url":"https://api.github.com/users/wmanley/events{/privacy}","received_events_url":"https://api.github.com/users/wmanley/received_events","type":"User","site_admin":false},"body":"> Seems like a fair bit of extra work if it's simply to work around having a global variable?\r\n\r\nI doesn't seem too difficult to me.  Either way I've been putting in a bunch of effort to remove global state (#297 #333) so I don't want to merge something that introduces more.\r\n\r\n> It's not like you would/could instantiate multiple tracers because of the sys.settrace limitation, and ultimately it's not user-facing\r\n\r\n[`sys.settrace` docs](https://docs.python.org/2/library/sys.html#sys.settrace) say:\r\n\r\n> The function is thread-specific; for a debugger to support multiple threads, it must be registered using settrace() for each thread being debugged.\r\n\r\nAnd we could use `sys.gettrace` to chain the trace functions together, not that I'm suggesting that this would be necessary for this pull request.\r\n\r\n> much nicer to read (and easier to understand) than\r\n> \r\n>     if _tracer is not None:\r\n>         _tracer.__exit__(None, None, None)\r\n￼\r\nNote: Under my proposed scheme `_tracer` in `stbt-run` would never be `None`, it just might not install a trace handler.","created_at":"2016-02-25T16:30:54Z","updated_at":"2016-02-25T16:30:54Z","html_url":"https://github.com/stb-tester/stb-tester/pull/334#discussion_r54122339","pull_request_url":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334","_links":{"self":{"href":"https://api.github.com/repos/stb-tester/stb-tester/pulls/comments/54122339"},"html":{"href":"https://github.com/stb-tester/stb-tester/pull/334#discussion_r54122339"},"pull_request":{"href":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334"}}},"pull_request":{"url":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334","id":60670030,"html_url":"https://github.com/stb-tester/stb-tester/pull/334","diff_url":"https://github.com/stb-tester/stb-tester/pull/334.diff","patch_url":"https://github.com/stb-tester/stb-tester/pull/334.patch","issue_url":"https://api.github.com/repos/stb-tester/stb-tester/issues/334","number":334,"state":"open","locked":false,"title":"stbt-run: Don't install a trace handler if it's not necessary","user":{"login":"LewisHaley","id":3766135,"avatar_url":"https://avatars.githubusercontent.com/u/3766135?v=3","gravatar_id":"","url":"https://api.github.com/users/LewisHaley","html_url":"https://github.com/LewisHaley","followers_url":"https://api.github.com/users/LewisHaley/followers","following_url":"https://api.github.com/users/LewisHaley/following{/other_user}","gists_url":"https://api.github.com/users/LewisHaley/gists{/gist_id}","starred_url":"https://api.github.com/users/LewisHaley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LewisHaley/subscriptions","organizations_url":"https://api.github.com/users/LewisHaley/orgs","repos_url":"https://api.github.com/users/LewisHaley/repos","events_url":"https://api.github.com/users/LewisHaley/events{/privacy}","received_events_url":"https://api.github.com/users/LewisHaley/received_events","type":"User","site_admin":false},"body":"We're attempting to forward-port our own patches onto a new version of STBT, and in doing so found a clash between our trace and stbt's build in tracer (https://github.com/stb-tester/stb-tester/commit/fa60ea989c9d87e82af04eaaab76cfe6409d194b). The other commits are tidy-ups.\r\n\r\nI wasn't able to get `make check` to pass fully because of failures with the virtual-stb tests: \r\n> /usr/libexec/Xorg.wrap: Only console users are allowed to run the X server","created_at":"2016-02-25T12:19:20Z","updated_at":"2016-02-25T16:30:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"25dfce30bd58d67d39a22b2d799bb7167dce193a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334/commits","review_comments_url":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334/comments","review_comment_url":"https://api.github.com/repos/stb-tester/stb-tester/pulls/comments{/number}","comments_url":"https://api.github.com/repos/stb-tester/stb-tester/issues/334/comments","statuses_url":"https://api.github.com/repos/stb-tester/stb-tester/statuses/9402ccc2cac720ed6070d1c49314ada57d9fab4a","head":{"label":"LewisHaley:refactor-stbt-run","ref":"refactor-stbt-run","sha":"9402ccc2cac720ed6070d1c49314ada57d9fab4a","user":{"login":"LewisHaley","id":3766135,"avatar_url":"https://avatars.githubusercontent.com/u/3766135?v=3","gravatar_id":"","url":"https://api.github.com/users/LewisHaley","html_url":"https://github.com/LewisHaley","followers_url":"https://api.github.com/users/LewisHaley/followers","following_url":"https://api.github.com/users/LewisHaley/following{/other_user}","gists_url":"https://api.github.com/users/LewisHaley/gists{/gist_id}","starred_url":"https://api.github.com/users/LewisHaley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LewisHaley/subscriptions","organizations_url":"https://api.github.com/users/LewisHaley/orgs","repos_url":"https://api.github.com/users/LewisHaley/repos","events_url":"https://api.github.com/users/LewisHaley/events{/privacy}","received_events_url":"https://api.github.com/users/LewisHaley/received_events","type":"User","site_admin":false},"repo":{"id":8608956,"name":"stb-tester","full_name":"LewisHaley/stb-tester","owner":{"login":"LewisHaley","id":3766135,"avatar_url":"https://avatars.githubusercontent.com/u/3766135?v=3","gravatar_id":"","url":"https://api.github.com/users/LewisHaley","html_url":"https://github.com/LewisHaley","followers_url":"https://api.github.com/users/LewisHaley/followers","following_url":"https://api.github.com/users/LewisHaley/following{/other_user}","gists_url":"https://api.github.com/users/LewisHaley/gists{/gist_id}","starred_url":"https://api.github.com/users/LewisHaley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LewisHaley/subscriptions","organizations_url":"https://api.github.com/users/LewisHaley/orgs","repos_url":"https://api.github.com/users/LewisHaley/repos","events_url":"https://api.github.com/users/LewisHaley/events{/privacy}","received_events_url":"https://api.github.com/users/LewisHaley/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/LewisHaley/stb-tester","description":"A video-capture record/playback system for automated testing of set-top boxes","fork":true,"url":"https://api.github.com/repos/LewisHaley/stb-tester","forks_url":"https://api.github.com/repos/LewisHaley/stb-tester/forks","keys_url":"https://api.github.com/repos/LewisHaley/stb-tester/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LewisHaley/stb-tester/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LewisHaley/stb-tester/teams","hooks_url":"https://api.github.com/repos/LewisHaley/stb-tester/hooks","issue_events_url":"https://api.github.com/repos/LewisHaley/stb-tester/issues/events{/number}","events_url":"https://api.github.com/repos/LewisHaley/stb-tester/events","assignees_url":"https://api.github.com/repos/LewisHaley/stb-tester/assignees{/user}","branches_url":"https://api.github.com/repos/LewisHaley/stb-tester/branches{/branch}","tags_url":"https://api.github.com/repos/LewisHaley/stb-tester/tags","blobs_url":"https://api.github.com/repos/LewisHaley/stb-tester/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LewisHaley/stb-tester/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LewisHaley/stb-tester/git/refs{/sha}","trees_url":"https://api.github.com/repos/LewisHaley/stb-tester/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LewisHaley/stb-tester/statuses/{sha}","languages_url":"https://api.github.com/repos/LewisHaley/stb-tester/languages","stargazers_url":"https://api.github.com/repos/LewisHaley/stb-tester/stargazers","contributors_url":"https://api.github.com/repos/LewisHaley/stb-tester/contributors","subscribers_url":"https://api.github.com/repos/LewisHaley/stb-tester/subscribers","subscription_url":"https://api.github.com/repos/LewisHaley/stb-tester/subscription","commits_url":"https://api.github.com/repos/LewisHaley/stb-tester/commits{/sha}","git_commits_url":"https://api.github.com/repos/LewisHaley/stb-tester/git/commits{/sha}","comments_url":"https://api.github.com/repos/LewisHaley/stb-tester/comments{/number}","issue_comment_url":"https://api.github.com/repos/LewisHaley/stb-tester/issues/comments{/number}","contents_url":"https://api.github.com/repos/LewisHaley/stb-tester/contents/{+path}","compare_url":"https://api.github.com/repos/LewisHaley/stb-tester/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LewisHaley/stb-tester/merges","archive_url":"https://api.github.com/repos/LewisHaley/stb-tester/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LewisHaley/stb-tester/downloads","issues_url":"https://api.github.com/repos/LewisHaley/stb-tester/issues{/number}","pulls_url":"https://api.github.com/repos/LewisHaley/stb-tester/pulls{/number}","milestones_url":"https://api.github.com/repos/LewisHaley/stb-tester/milestones{/number}","notifications_url":"https://api.github.com/repos/LewisHaley/stb-tester/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LewisHaley/stb-tester/labels{/name}","releases_url":"https://api.github.com/repos/LewisHaley/stb-tester/releases{/id}","deployments_url":"https://api.github.com/repos/LewisHaley/stb-tester/deployments","created_at":"2013-03-06T17:25:56Z","updated_at":"2014-12-30T09:48:59Z","pushed_at":"2016-02-25T12:34:59Z","git_url":"git://github.com/LewisHaley/stb-tester.git","ssh_url":"git@github.com:LewisHaley/stb-tester.git","clone_url":"https://github.com/LewisHaley/stb-tester.git","svn_url":"https://github.com/LewisHaley/stb-tester","homepage":"http://stb-tester.com","size":6946,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"stb-tester:master","ref":"master","sha":"582acc887e0053520cffdb4df7adbcc4263a4cfd","user":{"login":"stb-tester","id":5528358,"avatar_url":"https://avatars.githubusercontent.com/u/5528358?v=3","gravatar_id":"","url":"https://api.github.com/users/stb-tester","html_url":"https://github.com/stb-tester","followers_url":"https://api.github.com/users/stb-tester/followers","following_url":"https://api.github.com/users/stb-tester/following{/other_user}","gists_url":"https://api.github.com/users/stb-tester/gists{/gist_id}","starred_url":"https://api.github.com/users/stb-tester/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stb-tester/subscriptions","organizations_url":"https://api.github.com/users/stb-tester/orgs","repos_url":"https://api.github.com/users/stb-tester/repos","events_url":"https://api.github.com/users/stb-tester/events{/privacy}","received_events_url":"https://api.github.com/users/stb-tester/received_events","type":"Organization","site_admin":false},"repo":{"id":4739298,"name":"stb-tester","full_name":"stb-tester/stb-tester","owner":{"login":"stb-tester","id":5528358,"avatar_url":"https://avatars.githubusercontent.com/u/5528358?v=3","gravatar_id":"","url":"https://api.github.com/users/stb-tester","html_url":"https://github.com/stb-tester","followers_url":"https://api.github.com/users/stb-tester/followers","following_url":"https://api.github.com/users/stb-tester/following{/other_user}","gists_url":"https://api.github.com/users/stb-tester/gists{/gist_id}","starred_url":"https://api.github.com/users/stb-tester/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stb-tester/subscriptions","organizations_url":"https://api.github.com/users/stb-tester/orgs","repos_url":"https://api.github.com/users/stb-tester/repos","events_url":"https://api.github.com/users/stb-tester/events{/privacy}","received_events_url":"https://api.github.com/users/stb-tester/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/stb-tester/stb-tester","description":"Automated Testing for Set-Top Boxes and Smart TVs","fork":false,"url":"https://api.github.com/repos/stb-tester/stb-tester","forks_url":"https://api.github.com/repos/stb-tester/stb-tester/forks","keys_url":"https://api.github.com/repos/stb-tester/stb-tester/keys{/key_id}","collaborators_url":"https://api.github.com/repos/stb-tester/stb-tester/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/stb-tester/stb-tester/teams","hooks_url":"https://api.github.com/repos/stb-tester/stb-tester/hooks","issue_events_url":"https://api.github.com/repos/stb-tester/stb-tester/issues/events{/number}","events_url":"https://api.github.com/repos/stb-tester/stb-tester/events","assignees_url":"https://api.github.com/repos/stb-tester/stb-tester/assignees{/user}","branches_url":"https://api.github.com/repos/stb-tester/stb-tester/branches{/branch}","tags_url":"https://api.github.com/repos/stb-tester/stb-tester/tags","blobs_url":"https://api.github.com/repos/stb-tester/stb-tester/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/stb-tester/stb-tester/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/stb-tester/stb-tester/git/refs{/sha}","trees_url":"https://api.github.com/repos/stb-tester/stb-tester/git/trees{/sha}","statuses_url":"https://api.github.com/repos/stb-tester/stb-tester/statuses/{sha}","languages_url":"https://api.github.com/repos/stb-tester/stb-tester/languages","stargazers_url":"https://api.github.com/repos/stb-tester/stb-tester/stargazers","contributors_url":"https://api.github.com/repos/stb-tester/stb-tester/contributors","subscribers_url":"https://api.github.com/repos/stb-tester/stb-tester/subscribers","subscription_url":"https://api.github.com/repos/stb-tester/stb-tester/subscription","commits_url":"https://api.github.com/repos/stb-tester/stb-tester/commits{/sha}","git_commits_url":"https://api.github.com/repos/stb-tester/stb-tester/git/commits{/sha}","comments_url":"https://api.github.com/repos/stb-tester/stb-tester/comments{/number}","issue_comment_url":"https://api.github.com/repos/stb-tester/stb-tester/issues/comments{/number}","contents_url":"https://api.github.com/repos/stb-tester/stb-tester/contents/{+path}","compare_url":"https://api.github.com/repos/stb-tester/stb-tester/compare/{base}...{head}","merges_url":"https://api.github.com/repos/stb-tester/stb-tester/merges","archive_url":"https://api.github.com/repos/stb-tester/stb-tester/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/stb-tester/stb-tester/downloads","issues_url":"https://api.github.com/repos/stb-tester/stb-tester/issues{/number}","pulls_url":"https://api.github.com/repos/stb-tester/stb-tester/pulls{/number}","milestones_url":"https://api.github.com/repos/stb-tester/stb-tester/milestones{/number}","notifications_url":"https://api.github.com/repos/stb-tester/stb-tester/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/stb-tester/stb-tester/labels{/name}","releases_url":"https://api.github.com/repos/stb-tester/stb-tester/releases{/id}","deployments_url":"https://api.github.com/repos/stb-tester/stb-tester/deployments","created_at":"2012-06-21T13:11:28Z","updated_at":"2016-01-27T15:03:44Z","pushed_at":"2016-02-25T12:34:59Z","git_url":"git://github.com/stb-tester/stb-tester.git","ssh_url":"git@github.com:stb-tester/stb-tester.git","clone_url":"https://github.com/stb-tester/stb-tester.git","svn_url":"https://github.com/stb-tester/stb-tester","homepage":"http://stb-tester.com","size":23506,"stargazers_count":50,"watchers_count":50,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":46,"mirror_url":null,"open_issues_count":22,"forks":46,"open_issues":22,"watchers":50,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334"},"html":{"href":"https://github.com/stb-tester/stb-tester/pull/334"},"issue":{"href":"https://api.github.com/repos/stb-tester/stb-tester/issues/334"},"comments":{"href":"https://api.github.com/repos/stb-tester/stb-tester/issues/334/comments"},"review_comments":{"href":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334/comments"},"review_comment":{"href":"https://api.github.com/repos/stb-tester/stb-tester/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/stb-tester/stb-tester/pulls/334/commits"},"statuses":{"href":"https://api.github.com/repos/stb-tester/stb-tester/statuses/9402ccc2cac720ed6070d1c49314ada57d9fab4a"}}}}