コード例 #1
0
#!/usr/bin/env python3

import hoare_logic

autograder = hoare_logic.program_conditions("conditions.txt")

# Question 1:

scope = {'i': 2, 'j': 16, 'k': 54}
autograder.check_conditions({}, scope)

# Question 2:

scope['numbers'] = [6, 3, 14, 18, 8, -5, 23, -14]
autograder.check_conditions({}, scope)

# Question 3:

scope['numbers'] = [6, 3, 14, 18, 8, -5, 23, -14]
autograder.check_conditions({}, scope)

autograder.program_end({}, scope)
コード例 #2
0
    def mainTracer(self, frame, event, arg):
        '''
        This is a settrace() callback function.  It is the global callback
        function set during initialization.
        :param frame: frame object
        :param event String: "call", "line", "return", "exception", "opcode"
        :param arg:
        :return:    settrace() local callback function
        This function only processes the 'call' event.  During processing
        of the call event, the function sets the local callback function
        which will trace the lines within the called function.  Thus,
        innerFunction() and InnerFunctionStepover() trace line and return
        events.
        The first invocation is a call event to <module>
        '''
        self.curFrame = frame
        self.curEvent = event
        # if self.scope is None or self.scope.is_empty():
        #     self.scope = LinkedList.FrameList()
        #     self.scope.insert_frame(frame.f_code.co_name, frame.f_locals)
        #     del self.scope.current_frame.frame_vars['__builtins__']
        #     self.scope.insert_frame(frame.f_code.co_name, frame.f_locals)
        # else:
        #     if event == 'call'or event == 'line':
        #         self.scope.insert_frame(frame.f_code.co_name, frame.f_locals)
        #     if event == 'return':
        #         self.scope.exit_frame()

        # push current frame onto CactusStack
        print("mainTracer(): push node ", frame.f_code.co_name)
        # if first time through
        if self.logic_checker is None:
            self.logic_checker = hoare_logic.program_conditions(
                "conditions.txt")
        if self.CactusStack is None or self.CactusStack.is_empty():
            self.CactusStack = None
            self.scope = None
            self.CactusStack = Node(frame.f_code.co_name, frame.f_locals)
            self.CactusStack.reset_scopes()
            del self.CactusStack.current_frame.vars['__builtins__']
        else:
            # add node for current frame
            self.logic_checker.check_conditions(
                {}, scopes=self.CactusStack.current_frame.vars)
            self.CactusStack.push(Node(frame.f_code.co_name, frame.f_locals))

        print('waiting at mainTracer(): ', event, ' ', frame.f_code.co_name)
        # print the current source line
        print(str(frame.f_lineno) + '\t' +
              linecache.getline(self.filepath[1:], frame.f_lineno),
              end='')
        # wait for a commend
        self.WaitUntil(1)
        print('mainTracer(): ', self.command)

        # The normal means of quitting is when a local trace function
        # encounters a returm from <module>.
        # if commanded to quit, push frame and exit
        if self.quitValue == 1:
            print("mainTracer(): quitValue")
            self.CactusStack.push(Node(frame.f_code.co_name, frame.f_locals))
            self.logic_checker.check_conditions(
                {}, scopes=self.CactusStack.current_frame.vars)
            raise SystemExit()
        if self.command != "stepover":
            self.Set(0)
        # one way that stepover must end, at outer scope
        if self.command == "stepover" and frame.f_code.co_name == '<module>':
            print('here I got')
            self.Set(0)
        if event == 'call' and self.command == 'step':
            # set local settrace() callback function for stepping
            return self.innerFunction
        if event == 'call' and self.command == 'stepover' and frame.f_code.co_name != '<module>':
            self.stepover_call_depth += 1
            # switch settrace() local callback function to innerFunctionStepover()
            return self.innerFunctionStepover