Beispiel #1
0
    def _treetrace_hidden_before_expr(self, traced_file, _tree_index):
        # type: (TracedFile, int) -> ast.expr
        """
        Called directly from the modified code before an expression is
        evaluated.
        """
        node = traced_file.nodes[_tree_index]
        node = cast(ast.expr, node)
        frame = inspect.currentframe().f_back  # type: FrameType

        frame_info = self.stack.get(frame)
        if frame_info is None:
            # This means there are no statements in this frame, and it's just started
            frame_info = FrameInfo()
            self.stack[frame] = frame_info

            # This is likely inside a comprehension. Find the parent frame corresponding
            # to a normal function call
            owner_frame = frame
            while owner_frame.f_code.co_name in ('<listcomp>', '<dictcomp>', '<setcomp>'):
                owner_frame = owner_frame.f_back
            if owner_frame != frame:
                comprehension = safe_next(of_type(self.SPECIAL_COMPREHENSION_TYPES,
                                                  ancestors(node)))  # type: ast.expr
                self.stack[owner_frame].comprehension_frames[comprehension] = frame

        frame_info.expression_stack.append(node)

        self.before_expr(node, frame)
        return node
Beispiel #2
0
    def _treetrace_hidden_before_expr(self, traced_file, _tree_index):
        node = traced_file.nodes[_tree_index]
        frame = inspect.currentframe().f_back

        frame_info = self.stack.get(frame)
        if frame_info is None:
            frame_info = FrameInfo()
            self.stack[frame] = frame_info
            owner_frame = frame
            while owner_frame.f_code.co_name in ('<listcomp>', '<dictcomp>', '<setcomp>'):
                owner_frame = owner_frame.f_back
            if owner_frame != frame:
                comprehension = safe_next(of_type(self.SPECIAL_COMPREHENSION_TYPES,
                                                  ancestors(node)))
                self.stack[owner_frame].comprehension_frames[comprehension] = frame

        frame_info.expression_stack.append(node)

        self.before_expr(node, frame)
        return node