h2o-py/h2o/utils/debugging.py
def _find_function_from_code(frame, code):
    """
    Given a frame and a compiled function code, find the corresponding function object within the frame.

    This function addresses the following problem: when handling a stacktrace, we receive information about
    which piece of code was being executed in the form of a CodeType object. That objects contains function name,
    file name, line number, and the compiled bytecode. What it *doesn't* contain is the function object itself.

    So this utility function aims at locating this function object, and it does so by searching through objects
    in the preceding local frame (i.e. the frame where the function was called from). We expect that the function
    should usually exist there -- either by itself, or as a method on one of the objects.

    :param types.FrameType frame: local frame where the function ought to be found somewhere.
    :param types.CodeType code: the compiled code of the function to look for.

    :returns: the function object, or None if not found.
    """
    def find_code(iterable, depth=0):
        if depth > 3: return  # Avoid potential infinite loops, or generally objects that are too deep.
        for item in iterable:
            if item is None: continue
            found = None
            if hasattr(item, "__code__") and item.__code__ == code:
                found = item
            elif isinstance(item, type) or isinstance(item, ModuleType):  # class / module
                try:
                    found = find_code((getattr(item, n, None) for n in dir(item)), depth + 1)
                except Exception:
                    # Sometimes merely getting module's attributes may cause an exception. For example :mod:`six.moves`
                    # is such an offender...
                    continue
            elif isinstance(item, (list, tuple, set)):
                found = find_code(item, depth + 1)
            elif isinstance(item, dict):
                found = find_code(item.values(), depth + 1)
            if found: return found
    return find_code(frame.f_locals.values()) or find_code(frame.f_globals.values())
def _find_function_from_code(frame, code):
    def find_code(iterable, depth=0):
        if depth > 3: return  # Avoid potential infinite loops, or generally objects that are too deep.
        for item in iterable:
            if item is None: continue
            found = None
            if hasattr(item, "__code__") and item.__code__ == code:
                found = item
            elif isinstance(item, type) or isinstance(item, ModuleType):  # class / module
                try:
                    found = find_code((getattr(item, n, None) for n in dir(item)), depth + 1)
                except Exception:
                    # Sometimes merely getting module's attributes may cause an exception. For example :mod:`six.moves`
                    # is such an offender...
                    continue
            elif isinstance(item, (list, tuple, set)):
                found = find_code(item, depth + 1)
            elif isinstance(item, dict):
                found = find_code(item.values(), depth + 1)
            if found: return found
    return find_code(frame.f_locals.values()) or find_code(frame.f_globals.values())
def _find_function_from_code ( frame , code ) : def find_code ( iterable , depth = 0 ) : if depth > 3 : return # Avoid potential infinite loops, or generally objects that are too deep. for item in iterable : if item is None : continue found = None if hasattr ( item , "__code__" ) and item . __code__ == code : found = item elif isinstance ( item , type ) or isinstance ( item , ModuleType ) : # class / module try : found = find_code ( ( getattr ( item , n , None ) for n in dir ( item ) ) , depth + 1 ) except Exception : # Sometimes merely getting module's attributes may cause an exception. For example :mod:`six.moves` # is such an offender... continue elif isinstance ( item , ( list , tuple , set ) ) : found = find_code ( item , depth + 1 ) elif isinstance ( item , dict ) : found = find_code ( item . values ( ) , depth + 1 ) if found : return found return find_code ( frame . f_locals . values ( ) ) or find_code ( frame . f_globals . values ( ) )
Given a frame and a compiled function code, find the corresponding function object within the frame.

    This function addresses the following problem: when handling a stacktrace, we receive information about
    which piece of code was being executed in the form of a CodeType object. That objects contains function name,
    file name, line number, and the compiled bytecode. What it *doesn't* contain is the function object itself.

    So this utility function aims at locating this function object, and it does so by searching through objects
    in the preceding local frame (i.e. the frame where the function was called from). We expect that the function
    should usually exist there -- either by itself, or as a method on one of the objects.

    :param types.FrameType frame: local frame where the function ought to be found somewhere.
    :param types.CodeType code: the compiled code of the function to look for.

    :returns: the function object, or None if not found.
Given a frame and a compiled function code find the corresponding function object within the frame .