Python sys.setprofile() Examples

The following are 30 code examples of sys.setprofile(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sys , or try the search function .
Example #1
Source File: __init__.py    From profiling with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(self):
        if sys.getprofile() is not None:
            # NOTE: There's no threading.getprofile().
            # The profiling function will be stored at threading._profile_hook
            # but it's not documented.
            raise RuntimeError('Another profiler already registered')
        with deferral() as defer:
            self._times_entered.clear()
            self.overhead = 0.0
            sys.setprofile(self._profile)
            defer(sys.setprofile, None)
            threading.setprofile(self._profile)
            defer(threading.setprofile, None)
            self.timer.start(self)
            defer(self.timer.stop)
            yield 
Example #2
Source File: profiler.py    From pyFileFixity with MIT License 6 votes vote down vote up
def start(self):
        self.last_profile_time = timer()

        if self.use_signal:
            try:
                signal.signal(signal.SIGALRM, self._signal)
                # the following tells the system to restart interrupted system calls if they are
                # interrupted before any data has been transferred. This avoids many of the problems
                # related to signals interrupting system calls, see issue #16
                signal.siginterrupt(signal.SIGALRM, False)
            except ValueError:
                raise NotMainThreadError()

            signal.setitimer(signal.ITIMER_REAL, self.interval, 0.0)
        else:
            sys.setprofile(self._profile) 
Example #3
Source File: test_sys_setprofile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example #4
Source File: utils.py    From profiling with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def deferral():
    """Defers a function call when it is being required like Go.

    ::

       with deferral() as defer:
           sys.setprofile(f)
           defer(sys.setprofile, None)
           # do something.

    """
    deferred = []
    defer = lambda f, *a, **k: deferred.append((f, a, k))
    try:
        yield defer
    finally:
        while deferred:
            f, a, k = deferred.pop()
            f(*a, **k) 
Example #5
Source File: base_cdm_dbg.py    From codimension with GNU General Public License v3.0 6 votes vote down vote up
def set_trace(self, frame=None):
        """Starts debugging from 'frame'"""
        if frame is None:
            frame = sys._getframe().f_back  # Skip set_trace method

        if sys.version_info[0] == 2:
            stopOnHandleLine = self._dbgClient.handleLine.func_code
        else:
            stopOnHandleLine = self._dbgClient.handleLine.__code__

        frame.f_trace = self.trace_dispatch
        while frame.f_back is not None:
            # stop at erics debugger frame or a threading bootstrap
            if frame.f_back.f_code == stopOnHandleLine:
                frame.f_trace = self.trace_dispatch
                break

            frame = frame.f_back

        self.stop_everywhere = True
        sys.settrace(self.trace_dispatch)
        sys.setprofile(self._dbgClient.callTraceEnabled) 
Example #6
Source File: test_sampling.py    From profiling with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_tracing_sampler_does_not_sample_too_often():
    pytest.importorskip('yappi')
    # pytest-cov cannot detect a callback function registered by
    # :func:`sys.setprofile`.
    class fake_profiler(object):
        samples = []
        @classmethod
        def sample(cls, frame):
            cls.samples.append(frame)
        @classmethod
        def count_and_clear_samples(cls):
            count = len(cls.samples)
            del cls.samples[:]
            return count
    sampler = TracingSampler(0.1)
    sampler._profile(fake_profiler, None, None, None)
    assert fake_profiler.count_and_clear_samples() == 1
    sampler._profile(fake_profiler, None, None, None)
    assert fake_profiler.count_and_clear_samples() == 0
    spin(0.5)
    sampler._profile(fake_profiler, None, None, None)
    assert fake_profiler.count_and_clear_samples() == 1 
Example #7
Source File: threading.py    From lightbulb-framework with MIT License 5 votes vote down vote up
def setprofile(func):
    global _profile_hook
    _profile_hook = func 
Example #8
Source File: Util.py    From variety with GNU General Public License v3.0 5 votes vote down vote up
def start(self):
        """
        Starts the module profiler for all future threads.
        """
        threading.setprofile(self.profiler) 
Example #9
Source File: Util.py    From variety with GNU General Public License v3.0 5 votes vote down vote up
def stop(self):
        """
        Removes the module profiler globally and from future threads.
        """
        if sys.getprofile() != self.profiler:
            logger.warning(
                "ModuleProfiler: The currently enabled profile function was not ours - unbinding anyways"
            )
        threading.setprofile(None)
        sys.setprofile(None) 
Example #10
Source File: collect_types.py    From pyannotate with Apache License 2.0 5 votes vote down vote up
def stop_types_collection():
    # type: () -> None
    """
    Remove profiler hooks.
    """
    sys.setprofile(None)
    threading.setprofile(None)  # type: ignore 
Example #11
Source File: test_sys_setprofile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def callback(self, frame, event, arg):
        # Callback registered with sys.setprofile()/sys.settrace()
        self.dispatch[event](self, frame) 
Example #12
Source File: test_sys_setprofile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example #13
Source File: test_sys_setprofile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        sys.setprofile(None) 
Example #14
Source File: test_sys_setprofile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        sys.setprofile(None) 
Example #15
Source File: test_sys_setprofile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def capture_events(callable, p=None):
    if p is None:
        p = HookWatcher()
    # Disable the garbage collector. This prevents __del__s from showing up in
    # traces.
    old_gc = gc.isenabled()
    gc.disable()
    try:
        sys.setprofile(p.callback)
        protect(callable, p)
        sys.setprofile(None)
    finally:
        if old_gc:
            gc.enable()
    return p.get_events()[1:-1] 
Example #16
Source File: profile.py    From Imogen with MIT License 5 votes vote down vote up
def runctx(self, cmd, globals, locals):
        self.set_cmd(cmd)
        sys.setprofile(self.dispatcher)
        try:
            exec(cmd, globals, locals)
        finally:
            sys.setprofile(None)
        return self

    # This method is more useful to profile a single function call. 
Example #17
Source File: mprofile.py    From pyFileFixity with MIT License 5 votes vote down vote up
def run(self, cmd):
        sys.setprofile(self.profile)
        try:
            exec(cmd)
        finally:
            sys.setprofile(None)
        return self 
Example #18
Source File: __init__.py    From pyglet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _install_trace():
    global _trace_thread_count
    sys.setprofile(_thread_trace_func(_trace_thread_count))
    _trace_thread_count += 1 
Example #19
Source File: profiler.py    From pyFileFixity with MIT License 5 votes vote down vote up
def stop(self):
        if self.use_signal:
            signal.setitimer(signal.ITIMER_REAL, 0.0, 0.0)

            try:
                signal.signal(signal.SIGALRM, signal.SIG_IGN)
            except ValueError:
                raise NotMainThreadError()
        else:
            sys.setprofile(None) 
Example #20
Source File: threading.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def setprofile(func):
    """Set a profile function for all threads started from the threading module.

    The func will be passed to sys.setprofile() for each thread, before its
    run() method is called.

    """
    global _profile_hook
    _profile_hook = func 
Example #21
Source File: test_sys.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_getdefaultencoding(self):
        self.assertRaises(TypeError, sys.getdefaultencoding, 42)
        # can't check more than the type, as the user might have changed it
        self.assertIsInstance(sys.getdefaultencoding(), str)

    # testing sys.settrace() is done in test_sys_settrace.py
    # testing sys.setprofile() is done in test_sys_setprofile.py 
Example #22
Source File: test_sys_setprofile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def capture_events(callable, p=None):
    if p is None:
        p = HookWatcher()
    # Disable the garbage collector. This prevents __del__s from showing up in
    # traces.
    old_gc = gc.isenabled()
    gc.disable()
    try:
        sys.setprofile(p.callback)
        protect(callable, p)
        sys.setprofile(None)
    finally:
        if old_gc:
            gc.enable()
    return p.get_events()[1:-1] 
Example #23
Source File: test_sys_setprofile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example #24
Source File: test_sys_setprofile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        sys.setprofile(None) 
Example #25
Source File: test_sys_setprofile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        sys.setprofile(None) 
Example #26
Source File: profile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def runctx(self, cmd, globals, locals):
        self.set_cmd(cmd)
        sys.setprofile(self.dispatcher)
        try:
            exec(cmd, globals, locals)
        finally:
            sys.setprofile(None)
        return self

    # This method is more useful to profile a single function call. 
Example #27
Source File: __init__.py    From flappy-bird-py with GNU General Public License v2.0 5 votes vote down vote up
def _install_trace():
    sys.setprofile(_trace_func) 
Example #28
Source File: __init__.py    From flappy-bird-py with GNU General Public License v2.0 5 votes vote down vote up
def _install_trace():
    sys.setprofile(_trace_func) 
Example #29
Source File: __init__.py    From flappy-bird-py with GNU General Public License v2.0 5 votes vote down vote up
def _install_trace():
    sys.setprofile(_trace_func) 
Example #30
Source File: threading.py    From pmatic with GNU General Public License v2.0 5 votes vote down vote up
def setprofile(func):
    global _profile_hook
    _profile_hook = func