Skip to content

Instantly share code, notes, and snippets.

@simon-weber
Created June 15, 2017 22:06
Show Gist options
  • Save simon-weber/8d43e33448684f85718417ce1a072bc8 to your computer and use it in GitHub Desktop.
Save simon-weber/8d43e33448684f85718417ce1a072bc8 to your computer and use it in GitHub Desktop.
disabling profiling around python-libfaketime example
# An example to disable profiling when faking time with python-libfaketime.
# Call this once from setup_package or a similar hook at the start of the test run.
# https://github.com/simon-weber/python-libfaketime
def disable_profiling_around_libfaketime(nose_config):
"""
libfaketime's interception of time system calls will break profilers.
If we detect that we're running under a profiler, we set up libfaketime callbacks to
temporarily disable profiling when we're faking the time.
"""
try:
import nose_cprofile
except ImportError:
return
profile_plugins = [p for p in nose_config.plugins.plugins if isinstance(p, nose_cprofile.Profile)]
if not profile_plugins:
return
assert len(profile_plugins) == 1
plugin = profile_plugins[0]
if plugin.enabled:
profiler = plugin.prof
libfaketime.begin_callback = lambda _: profiler.disable()
libfaketime.end_callback = lambda _: profiler.enable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment