Python threading.settrace() Examples

The following are 30 code examples for showing how to use threading.settrace(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module threading , or try the search function .

Example 1
Project: Fluid-Designer   Author: Microvellum   File: test_threading.py    License: 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
Project: ironpython3   Author: IronLanguages   File: test_threading.py    License: 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
Project: python-hunter   Author: ionelmc   File: tracer.py    License: 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
Project: python-hunter   Author: ionelmc   File: tracer.py    License: 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
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 6
Project: odoo13-x64   Author: guohuadeng   File: trace.py    License: 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 7
Project: filmkodi   Author: mrknow   File: pydevd.py    License: 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 8
Project: meddle   Author: glmcdona   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example 9
Project: meddle   Author: glmcdona   File: trace.py    License: MIT License 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example 10
Project: meddle   Author: glmcdona   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example 11
Project: meddle   Author: glmcdona   File: trace.py    License: MIT License 5 votes vote down vote up
def run(self, cmd):
        import __main__
        dict = __main__.__dict__
        if not self.donothing:
            threading.settrace(self.globaltrace)
            sys.settrace(self.globaltrace)
        try:
            exec cmd in dict, dict
        finally:
            if not self.donothing:
                sys.settrace(None)
                threading.settrace(None) 
Example 12
Project: meddle   Author: glmcdona   File: trace.py    License: MIT License 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 13
Project: ironpython2   Author: IronLanguages   File: trace.py    License: Apache License 2.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example 14
Project: ironpython2   Author: IronLanguages   File: trace.py    License: Apache License 2.0 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example 15
Project: ironpython2   Author: IronLanguages   File: trace.py    License: Apache License 2.0 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example 16
Project: ironpython2   Author: IronLanguages   File: trace.py    License: 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 17
Project: ironpython2   Author: IronLanguages   File: test_threading.py    License: Apache License 2.0 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 18
Project: Comparative-Annotation-Toolkit   Author: ComparativeGenomicsToolkit   File: trace.py    License: Apache License 2.0 5 votes vote down vote up
def enable(self):
        """enable logging on all threads."""
        assert(self.fh is not None)
        sys.settrace(self.__callback)
        threading.settrace(self.__callback) 
Example 19
Project: Comparative-Annotation-Toolkit   Author: ComparativeGenomicsToolkit   File: trace.py    License: Apache License 2.0 5 votes vote down vote up
def disable(self):
        """disable logging on all threads."""
        sys.settrace(None)
        threading.settrace(None) 
Example 20
Project: BinderFilter   Author: dxwu   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example 21
Project: BinderFilter   Author: dxwu   File: trace.py    License: MIT License 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example 22
Project: BinderFilter   Author: dxwu   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example 23
Project: BinderFilter   Author: dxwu   File: trace.py    License: MIT License 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 24
Project: Computable   Author: ktraunmueller   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example 25
Project: Computable   Author: ktraunmueller   File: trace.py    License: MIT License 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func) 
Example 26
Project: Computable   Author: ktraunmueller   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None)
        threading.settrace(None) 
Example 27
Project: Computable   Author: ktraunmueller   File: trace.py    License: MIT License 5 votes vote down vote up
def run(self, cmd):
        import __main__
        dict = __main__.__dict__
        if not self.donothing:
            threading.settrace(self.globaltrace)
            sys.settrace(self.globaltrace)
        try:
            exec cmd in dict, dict
        finally:
            if not self.donothing:
                sys.settrace(None)
                threading.settrace(None) 
Example 28
Project: Computable   Author: ktraunmueller   File: trace.py    License: MIT License 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 29
Project: oss-ftp   Author: aliyun   File: trace.py    License: MIT License 5 votes vote down vote up
def _unsettrace():
        sys.settrace(None) 
Example 30
Project: oss-ftp   Author: aliyun   File: trace.py    License: MIT License 5 votes vote down vote up
def _settrace(func):
        threading.settrace(func)
        sys.settrace(func)