Python threading.settrace() Examples

The following are 30 code examples of threading.settrace(). 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 threading , or try the search function .
Example #1
Source File: test_threading.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_finalize_with_trace(self):
        # Issue1733757
        # Avoid a deadlock when sys.settrace steps into threading._shutdown
        assert_python_ok("-c", """if 1:
            import sys, threading

            # A deadlock-killer, to prevent the
            # testsuite to hang forever
            def killer():
                import os, time
                time.sleep(2)
                print('program blocked; aborting')
                os._exit(2)
            t = threading.Thread(target=killer)
            t.daemon = True
            t.start()

            # This is the trace function
            def func(frame, event, arg):
                threading.current_thread()
                return func

            sys.settrace(func)
            """) 
Example #2
Source File: test_threading.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_finalize_with_trace(self):
        # Issue1733757
        # Avoid a deadlock when sys.settrace steps into threading._shutdown
        assert_python_ok("-c", """if 1:
            import sys, threading

            # A deadlock-killer, to prevent the
            # testsuite to hang forever
            def killer():
                import os, time
                time.sleep(2)
                print('program blocked; aborting')
                os._exit(2)
            t = threading.Thread(target=killer)
            t.daemon = True
            t.start()

            # This is the trace function
            def func(frame, event, arg):
                threading.current_thread()
                return func

            sys.settrace(func)
            """) 
Example #3
Source File: tracer.py    From python-hunter with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __call__(self, frame, kind, arg):
        """
        The settrace function.

        .. note::

            This always returns self (drills down) - as opposed to only drilling down when ``predicate(event)`` is True
            because it might match further inside.
        """
        if self._handler is not None:
            if kind == 'return' and self.depth > 0:
                self.depth -= 1
            try:
                self._handler(Event(frame, kind, arg, self))
            except Exception as exc:
                traceback.print_exc(file=hunter._default_stream)
                hunter._default_stream.write('Disabling tracer because handler {} failed ({!r}).\n\n'.format(
                    self._handler, exc))
                self.stop()
                return
            if kind == 'call':
                self.depth += 1
                self.calls += 1

            return self 
Example #4
Source File: tracer.py    From python-hunter with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def trace(self, predicate):
        """
        Starts tracing with the given callable.

        Args:
            predicate (callable that accepts a single :obj:`~hunter.event.Event` argument):
        Return:
            self
        """
        self._handler = predicate
        if self.threading_support is None or self.threading_support:
            self._threading_previous = getattr(threading, '_trace_hook', None)
            threading.settrace(self)
        self._previous = sys.gettrace()
        sys.settrace(self)
        return self 
Example #5
Source File: pydevd.py    From filmkodi with Apache License 2.0 6 votes vote down vote up
def stoptrace():
    global connected
    if connected:
        pydevd_tracing.restore_sys_set_trace_func()
        sys.settrace(None)
        try:
            #not available in jython!
            threading.settrace(None) # for all future threads
        except:
            pass

        from _pydev_bundle.pydev_monkey import undo_patch_thread_modules
        undo_patch_thread_modules()

        debugger = get_global_debugger()

        if debugger:

            debugger.set_trace_for_frame_and_parents(
                    get_frame(), also_add_to_passed_frame=True, overwrite_prev_trace=True, dispatch_func=lambda *args:None)
            debugger.exiting()

            kill_all_pydev_threads()

        connected = False 
Example #6
Source File: test_threading.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_finalize_with_trace(self):
        # Issue1733757
        # Avoid a deadlock when sys.settrace steps into threading._shutdown
        assert_python_ok("-c", """if 1:
            import sys, threading

            # A deadlock-killer, to prevent the
            # testsuite to hang forever
            def killer():
                import os, time
                time.sleep(2)
                print('program blocked; aborting')
                os._exit(2)
            t = threading.Thread(target=killer)
            t.daemon = True
            t.start()

            # This is the trace function
            def func(frame, event, arg):
                threading.current_thread()
                return func

            sys.settrace(func)
            """) 
Example #7
Source File: trace.py    From odoo13-x64 with GNU General Public License v3.0 6 votes vote down vote up
def runfunc(*args, **kw):
        if len(args) >= 2:
            self, func, *args = args
        elif not args:
            raise TypeError("descriptor 'runfunc' of 'Trace' object "
                            "needs an argument")
        elif 'func' in kw:
            func = kw.pop('func')
            self, *args = args
        else:
            raise TypeError('runfunc expected at least 1 positional argument, '
                            'got %d' % (len(args)-1))

        result = None
        if not self.donothing:
            sys.settrace(self.globaltrace)
        try:
            result = func(*args, **kw)
        finally:
            if not self.donothing:
                sys.settrace(None)
        return result 
Example #8
Source File: trace.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example #9
Source File: trace.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example #10
Source File: trace.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def runfunc(self, func, *args, **kw):
        result = None
        if not self.donothing:
            sys.settrace(self.globaltrace)
        try:
            result = func(*args, **kw)
        finally:
            if not self.donothing:
                sys.settrace(None)
        return result 
Example #11
Source File: app.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def opt_spew(self):
        """Print an insanely verbose log of everything that happens.
        Useful when debugging freezes or locks in complex code."""
        sys.settrace(util.spewer)
        try:
            import threading
        except ImportError:
            return
        threading.settrace(util.spewer) 
Example #12
Source File: trace.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example #13
Source File: pydevd.py    From filmkodi with Apache License 2.0 5 votes vote down vote up
def has_data_to_redirect():
    if getattr(sys, 'stdoutBuf', None):
        if not sys.stdoutBuf.empty():
            return True
    if getattr(sys, 'stderrBuf', None):
        if not sys.stderrBuf.empty():
            return True

    return False

#=======================================================================================================================
# settrace
#======================================================================================================================= 
Example #14
Source File: pydevd.py    From filmkodi with Apache License 2.0 5 votes vote down vote up
def settrace_forked():
    '''
    When creating a fork from a process in the debugger, we need to reset the whole debugger environment!
    '''
    host, port = dispatch()

    from _pydevd_bundle import pydevd_tracing
    pydevd_tracing.restore_sys_set_trace_func()

    if port is not None:
        global connected
        connected = False

        custom_frames_container_init()

        settrace(
                host,
                port=port,
                suspend=False,
                trace_only_current_thread=False,
                overwrite_prev_trace=True,
                patch_multiprocessing=True,
        )

#=======================================================================================================================
# SetupHolder
#======================================================================================================================= 
Example #15
Source File: trace.py    From datafari with Apache License 2.0 5 votes vote down vote up
def runfunc(self, func, *args, **kw):
        result = None
        if not self.donothing:
            sys.settrace(self.globaltrace)
        try:
            result = func(*args, **kw)
        finally:
            if not self.donothing:
                sys.settrace(None)
        return result 
Example #16
Source File: trace.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example #17
Source File: trace.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example #18
Source File: trace.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example #19
Source File: pydevd.py    From filmkodi with Apache License 2.0 5 votes vote down vote up
def patch_threads(self):
        try:
            # not available in jython!
            import threading
            threading.settrace(self.trace_dispatch)  # for all future threads
        except:
            pass

        from _pydev_bundle.pydev_monkey import patch_thread_modules
        patch_thread_modules() 
Example #20
Source File: trace.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example #21
Source File: trace.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example #22
Source File: trace.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example #23
Source File: trace.py    From datafari with Apache License 2.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example #24
Source File: trace.py    From datafari with Apache License 2.0 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example #25
Source File: trace.py    From datafari with Apache License 2.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example #26
Source File: test_threading.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_frame_tstate_tracing(self):
        # Issue #14432: Crash when a generator is created in a C thread that is
        # destroyed while the generator is still used. The issue was that a
        # generator contains a frame, and the frame kept a reference to the
        # Python state of the destroyed C thread. The crash occurs when a trace
        # function is setup.

        def noop_trace(frame, event, arg):
            # no operation
            return noop_trace

        def generator():
            while 1:
                yield "genereator"

        def callback():
            if callback.gen is None:
                callback.gen = generator()
            return next(callback.gen)
        callback.gen = None

        old_trace = sys.gettrace()
        sys.settrace(noop_trace)
        try:
            # Install a trace function
            threading.settrace(noop_trace)

            # Create a generator in a C thread which exits after the call
            _testcapi.call_in_temporary_c_thread(callback)

            # Call the generator in a different Python thread, check that the
            # generator didn't keep a reference to the destroyed thread state
            for test in range(3):
                # The trace function is still called here
                callback()
        finally:
            sys.settrace(old_trace) 
Example #27
Source File: test_threading.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_finalize_with_trace(self):
        # Issue1733757
        # Avoid a deadlock when sys.settrace steps into threading._shutdown
        p = subprocess.Popen([sys.executable, "-c", """if 1:
            import sys, threading

            # A deadlock-killer, to prevent the
            # testsuite to hang forever
            def killer():
                import os, time
                time.sleep(2)
                print 'program blocked; aborting'
                os._exit(2)
            t = threading.Thread(target=killer)
            t.daemon = True
            t.start()

            # This is the trace function
            def func(frame, event, arg):
                threading.current_thread()
                return func

            sys.settrace(func)
            """],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        self.addCleanup(p.stdout.close)
        self.addCleanup(p.stderr.close)
        stdout, stderr = p.communicate()
        rc = p.returncode
        self.assertFalse(rc == 2, "interpreted was blocked")
        self.assertTrue(rc == 0,
                        "Unexpected error: " + repr(stderr)) 
Example #28
Source File: tracer.py    From python-hunter with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def stop(self):
        """
        Stop tracing. Reinstalls the :attr:`~hunter.tracer.Tracer.previous` tracer.
        """
        if self._handler is not None:
            sys.settrace(self._previous)
            self._handler = self._previous = None
            if self.threading_support is None or self.threading_support:
                threading.settrace(self._threading_previous)
                self._threading_previous = None 
Example #29
Source File: trace.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def runfunc(self, func, *args, **kw):
        result = None
        if not self.donothing:
            sys.settrace(self.globaltrace)
        try:
            result = func(*args, **kw)
        finally:
            if not self.donothing:
                sys.settrace(None)
        return result 
Example #30
Source File: trace.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None)