Exemple #1
0
def findsource(object):
    # Return the entire source file and starting line number for an object.

    file = getsourcefile(object) or getfile(object)
    globals_dict = None
    if inspect.isframe(object):
        globals_dict = object.f_globals
    else:
        module = getmodule(object, file)
        if module:
            globals_dict = module.__dict__
    lines = linecache.getlines(file, globals_dict)
    if not lines:
        raise IOError('could not get source code')

    if ismodule(object):
        return lines, 0

    if isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                if lines[i][0] == 'c':
                    return lines, i
                candidates.append((match.group(1), i))
        if candidates:
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if ismethod(object):
        object = object.im_func
    if isfunction(object):
        object = object.func_code
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        pmatch = pat.match
        lnum = min(object.co_firstlineno,len(lines))-1
        while lnum > 0:
            if pmatch(lines[lnum]): break
            lnum -= 1
 
        return lines, lnum
    raise IOError('could not find code object')
Exemple #2
0
def magic_name(value, *, path_prefix):
    """Fetch the name of the variable with the given value passed to the calling function.

    Parameters
    ----------
    value : any
        The value of the desired variable.
    path_prefix : absolute path-like, kwonly
        The path prefixes to ignore.

    Returns
    -------
    name : str or None
        Name of the variable, if found.
    """
    frame = inspect.currentframe()
    try:
        # See issue #1635 regarding potential AttributeError
        # since frame could be None.
        # https://github.com/napari/napari/pull/1635
        if inspect.isframe(frame):
            frame = frame.f_back

        # Iterate frames while filename starts with path_prefix (part of Napari)
        # or is autogenerated such as for the add_* for layers (#1694 / #1709)
        while (inspect.isframe(frame) and inspect.iscode(frame.f_code)
               and (frame.f_code.co_filename.startswith(path_prefix)
                    or frame.f_code.co_filename == "<string>")):
            frame = frame.f_back

        if inspect.isframe(frame) and inspect.iscode(frame.f_code):
            varmap = ChainMap(frame.f_locals, frame.f_globals)
            names = *frame.f_code.co_varnames, *frame.f_code.co_names

            for name in names:
                if (name.isidentifier() and name in varmap
                        and varmap[name] is value):
                    return name
        return None
    finally:
        # We need to delete the frame explicitly according to the inspect
        # documentation for deterministic removal of the frame.
        # Otherwise, proper deletion is dependent on a cycle detector and
        # automatic garbage collection.
        # See handle_stackframe_without_leak example at the following URLs:
        # https://docs.python.org/3/library/inspect.html#the-interpreter-stack
        # https://bugs.python.org/issue543148
        del frame
