Skip to content
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

[core] Add import hook module #769

Merged
merged 20 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ddtrace/utils/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def register_post_import_hook(name, hook):
sys.meta_path.insert(0, ImportHookFinder())

hooks = _post_import_hooks.get(name, [])

if hook in hooks:
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
log.debug('hook "{}" already exists on module "{}"'.format(hook, name))
return

module = sys.modules.get(name, None)

# If the module has been imported already fire the hook and log a debug msg.
Expand Down
1 change: 1 addition & 0 deletions tests/subprocesstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _run_test_in_subprocess(self, result):
raise Exception('Subprocess Test "{}" Failed'.format(cmdf))
except Exception:
exc_info = sys.exc_info()

sys.stderr.write(stderr.decode())
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
sys.stdout.write(stdout.decode())
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
result.addFailure(self, exc_info)
Expand Down
25 changes: 18 additions & 7 deletions tests/utils/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,20 @@ def test_register_post_import_hook_duplicate_register(self):
Test that a function can be registered as a hook twice.
"""
test_hook = mock.MagicMock()
register_post_import_hook('tests.utils.test_module', test_hook)
register_post_import_hook('tests.utils.test_module', test_hook)
import tests.utils.test_module # noqa
self.assertEqual(test_hook.call_count, 2)
with mock.patch('ddtrace.utils.hook.log') as log_mock:
register_post_import_hook('tests.utils.test_module', test_hook)
register_post_import_hook('tests.utils.test_module', test_hook)
import tests.utils.test_module # noqa

class Matcher(object):
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
def __eq__(self, other):
return 'already exists on module "tests.utils.test_module"'

calls = [
mock.call(Matcher())
]
self.assertEqual(test_hook.call_count, 1)
log_mock.debug.assert_has_calls(calls)

def test_deregister_post_import_hook_no_register(self):
"""
Expand Down Expand Up @@ -128,8 +138,9 @@ def test_deregister_post_import_hook_after_register_multiple_one_match(self):
"""
Test that only specified import hooks can be deregistered after being registered.
"""
test_hook = mock.MagicMock()
test_hook2 = mock.MagicMock()
# Enforce a spec so that hasattr doesn't vacuously return True.
test_hook = mock.MagicMock(spec=[])
test_hook2 = mock.MagicMock(spec=[])
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
setattr(test_hook, '_test', True)
setattr(test_hook2, '_test2', True)
register_post_import_hook('tests.utils.test_module', test_hook)
Expand All @@ -141,7 +152,7 @@ def matcher(hook):
deregister_post_import_hook('tests.utils.test_module', matcher)
import tests.utils.test_module # noqa
self.assertEqual(test_hook.call_count, 0, 'hook has been deregistered and should be removed')
self.assertEqual(test_hook2.call_count, 0, 'hook has been deregistered and should be removed')
self.assertEqual(test_hook2.call_count, 1, 'hook should have been called')

def test_deregister_post_import_hook_after_import(self):
"""
Expand Down