Code Monkey home page Code Monkey logo

pydebug's People

Contributors

bobfrank avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pydebug's Issues

Feature request: Consider making ServerHandler.__setup_thread_tracer a public interface

Given your own StackOverflow question (and answer), it might be cool to provide a "generic" version of your solution that others could use. For example, I could see doing something like this (untested):

# …
import platform
import warn
# …
_EMPTY_OBJ = ctypes.py_object()
# …

def install_thread_tracer(trace_function, arg=None, skip_thread_ids=()):
    trace_f = Py_tracefunc(trace_function)
    trace_f_arg = arg
    _set_thread_tracer(trace_f, trace_f_arg, skip_thread_ids)

def uninstall_thread_tracer(skip_thread_ids=()):
    trace_f = ctypes.cast(None, Py_tracefunc)
    trace_f_arg = _EMPTY_OBJ
    _set_thread_tracer(trace_f, trace_f_arg, skip_thread_ids)

def _set_thread_tracer_cpython(trace_f, trace_f_arg, skip_thread_ids):
    interp = ctypes.pythonapi.PyInterpreterState_Head()
    t = ctypes.pythonapi.PyInterpreterState_ThreadHead(interp)
    while t != 0:
        t_p = ctypes.cast(t, ctypes.POINTER(PyThreadState))
        if t_p[0].thread_id != _thread.get_ident() and t_p[0].thread_id not \
                in skip_thread_ids:
            try:
                temp = t_p[0].c_traceobj
            except ValueError:
                temp = None
            if trace_f_arg != _EMPTY_OBJ:  # Py_XINCREF
                # ctypes.pythonapi._Total
                refcount = ctypes.c_long.from_address(id(trace_f_arg))
                refcount.value += 1
            t_p[0].c_tracefunc = ctypes.cast(None, Py_tracefunc)
            t_p[0].c_traceobj = _EMPTY_OBJ
            t_p[0].use_tracing = int(t_p[0].c_profilefunc is not None)
            if temp is not None:  # Py_XDECREF
                refcount = ctypes.c_long.from_address(id(temp))
                # Don't need to dealloc since we have a ref in here and it'll
                # always be >0
                refcount.value -= 1
            t_p[0].c_tracefunc = trace_f
            t_p[0].c_traceobj = trace_f_arg
            t_p[0].use_tracing = int(trace_f is not None or
                t_p[0].c_profilefunc is not None)
        t = ctypes.pythonapi.PyThreadState_Next(t)

_THREAD_TRACER_SETTERS = {
    'CPython': _set_thread_tracer_cpython,
    # Future implementations, maybe?
}

def _set_thread_tracer_noop(*arg, **kw):  # pylint: disable=unused-arg
    pass

def _set_thread_tracer(trace_f, trace_f_arg, skip_thread_ids):
    _thread_tracer_setter = _THREAD_TRACER_SETTERS.get(
        platform.python_implementation(), _set_thread_tracer_noop)
    if _thread_tracer_setter is _set_thread_tracer_noop:
        warnings.warn('no thread tracer found for {}'.format(
            platform.python_implementation()), RuntimeWarning)
    _thread_tracer_setter(trace_f, trace_f_arg, skip_thread_ids)
    # Skip lookup next time
    global _set_thread_tracer
    _set_thread_tracer = _thread_tracer_setter

class ServerHandler(object):
    # …
    def __setup_thread_tracer(self, setting):
        global debug__has_loaded_thread
        if setting:
            install_thread_tracer(breakpoint_tracer, self,
                skip_threads=(debug__has_loaded_thread,))
        else:
            uninstall_thread_tracer(skip_threads=(debug__has_loaded_thread,))
    # …

I'd be happy to submit a PR if you think the idea has merit.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.