Python sys.getprofile() Examples

The following are code examples for showing how to use sys.getprofile(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: pytypes   Author: Stewori   File: type_util.py    (license) View Source Project 6 votes vote down vote up
def restore_profiler():
    """If a typechecking profiler is active, e.g. created by
    pytypes.set_global_typechecked_profiler(), such a profiler
    must be restored whenever a TypeCheckError is caught.
    The call must stem from the thread that raised the error.
    Otherwise the typechecking profiler is implicitly disabled.
    Alternatively one can turn pytypes into warning mode. In that
    mode no calls to this function are required (unless one uses
    filterwarnings("error") or likewise).
    """
    idn = threading.current_thread().ident
    if not sys.getprofile() is None:
        warn("restore_profiler: Current profile is not None!")
    if not idn in _saved_profilers:
        warn("restore_profiler: No saved profiler for calling thread!")
    else:
        sys.setprofile(_saved_profilers[idn])
        del _saved_profilers[idn] 
Example 2
Project: pytypes   Author: Stewori   File: type_util.py    (license) View Source Project 6 votes vote down vote up
def start(self):
        if self._active:
            raise RuntimeError('type checker already running')
        elif self._pending:
            raise RuntimeError('type checker already starting up')
        self._pending = True
        # Install this instance as the current profiler
        self._previous_profiler = sys.getprofile()
        self._set_caller_level_shift(0)
        sys.setprofile(self)

        # If requested, set this instance as the default profiler for all future threads
        # (does not affect existing threads)
        if self.all_threads:
            self._previous_thread_profiler = threading._profile_hook
            threading.setprofile(self)
        self._active, self._pending = True, False 
Example 3
Project: pytypes   Author: Stewori   File: type_util.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        if self._active and not self._pending:
            self._pending = True
            if sys.getprofile() is self:
                sys.setprofile(self._previous_profiler)
                if not self._previous_profiler is None and \
                        isinstance(self._previous_profiler, TypeAgent):
                    self._previous_profiler._set_caller_level_shift(0)
            else:
                if not (sys.getprofile() is None and self._cleared):
                    warn('the system profiling hook has changed unexpectedly')
            if self.all_threads:
                if threading._profile_hook is self:
                    threading.setprofile(self._previous_thread_profiler)
                else:  # pragma: no cover
                    warn('the threading profiling hook has changed unexpectedly')
            self._active, self._pending = False, False 
Example 4
Project: artemis   Author: QUVA-Lab   File: test_local_capture.py    (license) View Source Project 6 votes vote down vote up
def test_execute_and_capture_locals():

    def func():
        a=3
        return a+4

    def outer_func():
        b=4
        def nested():
            return func()+b+5
        return nested

    out, local_vars = execute_and_capture_locals(func)
    assert out == 3+4
    assert local_vars == {'a': 3}
    assert sys.getprofile() is None

    out, local_vars = execute_and_capture_locals(outer_func())
    assert out == 7+4+5
    assert local_vars == {'b': 4, 'func': func}
    assert sys.getprofile() is None 
Example 5
Project: OpenMDAO   Author: OpenMDAO   File: iprofile.py    (license) View Source Project 6 votes vote down vote up
def start():
    """
    Turn on profiling.
    """
    global _profile_start, _profile_setup, _call_stack, _inst_data
    if _profile_start is not None:
        print("profiling is already active.")
        return

    if not _profile_setup:
        setup()  # just do a default setup

    _profile_start = etime()
    _call_stack.append(('$total', _profile_start, None))
    if '$total' not in _inst_data:
        _inst_data['$total'] = [None, 0., 0]

    if sys.getprofile() is not None:
        raise RuntimeError("another profile function is already active.")
    sys.setprofile(_instance_profile_callback) 
Example 6
Project: MonkeyType   Author: Instagram   File: tracing.py    (license) View Source Project 5 votes vote down vote up
def trace_calls(
    logger: CallTraceLogger,
    code_filter: Optional[CodeFilter] = None,
    sample_rate: Optional[int] = None,
) -> Iterator[None]:
    """Enable call tracing for a block of code"""
    old_trace = sys.getprofile()
    sys.setprofile(CallTracer(logger, code_filter, sample_rate))
    try:
        yield
    finally:
        sys.setprofile(old_trace)
        logger.flush() 
Example 7
Project: zippy   Author: securesystemslab   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 8
Project: zippy   Author: securesystemslab   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 9
Project: oil   Author: oilshell   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 10
Project: oil   Author: oilshell   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 11
Project: python2-tracer   Author: extremecoders-re   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 12
Project: python2-tracer   Author: extremecoders-re   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 13
Project: web_ctp   Author: molebot   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 14
Project: web_ctp   Author: molebot   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 15
Project: pefile.pypy   Author: cloudtracer   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 16
Project: pefile.pypy   Author: cloudtracer   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 17
Project: ouroboros   Author: pybee   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 18
Project: ouroboros   Author: pybee   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 19
Project: ndk-python   Author: gittor   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 20
Project: ndk-python   Author: gittor   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 21
Project: kbe_server   Author: xiaohaoppy   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_empty(self):
        self.assertIsNone(sys.getprofile()) 
Example 22
Project: kbe_server   Author: xiaohaoppy   File: test_sys_setprofile.py    (license) View Source Project 5 votes vote down vote up
def test_setget(self):
        def fn(*args):
            pass

        sys.setprofile(fn)
        self.assertIs(sys.getprofile(), fn) 
Example 23
Project: OpenMDAO   Author: OpenMDAO   File: itrace.py    (license) View Source Project 5 votes vote down vote up
def start():
    """
    Start call tracing.
    """
    global _trace_calls
    if sys.getprofile() is not None:
        raise RuntimeError("another profile function is already active.")
    if _trace_calls is None:
        raise RuntimeError("trace.setup() was not called before trace.start().")
    sys.setprofile(_trace_calls) 
Example 24
Project: OpenMDAO   Author: OpenMDAO   File: iprof_mem.py    (license) View Source Project 5 votes vote down vote up
def start():
    """
    Turn on memory profiling.
    """
    global _trace_memory
    if sys.getprofile() is not None:
        raise RuntimeError("another profile function is already active.")
    if _trace_memory is None:
        raise RuntimeError("trace.setup() was not called before trace.start().")
    sys.setprofile(_trace_memory)