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
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
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
def check(inp, out): self.assertEqual(flatten_list(inp), out)