Exemple #3
0
 def _find_lineno(self, obj, source_lines):
     lineno = None
     if inspect.ismodule(obj):
         lineno = 0
     if inspect.isclass(obj):
         if source_lines is None:
             return
         pat = re.compile('^\\s*class\\s*%s\\b' % getattr(obj, '__name__', '-'))
         for (i, line) in enumerate(source_lines):
             while pat.match(line):
                 lineno = i
                 break
     if inspect.ismethod(obj):
         obj = obj.__func__
     if inspect.isfunction(obj):
         obj = obj.__code__
     if inspect.istraceback(obj):
         obj = obj.tb_frame
     if inspect.isframe(obj):
         obj = obj.f_code
     if inspect.iscode(obj):
         lineno = getattr(obj, 'co_firstlineno', None) - 1
     if lineno is not None:
         if source_lines is None:
             return lineno + 1
         pat = re.compile('(^|.*:)\\s*\\w*("|\')')
         for lineno in range(lineno, len(source_lines)):
             while pat.match(source_lines[lineno]):
                 return lineno
    def import_from(s1, module):
        syms = inspect.getmembers(module)
        str_syms = dir(module)
        name_as = ""
        if len(s1) == 4:
            name_as = s1[3][1]


        if not (s1[1][1] in str_syms):
            print("import error")
            exit()
        else:
            for sym in syms:
                if sym[0] == s1[1][1]:
                    if inspect.isfunction(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Class(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                    else:
                        print(sym[0])
 def import_name(s1):
     if s1[0] in NON_TERMINAL:
         if s1[0] in NON_TERMINAL and s1[0] == 286:
             dot_name = ""
             module_name = ""
             for name in s1[1]:
                 if not isinstance(name, int):
                     module_name += name[1]
             if len(s1) == 2:
                 dot_name = module_name
             elif len(s1) == 4:
                 dot_name = s1[3][1]
             try:
                 module = importlib.import_module(module_name)
             except ImportError:
                 print("Import Error, No module named " + module_name)
                 exit()
             new_module = Module(module_name)
             new_module.SYMBOL_LIST = []
             syms = inspect.getmembers(module)
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     #new_module.SYMBOL_LIST.append(Function(dot_name+'.' + sym[0]))
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     new_module.SYMBOL_LIST.append(Class(sym[0], [], []))
                 else:
                     print(sym[0])
                 self.local_names.append(new_module)
         else:
             for j in range(1,len(s1)):
                 import_name(s1[j])
Exemple #6
0
def getArgValues(frame):
    """Provides information about arguments passed into a particular frame"""
    if not isframe(frame):
        raise TypeError('{0!r} is not a frame object'.format(frame))

    args, varargs, kwonlyargs, varkw = _getfullargs(frame.f_code)
    return ArgInfo(args + kwonlyargs, varargs, varkw, frame.f_locals)
 def print_variable_type(self, objType):
     default_vars=["__builtins__", "__doc__","__path__", "__cached__", "__file__", "__name__", "__package__", "__version__"]
     self.dprint("[ %s ]" %objType.__name__)
     self.indent();self.indent()
     for ModObj in self.Modules:
         for name in dir(ModObj):
             obj = getattr(ModObj, name)
             #print(name, ":", type(obj))
             if not (inspect.isclass(obj) or
                 inspect.isfunction(obj)  or
                 inspect.isroutine(obj)  or
                 inspect.isfunction(obj)  or
                 inspect.isgeneratorfunction(obj)  or
                 inspect.isgenerator(obj)  or
                 inspect.istraceback(obj)  or
                 inspect.isframe(obj)      or
                 inspect.iscode(obj)       or
                 inspect.isabstract(obj)   or
                 inspect.ismethoddescriptor(obj)  or
                 inspect.isdatadescriptor(obj)    or
                 inspect.isgetsetdescriptor(obj)  or
                 inspect.ismemberdescriptor(obj)  or
                 inspect.isbuiltin(obj)):
                 if name not in default_vars:
                     if type(obj) is  objType:
                         ObjName = ModObj.__name__ + '.' + name
                         self.dprint("%s" %ObjName)
     self.dedent();self.dedent()
def getsourcelines(obj):
    (lines, lineno) = inspect.findsource(obj)
    if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
        return (lines, 1)
    if inspect.ismodule(obj):
        return (lines, 1)
    return (inspect.getblock(lines[lineno:]), lineno + 1)
Exemple #9
0
def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved."""
    file = inspect.getsourcefile(object) or inspect.getfile(object)
    module = inspect.getmodule(object, file)
    if module:
        lines = linecache.getlines(file, module.__dict__)
    else:
        lines = linecache.getlines(file)
    if not lines:
        raise IOError('could not get source code')

    if inspect.ismodule(object):
        return lines, 0

    if inspect.isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if inspect.ismethod(object):
        object = object.__func__
    if inspect.isfunction(object):
        object = sys.get_func_code(object)
    if inspect.istraceback(object):
        object = object.tb_frame
    if inspect.isframe(object):
        object = object.f_code
    if inspect.iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        lnum = object.co_firstlineno - 1
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        while lnum > 0:
            if pat.match(lines[lnum]): break
            lnum = lnum - 1
        return lines, lnum
    raise IOError('could not find code object')
Exemple #10
0
def dump_references(log, instances, exclude=[]):
    import gc
    import inspect
    gc.collect()
    frame = inspect.currentframe()
    try:
        exclude.append(instances)
        exclude.append([frame])
        for instance in instances:
            referrers = [x for x in gc.get_referrers(instance) if (x not in exclude and len([y for y in exclude if x in y])==0)]
            log.info("referrers for %s: %s", instance, len(referrers))
            for i in range(len(referrers)):
                r = referrers[i]
                log.info("[%s] in %s", i, type(r))
                if inspect.isframe(r):
                    log.info("  frame info: %s", str(inspect.getframeinfo(r))[:1024])
                elif type(r)==list:
                    listref = gc.get_referrers(r)
                    log.info("  list: %s..  %s referrers: %s", str(r[:32])[:1024], len(listref), str(listref[:32])[:1024])
                elif type(r)==dict:
                    if len(r)>64:
                        log.info("  %s items: %s", len(r), str(r)[:1024])
                        continue
                    for k,v in r.items():
                        if k is instance:
                            log.info("  key with value=%s", v)
                        elif v is instance:
                            log.info("  for key=%s", k)
                else:
                    log.info("     %s : %s", type(r), r)
    finally:
        del frame
Exemple #11
0
def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved."""
    file = inspect.getsourcefile(object) or inspect.getfile(object)
    module = inspect.getmodule(object, file)
    if module:
        lines = linecache.getlines(file, module.__dict__)
    else:
        lines = linecache.getlines(file)
    if not lines:
        raise IOError('could not get source code')

    if inspect.ismodule(object):
        return lines, 0

    if inspect.isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if inspect.ismethod(object):
        object = object.im_func
    if isinstance(object, FunctionType):
        object = sys.get_func_code(object)
    if inspect.istraceback(object):
        object = object.tb_frame
    if inspect.isframe(object):
        object = object.f_code
    if inspect.iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        lnum = object.co_firstlineno - 1
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        while lnum > 0:
            if pat.match(lines[lnum]): break
            lnum = lnum - 1
        return lines, lnum
    raise IOError('could not find code object')
    def get_recursive(obj):
        if non_local["current_depth"] == max_depth:
            return []

        res, new_referrers, seen_referrers = [], [], []
        exclude.extend([id(res), id(new_referrers), id(seen_referrers)])

        for o in gc.get_referrers(obj):
            if inspect.isframe(o):  # TODO: exclude only my frames
                continue

            if id(o) in already_seen:
                seen_referrers.append(o)
            elif id(o) not in exclude:
                new_referrers.append(o)
                already_seen.append(id(o))

        # can't use "for" loop because we need iterator itself to exclude it from referrers
        iterator = iter(new_referrers)
        exclude.append(id(iterator))
        non_local["current_depth"] += 1
        try:
            while True:
                sub_obj = next(iterator)
                res.append((sub_obj, get_recursive(sub_obj)))
        except StopIteration:
            pass
        non_local["current_depth"] -= 1

        # save only ids for already seen objects
        for sub_obj in seen_referrers:
            res.append((id(sub_obj), []))
        return res
    def ascend(self, obj, depth=1):
        """Return a nested list containing referrers of the given object."""
        depth += 1
        parents = []

        # Gather all referrers in one step to minimize
        # cascading references due to repr() logic.
        refs = gc.get_referrers(obj)
        self.ignore.append(refs)
        if len(refs) > self.maxparents:
            return [('[%s referrers]' % len(refs), [])]

        try:
            ascendcode = self.ascend.__code__
        except AttributeError:
            ascendcode = self.ascend.im_func.func_code
        for parent in refs:
            if inspect.isframe(parent) and parent.f_code is ascendcode:
                continue
            if parent in self.ignore:
                continue
            if depth <= self.maxdepth:
                parents.append((parent, self.ascend(parent, depth)))
            else:
                parents.append((parent, []))

        return parents
Exemple #14
0
    def get_recursive(obj):
        if non_local["current_depth"] == max_depth:
            return []

        res, new_referrers, seen_referrers = [], [], []
        exclude.extend([id(res), id(new_referrers), id(seen_referrers)])

        for o in gc.get_referrers(obj):
            if inspect.isframe(o):  # TODO: exclude only my frames
                continue

            if id(o) in already_seen:
                seen_referrers.append(o)
            elif id(o) not in exclude:
                new_referrers.append(o)
                already_seen.append(id(o))

        # can't use "for" loop because we need iterator itself to exclude it from referrers
        iterator = iter(new_referrers)
        exclude.append(id(iterator))
        non_local["current_depth"] += 1
        try:
            while True:
                sub_obj = next(iterator)
                res.append((sub_obj, get_recursive(sub_obj)))
        except StopIteration:
            pass
        non_local["current_depth"] -= 1

        # save only ids for already seen objects
        for sub_obj in seen_referrers:
            res.append((id(sub_obj), []))
        return res
Exemple #15
0
def getframeinfo(frame, context=1):
    """Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line."""
    if istraceback(frame):
        lineno = frame.tb_lineno
        frame = frame.tb_frame
    else:
        lineno = frame.f_lineno
    if not isframe(frame):
        raise TypeError('{!r} is not a frame or traceback object'.format(frame))

    filename = getsourcefile(frame) or getfile(frame)
    if context > 0:
        start = lineno - 1 - context//2
        try:
            lines, _ = findsource(frame)
        except IOError:
            lines = index = None
        else:
            start = max(start, 1)
            start = max(0, min(start, len(lines) - context))
            lines = lines[start:start+context]
            index = lineno - 1 - start
    else:
        lines = index = None

    return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
 def print_variable_type(self, objType):
     default_vars = [
         "__builtins__", "__doc__", "__path__", "__cached__", "__file__",
         "__name__", "__package__", "__version__"
     ]
     self.dprint("[@ %s ]" % objType.__name__)
     self.indent()
     self.indent()
     for ModObj in self.Modules:
         for name in dir(ModObj):
             obj = getattr(ModObj, name)
             #print(name, ":", type(obj))
             if not (inspect.isclass(obj) or inspect.isfunction(obj)
                     or inspect.isroutine(obj) or inspect.isfunction(obj)
                     or inspect.isgeneratorfunction(obj)
                     or inspect.isgenerator(obj) or inspect.istraceback(obj)
                     or inspect.isframe(obj) or inspect.iscode(obj)
                     or inspect.isabstract(obj)
                     or inspect.ismethoddescriptor(obj)
                     or inspect.isdatadescriptor(obj)
                     or inspect.isgetsetdescriptor(obj)
                     or inspect.ismemberdescriptor(obj)
                     or inspect.isbuiltin(obj)):
                 if name not in default_vars:
                     if type(obj) is objType:
                         ObjName = ModObj.__name__ + '.' + name
                         self.dprint("%s" % ObjName)
     self.dedent()
     self.dedent()
Exemple #17
0
def source_findable(python_object):
    """Check if inspect.getfile has a chance to find the source."""
    return (inspect.ismodule(python_object) or inspect.isclass(python_object)
            or inspect.ismethod(python_object)
            or inspect.isfunction(python_object)
            or inspect.istraceback(python_object)
            or inspect.isframe(python_object) or inspect.iscode(python_object))
 def tostr(o):
     if isframe(o):
         return getframeinfo(o)
     try:
         return tostring(o)
     except:
         return repr(o)
Exemple #19
0
    def __init__(self, exc_type, exc_value, tb, context=None):
        if inspect.isframe(tb):
            self.lineno = tb.f_lineno
            tb_frame = tb
        else:
            self.lineno = tb.tb_lineno
            tb_frame = tb.tb_frame

        self.function_name = tb_frame.f_code.co_name
        self.locals = tb_frame.f_locals
        self.globals = tb_frame.f_globals
        self.context = context

        fn = inspect.getsourcefile(tb) or inspect.getfile(tb)
        if fn[-4:] in ('.pyo', '.pyc'):
            fn = fn[:-1]
            # if it's a file on the file system resolve the real filename.
        if os.path.isfile(fn):
            fn = os.path.realpath(fn)
        self.filename = fn
        self.module = self.globals.get('__name__')
        self.loader = self.globals.get('__loader__')
        self.code = tb_frame.f_code

        # support for paste's traceback extensions
        self.hide = self.locals.get('__traceback_hide__', False)
        info = self.locals.get('__traceback_info__')
        if info is not None:
            try:
                info = text_(info)
            except UnicodeError:
                info = str(info).decode('utf-8', 'replace')
        self.info = info
Exemple #20
0
def ignore_frames(x):
    import inspect
    import types

    l = []
    if inspect.isclass(x):
        # must be a class object
        l.extend([x.__mro__, x.__dict__])

        if hasattr(x, '__weakref__'):
            l.extend([x.__weakref__])

        for member in x.__dict__.values():
            # ignore attributes.
            if inspect.isgetsetdescriptor(member):
                l.append(member)

    # ignore the module and module dict
    if inspect.ismodule(x):
        l.extend([x, x.__dict__])

    # ignore a frame; this will not work with multi-threaded applications
    # use refcycle in that case for live applications
    if inspect.isframe(x):
        # this can't detect multi-threaded.
        l.extend([x])

    return l
Exemple #21
0
def _assert_no_instances(cls, when=''):
    __tracebackhide__ = True
    n = 0
    ref = list()
    gc.collect()
    objs = gc.get_objects()
    for obj in objs:
        try:
            check = isinstance(obj, cls)
        except Exception:  # such as a weakref
            check = False
        if check:
            rr = gc.get_referrers(obj)
            count = 0
            for r in rr:
                if r is not objs and \
                        r is not globals() and \
                        r is not locals() and \
                        not inspect.isframe(r):
                    if isinstance(r, (list, dict)):
                        rep = f'len={len(r)}'
                    else:
                        rep = repr(r)[:100].replace('\n', ' ')
                    ref.append(f'{r.__class__.__name__}: {rep}')
                    count += 1
                del r
            del rr
            n += count > 0
    assert n == 0, f'{n} {when}:\n' + '\n'.join(ref)
Exemple #22
0
 def tostr(o):
     if isframe(o):
         return getframeinfo(o)
     try:
         return tostring(o)
     except:
         return repr(o)
    def _where(cls, action, args):

        frame_info: Optional[inspect.Traceback] = None
        # Only inspect the slooow stack introspection if print()'ing or logging level is sufficient.
        if not cls._is_logging_on() or cls.logger.isEnabledFor(logging.INFO):
            syspath_caller: FrameType = inspect.currentframe().f_back.f_back
            if not (inspect.istraceback(syspath_caller) or inspect.isframe(syspath_caller)):
                return
            frame_info: inspect.Traceback = inspect.getframeinfo(syspath_caller)

        if frame_info:
            filename = PurePath(frame_info.filename)
            try:
                filename = PurePath(frame_info.filename).relative_to(sys.base_prefix)
            except ValueError:
                filename_orig = filename
                cwd = Path.cwd()
                while filename == filename_orig:
                    try:
                        filename = PurePath(frame_info.filename).relative_to(cwd)
                        break
                    except ValueError:
                        cwd = cwd.parent

            message = f"sys.path.{action}{args} from {filename}:{frame_info.lineno}"
            cls._inform_user(message)
Exemple #24
0
def _process_in_memory_objects(objects):
    """Processes objects tracked by GC.

    Processing is done in separate function to avoid generating overhead.
    """
    return _remove_duplicates(obj for obj in objects
                              if not inspect.isframe(obj))
Exemple #25
0
    def _find_lineno(self, obj, source_lines):
        """
        Return a line number of the given object's docstring.  Note:
        this method assumes that the object has a docstring.
        """
        lineno = None

        # Find the line number for modules.
        if inspect.ismodule(obj):
            lineno = 0

        # Find the line number for classes.
        # Note: this could be fooled if a class is defined multiple
        # times in a single file.
        if inspect.isclass(obj):
            if source_lines is None:
                return None
            pat = re.compile(r'^\s*class\s*%s\b' %
                             getattr(obj, '__name__', '-'))
            for i, line in enumerate(source_lines):
                if pat.match(line):
                    lineno = i
                    break

        # Find the line number for functions & methods.
        if inspect.ismethod(obj): obj = obj.__func__
        if inspect.isfunction(obj): obj = six.get_function_code(obj)
        if inspect.istraceback(obj): obj = obj.tb_frame
        if inspect.isframe(obj): obj = obj.f_code
        if inspect.iscode(obj):
            lineno = getattr(obj, 'co_firstlineno', None)-1

        # Find the line number where the docstring starts.  Assume
        # that it's the first line that begins with a quote mark.
        # Note: this 
        def show_referrers_type(cls, obj, depth, ignore=list()):
            # Debug function to get all references to an object and the
            # references to the references up to a depth of `depth`.
            import gc
            import textwrap
            import inspect
            l = []
            if depth > 0:
                referrers = gc.get_referrers(obj)
                for o in referrers:
                    if not any({o is i for i in ignore}):
                        for s in cls.show_referrers_type(o,
                                                         depth - 1,
                                                         ignore=ignore +
                                                         [referrers, o, obj]):
                            l.append(textwrap.indent(s, ' ' * 4))

            if inspect.isframe(obj):
                frame_info = inspect.getframeinfo(obj, context=1)
                if frame_info.function == 'show_referrers_type':
                    pass
                else:
                    info = f' {frame_info.function}, {frame_info.filename}:{frame_info.lineno}'
                    l.append(f'Frame: {type(obj)} {info}')
            else:
                l.append(str(type(obj)) + str(obj)[:80].replace('\n', ' '))
            return l
Exemple #27
0
def debugGetLocalVarFromThread(threadName, funcName, varName):
	th, stack = debugGetThreadStack(threadName)
	_tb = stack
	limit = None
	n = 0
	from inspect import isframe
	while _tb is not None and (limit is None or n < limit):
		if isframe(_tb): f = _tb
		else: f = _tb.tb_frame
		if f.f_code.co_name == funcName:
			if varName in f.f_locals:
				return f, f.f_locals[varName]
		if isframe(_tb): _tb = _tb.f_back
		else: _tb = _tb.tb_next
		n += 1		
	return None, None
Exemple #28
0
def dump_references(log, instances, exclude=[]):
    import gc
    import inspect
    gc.collect()
    frame = inspect.currentframe()
    try:
        exclude.append(instances)
        exclude.append([frame])
        for instance in instances:
            referrers = [x for x in gc.get_referrers(instance) if (x not in exclude and len([y for y in exclude if x in y])==0)]
            log.info("referrers for %s: %s", instance, len(referrers))
            for i in range(len(referrers)):
                r = referrers[i]
                log.info("[%s] in %s", i, type(r))
                if inspect.isframe(r):
                    log.info("  frame info: %s", str(inspect.getframeinfo(r))[:1024])
                elif type(r)==list:
                    listref = gc.get_referrers(r)
                    log.info("  list: %s..  %s referrers: %s", str(r[:32])[:1024], len(listref), str(listref[:32])[:1024])
                elif type(r)==dict:
                    if len(r)>64:
                        log.info("  %s items: %s", len(r), str(r)[:1024])
                        continue
                    for k,v in r.items():
                        if k is instance:
                            log.info("  key with value=%s", v)
                        elif v is instance:
                            log.info("  for key=%s", k)
                else:
                    log.info("     %s : %s", type(r), r)
    finally:
        del frame
    def step(self, depth, path):
        if self.shouldPrintStats:
            self.printStats(path)
            self.shouldPrintStats = False
        at = path[-1]
        if id(at) in self.visited:
            return
        if self.isAtRoot(at, path):
            self.found += 1
            return
        self.visited.add(id(at))
        referrers = [
            ref for ref in gc.get_referrers(at)
            if not (ref is path or inspect.isframe(ref)
                    or isinstance(ref, dict) and ref.keys() == locals().keys()
                    or ref is self.__dict__ or id(ref) in self.visited)
        ]
        if self.isManyRef(at, path, referrers):
            return
        while referrers:
            ref = referrers.pop()
            self.depth += 1
            for x in self.stepGenerator(depth + 1, path + [ref]):
                pass

            self.depth -= 1
Exemple #30
0
    def _format(self, obj, descend=True):
        """Return a string representation of a single object."""
        if inspect.isframe(obj):
            filename, lineno, func, context, index = inspect.getframeinfo(obj)
            return "<frame of function '%s'>" % func

        if not descend:
            return self.peek(repr(obj))

        if isinstance(obj, dict):
            return "{" + ", ".join([
                "%s: %s" % (self._format(
                    k, descend=False), self._format(v, descend=False))
                for k, v in obj.items()
            ]) + "}"
        elif isinstance(obj, list):
            return "[" + ", ".join(
                [self._format(item, descend=False) for item in obj]) + "]"
        elif isinstance(obj, tuple):
            return "(" + ", ".join(
                [self._format(item, descend=False) for item in obj]) + ")"

        r = self.peek(repr(obj))
        if isinstance(obj, (str, int, float)):
            return r
        return "%s: %s" % (type(obj), r)
Exemple #31
0
def debugGetLocalVarFromThread(threadName, funcName, varName):
    th, stack = debugGetThreadStack(threadName)
    _tb = stack
    limit = None
    n = 0
    from inspect import isframe
    while _tb is not None and (limit is None or n < limit):
        if isframe(_tb): f = _tb
        else: f = _tb.tb_frame
        if f.f_code.co_name == funcName:
            if varName in f.f_locals:
                return f, f.f_locals[varName]
        if isframe(_tb): _tb = _tb.f_back
        else: _tb = _tb.tb_next
        n += 1
    return None, None
Exemple #32
0
 def _find_lineno(self, obj, source_lines):
     lineno = None
     if inspect.ismodule(obj):
         lineno = 0
     if inspect.isclass(obj):
         if source_lines is None:
             return
         pat = re.compile('^\\s*class\\s*%s\\b' %
                          getattr(obj, '__name__', '-'))
         for (i, line) in enumerate(source_lines):
             while pat.match(line):
                 lineno = i
                 break
     if inspect.ismethod(obj):
         obj = obj.__func__
     if inspect.isfunction(obj):
         obj = obj.__code__
     if inspect.istraceback(obj):
         obj = obj.tb_frame
     if inspect.isframe(obj):
         obj = obj.f_code
     if inspect.iscode(obj):
         lineno = getattr(obj, 'co_firstlineno', None) - 1
     if lineno is not None:
         if source_lines is None:
             return lineno + 1
         pat = re.compile('(^|.*:)\\s*\\w*("|\')')
         for lineno in range(lineno, len(source_lines)):
             while pat.match(source_lines[lineno]):
                 return lineno
Exemple #33
0
    def __init__(self, exc_type, exc_value, tb, context=None):
        if inspect.isframe(tb):
            self.lineno = tb.f_lineno
            tb_frame = tb
        else:
            self.lineno = tb.tb_lineno
            tb_frame = tb.tb_frame

        self.function_name = tb_frame.f_code.co_name
        self.locals = tb_frame.f_locals
        self.globals = tb_frame.f_globals
        self.context = context

        fn = inspect.getsourcefile(tb) or inspect.getfile(tb)
        if fn[-4:] in ('.pyo', '.pyc'):
            fn = fn[:-1]
            # if it's a file on the file system resolve the real filename.
        if os.path.isfile(fn):
            fn = os.path.realpath(fn)
        self.filename = fn
        self.module = self.globals.get('__name__')
        self.loader = self.globals.get('__loader__')
        self.code = tb_frame.f_code

        # support for paste's traceback extensions
        self.hide = self.locals.get('__traceback_hide__', False)
        info = self.locals.get('__traceback_info__')
        if info is not None:
            try:
                info = text_(info)
            except UnicodeError:
                info = str(info).decode('utf-8', 'replace')
        self.info = info
Exemple #34
0
    def ascend(self, obj, depth=1):
        """Return a nested list containing referrers of the given object."""
        depth += 1
        parents = []

        # Gather all referrers in one step to minimize
        # cascading references due to repr() logic.
        refs = gc.get_referrers(obj)
        self.ignore.append(refs)
        if len(refs) > self.maxparents:
            return [("[%s referrers]" % len(refs), [])]

        try:
            ascendcode = self.ascend.__code__
        except AttributeError:
            ascendcode = self.ascend.im_func.func_code
        for parent in refs:
            if inspect.isframe(parent) and parent.f_code is ascendcode:
                continue
            if parent in self.ignore:
                continue
            if depth <= self.maxdepth:
                parents.append((parent, self.ascend(parent, depth)))
            else:
                parents.append((parent, []))

        return parents
Exemple #35
0
    def _get_ast_str(self, top):
        if not self._cache.is_empty(
        ):  # Data already computed and cached; could a "false-like" cached value
            return str(self._cache._data) if self._cache.is_scalar(
            ) else self._cache._id
        if self._cache._id is not None:
            return self._cache._id  # Data already computed under ID, but not cached
        assert isinstance(self._children, tuple)
        exec_str = "({} {})".format(
            self._op,
            " ".join([ExprNode._arg_to_expr(ast) for ast in self._children]))

        def is_ast_expr(ref):
            return isinstance(ref, list) and any(
                map(lambda r: isinstance(r, ASTId), ref))

        referrers = gc.get_referrers(self)
        # removing frames from the referrers to get a consistent behaviour accross Py versions
        #  as stack frames don't appear in the referrers from Py 3.7.
        # also removing the AST expressions built in astfun.py
        #  as they keep a reference to self if the lambda itself is using a free variable.
        proper_ref = [
            r for r in referrers if not (inspect.isframe(r) or is_ast_expr(r))
        ]
        ref_cnt = len(proper_ref)
        del referrers, proper_ref
        # if this self node is referenced by at least one other node (nested expr), then create a tmp frame
        if top or ref_cnt > 1:
            self._cache._id = _py_tmp_key(append=h2o.connection().session_id)
            exec_str = "(tmp= {} {})".format(self._cache._id, exec_str)
        return exec_str
Exemple #36
0
def getsourcelines(obj):
    (lines, lineno) = inspect.findsource(obj)
    if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
        return (lines, 1)
    if inspect.ismodule(obj):
        return (lines, 1)
    return (inspect.getblock(lines[lineno:]), lineno + 1)
Exemple #37
0
def ignore_frames(x):
    import inspect
    import types

    l = []
    if inspect.isclass(x):
        # must be a class object
        l.extend([x.__mro__, x.__dict__])

        if hasattr(x, '__weakref__'):
            l.extend([x.__weakref__])

        for member in x.__dict__.values():
            # ignore attributes.
            if inspect.isgetsetdescriptor(member):
                l.append(member)

    # ignore the module and module dict
    if inspect.ismodule(x):
        l.extend([x, x.__dict__])

    # ignore a frame; this will not work with multi-threaded applications
    # use refcycle in that case for live applications
    if inspect.isframe(x):
        # this can't detect multi-threaded.
        l.extend([x])

    return l
    def import_from(s1, module):
        syms = inspect.getmembers(module)
        str_syms = dir(module)
        name_as = ""
        if len(s1) == 4:
            name_as = s1[3][1]

        if not (s1[1][1] in str_syms):
            print("import error")
            exit()
        else:
            for sym in syms:
                if sym[0] == s1[1][1]:
                    if inspect.isfunction(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Class(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                    else:
                        print(sym[0])
Exemple #39
0
def _monocle_chain(to_gen, g, callback):
    # This function is complicated by the need to prevent unbounded recursion
    # arising from repeatedly yielding immediately ready callbacks.  This while
    # loop solves that by manually unfolding the recursion.

    while True:
        try:
            # Send the last result back as the result of the yield expression.
            start = time.time()
            try:
                if isinstance(to_gen, Exception):
                    from_gen = g.throw(type(to_gen), to_gen)
                elif isinstance(to_gen, TwistedFailure):
                    from_gen = g.throw(to_gen.type, to_gen.value, to_gen.tb)
                else:
                    from_gen = g.send(to_gen)
            finally:
                duration = (time.time() - start) * 1000
                if duration > blocking_warn_threshold:
                    if inspect.isframe(g.gi_frame):
                        fi = inspect.getframeinfo(g.gi_frame)
                        log.warn(
                            "oroutine '%s' blocked for %dms before %s:%s", g.__name__, duration, fi.filename, fi.lineno)
                    else:
                        log.warn(
                            "oroutine '%s' blocked for %dms", g.__name__, duration)
        except StopIteration:
            # "return" statement (or fell off the end of the generator)
            from_gen = Return()
        except Exception as e:
            callback(_add_monocle_tb(e, bounceback=(e == to_gen)))
            return callback

        if isinstance(from_gen, Return):
            try:
                g.close()
            except Exception as e:
                callback(_add_monocle_tb(e))
            else:
                callback(from_gen.value)
            return callback
        elif not isinstance(from_gen, Callback):
            if isinstance(from_gen, TwistedDeferred):
                cb = Callback()
                from_gen.addCallbacks(cb, lambda f: cb(_add_twisted_tb(f)))
                from_gen = cb
            else:
                e = InvalidYieldException(
                    "Unexpected value '%s' of type '%s' yielded from o-routine '%s'.  "
                    "O-routines can only yield Callback and Return types." % (from_gen, type(from_gen), g))
                return _monocle_chain(e, g, callback)

        if not hasattr(from_gen, 'result'):
            def gotResult(r):
                _monocle_chain(r, g, callback)
            from_gen.add(gotResult)
            return callback

        to_gen = from_gen.result
Exemple #40
0
def _pydoc_isdata_override(object):
    # yes, import in function is evil.
    # so is this function...
    import inspect
    return not (isinstance(object, partial) or
                inspect.ismodule(object) or inspect.isclass(object) or
                inspect.isroutine(object) or inspect.isframe(object) or
                inspect.istraceback(object) or inspect.iscode(object))
Exemple #41
0
def _pydoc_isdata_override(object):
    # yes, import in function is evil.
    # so is this function...
    import inspect
    return not (isinstance(object, partial) or inspect.ismodule(object)
                or inspect.isclass(object) or inspect.isroutine(object)
                or inspect.isframe(object) or inspect.istraceback(object)
                or inspect.iscode(object))
Exemple #42
0
 def getsourcelines(self, obj):
     lines, lineno = inspect.findsource(obj)
     if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
         # must be a module frame: do not try to cut a block out of it
         return lines, 1
     elif inspect.ismodule(obj):
         return lines, 1
     return inspect.getblock(lines[lineno:]), lineno + 1
Exemple #43
0
 def getsourcelines(self, obj):
     lines, lineno = inspect.findsource(obj)
     if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
         # must be a module frame: do not try to cut a block out of it
         return lines, 1
     elif inspect.ismodule(obj):
         return lines, 1
     return inspect.getblock(lines[lineno:]), lineno + 1
Exemple #44
0
def dump_gc_frames(logger=None):
    import gc
    #import types
    import inspect
    gc.collect()
    #frames = tuple(x for x in gc.get_objects() if isinstance(x, types.FrameType))
    frames = tuple((None, x) for x in gc.get_objects() if inspect.isframe(x))
    dump_frames(frames, logger)
 def parse_import(st):
     if st[0] == 283:
         import_name(st[2])
     elif st[0] == 284:
         module_name = ""
         if type(st[2]) != type(1):
             for name in st[2]:
                 if type(name) != type(1):
                     module_name += name[1]
         try:
             module = importlib.import_module(module_name)
         except ImportError:
             print("Import Error, No module named " + module_name)
             exit()
         if len(st) == 5 and st[4][1] == "*":
             syms = inspect.getmembers(module)
             str_syms = dir(module)
             name_as = ""
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                 else:
                     print(sym[0])
         else:
             for counter in range(len(st[4])):
                 if not isinstance(st[4][counter],
                                   int) and st[4][counter][0] == 285:
                     import_from(st[4][counter], module)
Exemple #46
0
    def import_name(s1):
        if s1[0] in NON_TERMINAL:
            if s1[0] in NON_TERMINAL and s1[0] == 286:
                dot_name = ""
                module_name = ""
                for name in s1[1]:
                    if type(name) != type(1):
                        module_name += name[1]
                if len(s1) == 2:
                    dot_name = module_name
                elif len(s1) == 4:
                    dot_name = s1[3][1]
                try:
                    module = importlib.import_module(module_name)
                except ImportError:
                    print("Import Error, No module named " + module_name)
                    exit()

                a = dir(module)
                syms = inspect.getmembers(module)
                for sym in syms:
                    if inspect.isfunction(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Class(dot_name + "." + sym[0], [], []))
                    else:
                        print(sym[0])
            else:
                for j in range(1, len(s1)):
                    import_name(s1[j])
Exemple #47
0
def source_findable(python_object):
    """Check if inspect.getfile has a chance to find the source."""
    return (inspect.ismodule(python_object) or
            inspect.isclass(python_object) or
            inspect.ismethod(python_object) or
            inspect.isfunction(python_object) or
            inspect.istraceback(python_object) or
            inspect.isframe(python_object) or
            inspect.iscode(python_object))
 def parse_import(st):
     if st[0] == 283:
         import_name(st[2])
     elif st[0] == 284:
         module_name = ""
         if type(st[2]) != type(1):
             for name in st[2]:
                 if type(name) != type(1):
                     module_name += name[1]
         try:
             module = importlib.import_module(module_name)
         except ImportError:
             print("Import Error, No module named " + module_name)
             exit()
         if len(st)==5 and st[4][1] == "*":
             syms = inspect.getmembers(module)
             str_syms = dir(module)
             name_as = ""
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                 else:
                     print(sym[0])
         else:
             for counter in range(len(st[4])):
                 if not isinstance(st[4][counter],int) and st[4][counter][0] == 285:
                     import_from(st[4][counter], module)
Exemple #49
0
def dump_references(log, instances, exclude=[]):
    import gc
    import inspect
    frame = inspect.currentframe()
    exclude.append(instances)
    exclude.append([frame])
    exclude = [[frame],]
    rexclude = exclude
    np = sys.modules.get("numpy")
    if np:
        rexclude = []
        skip_types = (np.ndarray, np.generic)
        for v in exclude:
            rexclude.append(tuple(x for x in v if not isinstance(x, skip_types)))
    del exclude
    gc.collect()
    try:
        log.info("dump references for %i instances:", len(instances))
        for j in range(len(instances)):
            instance = instances[j]
            referrers = tuple(x for x in gc.get_referrers(instance) if not any(y for y in rexclude if x in y))
            log.info("* %i : %s, type=%s, with %i referers", j, repr_ellipsized(str(instance)), type(instance), len(referrers))
            j += 1
            for i in range(len(referrers)):
                r = referrers[i]
                log.info("  [%s] in %s", i, type(r))
                if inspect.isframe(r):
                    log.info("    frame info: %s", str(inspect.getframeinfo(r))[:1024])
                elif type(r) in (list, tuple):
                    listref = gc.get_referrers(r)
                    lr = len(r)
                    if lr<=128:
                        log.info("    %i %s items: %s", lr, type(r), csv(repr_ellipsized(str(x)) for x in r))
                    elif lr<512:
                        log.info("    %i %s items: %s..", lr, type(r), repr_ellipsized(csv(r)))
                    else:
                        log.info("    %i %s items", lr, type(r))
                    ll = len(listref)
                    if ll<128:
                        log.info("    %i referrers: %s", ll, csv(repr_ellipsized(str(x)) for x in listref))
                    elif ll<512:
                        log.info("    %i referrers: %s", ll, repr_ellipsized(csv(listref)))
                    else:
                        log.info("    %i referrers", ll)
                elif type(r)==dict:
                    if len(r)>64:
                        log.info("    %s items: %s", len(r), repr_ellipsized(str(r)))
                        continue
                    for k,v in r.items():
                        if k is instance:
                            log.info("    key with value=%s", repr_ellipsized(str(v)))
                        elif v is instance:
                            log.info("    for key=%s", repr_ellipsized(str(k)))
                else:
                    log.info("     %s", repr_ellipsized(str(r)))
    finally:
        del frame
Exemple #50
0
    def stepGenerator(self, depth, path):
        if self.shouldPrintStats:
            self.printStats(path)

            self.shouldPrintStats = False

        at = path[-1]

        # check for success
        if (self.isAtRoot(at, path)):
            self.found += 1
            raise StopIteration

        if id(at) in self.visited:
            # don't continue down this path
            raise StopIteration

        # mark our progress after checking goal
        self.visited.add(id(at))

        # Look for all referrers, culling out the ones that
        # we know to be red herrings.
        referrers = [ref for ref in gc.get_referrers(at) \
                     if not (# we disregard the steps of our traversal
                             ref is path or \
                             # The referrer is this call frame

                             inspect.isframe(ref) or \
                             # The referrer is the locals() dictionary (closure)

                             (isinstance(ref, dict) and ref.keys() == locals().keys()) or \
                             # We found the reference on self

                             ref is self.__dict__ or \
                             # We've already seen this referrer

                             id(ref) in self.visited) ]

        # Check to see if this object has an unusually large
        # ref-count.  This usually indicates that it is some
        # sort of global, singleton, or manager object
        # and as such no further knowledge would be gained from
        # traversing further up the ref tree.
        if (self.isManyRef(at, path, referrers)):
            raise StopIteration

        while (referrers):
            ref = referrers.pop()
            self.depth += 1
            for x in self.stepGenerator(depth + 1, path + [ref]):
                yield None
                pass
            self.depth -= 1
            pass

        yield None
        pass
Exemple #51
0
def _is_visible_type(attribute):
    return not (inspect.isfunction(attribute) or inspect.ismethod(attribute)
                or inspect.isbuiltin(attribute) or inspect.isroutine(attribute)
                or inspect.isclass(attribute) or inspect.ismodule(attribute)
                or inspect.istraceback(attribute) or inspect.isframe(attribute)
                or inspect.iscode(attribute) or inspect.isabstract(attribute)
                or inspect.ismethoddescriptor(attribute)
                or inspect.isdatadescriptor(attribute)
                or inspect.isgetsetdescriptor(attribute)
                or inspect.ismemberdescriptor(attribute))
Exemple #52
0
def get_outer_frames(frame=None):
    '''
    get a list of frame records for a tracebacks frame and all inner
    frames. these frames represent the calls that lead to the current frame
    '''
    if frame == None:
        frame = inspect.currentframe()
        
    assert inspect.isframe(frame)
    [FrameRecord(fr) for fr in inspect.getouterframes(frame)]
Exemple #53
0
def hideframe(obj, set_trace=set_trace):
    def hideframe_code(c):
        if sys.version_info < (3,):
            return types.CodeType(
                c.co_argcount, c.co_nlocals, c.co_stacksize,
                c.co_flags, c.co_code,
                c.co_consts + (_HIDE_FRAME,),
                c.co_names, c.co_varnames, c.co_filename,
                c.co_name, c.co_firstlineno, c.co_lnotab,
                c.co_freevars, c.co_cellvars)
        # Python 3 takes an additional arg -- kwonlyargcount
        # typically set to 0
        return types.CodeType(
            c.co_argcount, 0, c.co_nlocals, c.co_stacksize,
            c.co_flags, c.co_code,
            c.co_consts + (_HIDE_FRAME,),
            c.co_names, c.co_varnames, c.co_filename,
            c.co_name, c.co_firstlineno, c.co_lnotab,
            c.co_freevars, c.co_cellvars)

    def hideframe_func(*funcs):
        for func in funcs:
            c = get_function_code(func)
            c = hideframe_code(c)
            func.func_code = c
        return funcs[-1] if funcs else None

    def hideframe_method(*methods):
        for method in methods:
            func = get_method_function(method)
            hideframe_func(func)
        return methods[-1] if methods else None

    def hideframe_frame(frame):
        filename = os.path.abspath(frame.f_code.co_filename)
        _HIDDEN_FILES.add(filename)
        # set_trace()
        return frame

    def hideframe_class(cls):
        funcs, methods = get_function_and_method_members(cls)
        hideframe_func(*funcs)
        hideframe_method(*methods)
        return cls

    if inspect.isfunction(obj):
        return hideframe_func(obj)
    if inspect.ismethod(obj):
        return hideframe_method(obj)
    if inspect.isclass(obj):
        return hideframe_class(obj)
    if inspect.isframe(obj):
        return hideframe_frame(obj)
    set_trace()
    return obj
    def stepGenerator(self, depth, path):
        if self.shouldPrintStats:
            self.printStats(path)
            
            self.shouldPrintStats = False

        at = path[-1]

        # check for success
        if (self.isAtRoot(at, path)):
            self.found += 1
            raise StopIteration 

        if id(at) in self.visited:
            # don't continue down this path
            raise StopIteration 

        # mark our progress after checking goal
        self.visited.add(id(at))

        # Look for all referrers, culling out the ones that
        # we know to be red herrings.
        referrers = [ref for ref in gc.get_referrers(at) \
                     if not (# we disregard the steps of our traversal
                             ref is path or \
                             # The referrer is this call frame
                             inspect.isframe(ref) or \
                             # The referrer is the locals() dictionary (closure)
                             (isinstance(ref, dict) and ref.keys() == locals().keys()) or \
                             # We found the reference on self
                             ref is self.__dict__ or \
                             # We've already seen this referrer
                             id(ref) in self.visited) ]

        # Check to see if this object has an unusually large
        # ref-count.  This usually indicates that it is some
        # sort of global, singleton, or manager object
        # and as such no further knowledge would be gained from
        # traversing further up the ref tree.
        if (self.isManyRef(at, path, referrers)):
            raise StopIteration 
            
        while(referrers):
            ref = referrers.pop()
            self.depth+=1
            for x in self.stepGenerator(depth + 1, path + [ref]):
                yield None
                pass
            self.depth-=1
            pass

        yield None
        pass
Exemple #55
0
def _get_line(obj):
    if inspect.ismethod(obj):
        obj = obj.__func__
    if inspect.isfunction(obj):
        obj = obj.__code__
    if inspect.istraceback(obj):
        obj = obj.tb_frame
    if inspect.isframe(obj):
        obj = obj.f_code
    if inspect.iscode(obj) and hasattr(obj, 'co_firstlineno'):
        return obj.co_firstlineno
    return None
Exemple #56
0
 def _clonable(of):
     sets = of.__dict__.copy()
     sets.update(type(of).__dict__.copy())
     final = sets.copy()
     for key in sets:
         v = sets[key]
         if isfunction(v) or ismethod(v) or isgeneratorfunction(v) or isgenerator(v) \
         or isroutine(v) or isabstract(v) or isclass(v) or ismodule(v) or istraceback(v) \
         or isframe(v) or iscode(v) or isbuiltin(v) or ismethoddescriptor(v) \
         or isdatadescriptor(v) or isgetsetdescriptor(v) or ismemberdescriptor(v) \
         or v is None or v == '__main__' or key == '__module__': final.pop(key)
     return final
def getframeinfo(frame, context=1):
    """
    Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line.

    This originally comes from ``inspect`` but is modified to handle issues
    with ``findsource()``.
    """
    if inspect.istraceback(frame):
        lineno = frame.tb_lineno
        frame = frame.tb_frame
    else:
        lineno = frame.f_lineno
    if not inspect.isframe(frame):
        raise TypeError('arg is not a frame or traceback object')

    filename = inspect.getsourcefile(frame) or inspect.getfile(frame)
    if context > 0:
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None
        else:
            start = max(start, 1)
            start = max(0, min(start, len(lines) - context))
            first_lines = lines[:2]
            lines = lines[start:(start + context)]
            index = lineno - 1 - start
    else:
        first_lines = lines = index = None

    # Code taken from Django's ExceptionReporter._get_lines_from_file
    if first_lines and isinstance(first_lines[0], bytes):
        encoding = 'ascii'
        for line in first_lines[:2]:
            # File coding may be specified. Match pattern from PEP-263
            # (http://www.python.org/dev/peps/pep-0263/)
            match = re.search(br'coding[:=]\s*([-\w.]+)', line)
            if match:
                encoding = match.group(1).decode('ascii')
                break
        lines = [line.decode(encoding, 'replace') for line in lines]

    if hasattr(inspect, 'Traceback'):
        return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index)
    else:
        return (filename, lineno, frame.f_code.co_name, lines, index)
Exemple #58
0
def DoFind(f, mod):
    import linecache

    if inspect.ismodule(mod):
        return f, 0, 0

    lines = linecache.getlines(f)

    if inspect.isclass(mod):
        name = mod.__name__
        pat = re.compile(r"^\s*class\s*" + name + r"\b")
        for i in xrange(len(lines)):
            if pat.match(lines[i]):
                return f, i, 0

        return f, 0, 0

    if inspect.ismethod(mod):
        mod = mod.im_func

    if inspect.isfunction(mod):
        try:
            mod = mod.func_code
        except AttributeError:
            mod = mod.__code__  # python 3k

    if inspect.istraceback(mod):
        mod = mod.tb_frame

    if inspect.isframe(mod):
        mod = mod.f_code

    if inspect.iscode(mod):
        if not hasattr(mod, "co_filename"):
            return None, 0, 0

        if not hasattr(mod, "co_firstlineno"):
            return mod.co_filename, 0, 0

        lnum = mod.co_firstlineno
        pat = re.compile(r"^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)")
        while lnum > 0:
            if pat.match(lines[lnum]):
                break
            lnum -= 1

        return f, lnum, 0

    raise RuntimeError("Do not know about: " + f + " " + str(mod))