Пример #1
0
    def trace_function(self, func):
        # type: (FunctionType) -> FunctionType
        new_func = super(BirdsEye, self).trace_function(func)
        code_info = self._code_infos.get(new_func.__code__)
        if code_info:
            return new_func

        lines, start_lineno = inspect.getsourcelines(
            func)  # type: List[Text], int
        end_lineno = start_lineno + len(lines)
        name = safe_qualname(func)
        source_file = inspect.getsourcefile(func)
        if source_file.startswith('<ipython-input'):
            filename = IPYTHON_FILE_PATH
        else:
            filename = os.path.abspath(source_file)
        traced_file = new_func.traced_file

        arg_info = inspect.getargs(new_func.__code__)
        arg_names = list(chain(flatten_list(arg_info[0]),
                               arg_info[1:]))  # type: List[str]
        self._trace(name,
                    filename,
                    traced_file,
                    new_func.__code__,
                    typ='function',
                    start_lineno=start_lineno,
                    end_lineno=end_lineno,
                    arg_names=arg_names)

        return new_func
Пример #2
0
    def trace_function(self, func):
        # type: (FunctionType) -> FunctionType
        new_func = super(BirdsEye, self).trace_function(func)
        code_info = self._code_infos.get(new_func.__code__)
        if code_info:
            return new_func

        lines, start_lineno = inspect.getsourcelines(func)  # type: List[Text], int
        end_lineno = start_lineno + len(lines)
        name = safe_qualname(func)
        source_file = inspect.getsourcefile(func)
        if source_file.startswith('<ipython-input'):
            filename = IPYTHON_FILE_PATH
        else:
            filename = os.path.abspath(source_file)
        nodes = list(self._nodes_of_interest(new_func.traced_file, start_lineno, end_lineno))
        html_body = self._nodes_html(nodes, start_lineno, end_lineno, new_func.traced_file)
        data = json.dumps(dict(
            node_loops={
                node._tree_index: [n._tree_index for n in node._loops]
                for node, _ in nodes
                if node._loops
            }),
            sort_keys=True)
        db_func = self._db_func(data, filename, html_body, name, start_lineno)
        arg_info = inspect.getargs(new_func.__code__)
        arg_names = list(chain(flatten_list(arg_info[0]), arg_info[1:]))  # type: List[str]
        self._code_infos[new_func.__code__] = CodeInfo(db_func, new_func.traced_file, arg_names)
        return new_func
Пример #3
0
def repr_object(x, helper):
    s = repr(x)
    if len(s) > cheap_repr.suppression_threshold:
        cls = x.__class__
        suppressed_classes.add(cls)
        warnings.warn(
            ReprSuppressedWarning(
                '%s.__repr__ is too long and has been suppressed. '
                'Register a repr for the class to avoid this warning '
                'and see an informative repr again, '
                'or increase cheap_repr.suppression_threshold' %
                safe_qualname(cls)))
    return helper.truncate(s)
Пример #4
0
    def trace_function(self, func):
        # type: (FunctionType) -> FunctionType
        new_func = super(BirdsEye, self).trace_function(func)
        code_info = self._code_infos.get(new_func.__code__)
        if code_info:
            return new_func

        lines, start_lineno = inspect.getsourcelines(
            func)  # type: List[Text], int
        end_lineno = start_lineno + len(lines)
        name = safe_qualname(func)
        source_file = inspect.getsourcefile(func)
        if source_file.startswith('<ipython-input'):
            filename = IPYTHON_FILE_PATH
        else:
            filename = os.path.abspath(source_file)
        nodes = list(
            self._nodes_of_interest(new_func.traced_file, start_lineno,
                                    end_lineno))
        html_body = self._nodes_html(nodes, start_lineno, end_lineno,
                                     new_func.traced_file)
        tokens = new_func.traced_file.tokens
        func_node = only(node for node, _ in nodes
                         if isinstance(node, ast.FunctionDef)
                         and node.first_token.start[0] == start_lineno)
        func_startpos, raw_body = source_without_decorators(tokens, func_node)
        data_dict = dict(
            # These are for the PyCharm plugin
            node_ranges=list(self._node_ranges(nodes, tokens, func_startpos)),
            loop_ranges=list(self._loop_ranges(nodes, tokens, func_startpos)),

            # This maps each node to the loops enclosing that node
            node_loops={
                node._tree_index: [n._tree_index for n in node._loops]
                for node, _ in nodes if node._loops
            })
        data = json.dumps(data_dict, sort_keys=True)
        db_func = self._db_func(data, filename, html_body, name, start_lineno,
                                raw_body)
        arg_info = inspect.getargs(new_func.__code__)
        arg_names = list(chain(flatten_list(arg_info[0]),
                               arg_info[1:]))  # type: List[str]
        self._code_infos[new_func.__code__] = CodeInfo(db_func,
                                                       new_func.traced_file,
                                                       arg_names)
        return new_func
Пример #5
0
def _try_repr(func, x, *args):
    try:
        return func(x, *args)
    except BaseException as e:
        should_raise = (cheap_repr.raise_exceptions or
                        getattr(func, 'raise_exceptions', False) or
                        func is repr and __raise_exceptions_from_default_repr)
        if should_raise:
            raise
        cls = x.__class__
        if cls not in suppressed_classes:
            suppressed_classes.add(cls)
            warnings.warn(ReprSuppressedWarning(
                "Exception '%s' in %s for object of type %s. "
                "The repr has been suppressed for this type." %
                (exception_string(e), func.__name__, safe_qualname(cls))))
        return _basic_but('exception in repr', x)
Пример #6
0
 def names(self):
     # type: () -> List[str]
     rev = dict((v, k) for k, v in self.data.items())
     return [safe_qualname(rev[i]) for i in range(len(rev))]
Пример #7
0
def repr_function(func, _helper):
    return '<function %s at %#x>' % (
        safe_qualname(func), id(func))