示例#1
0
    def __call__(self, *runtime_args, **runtime_kwargs):
        """ Execute the function, passing it the arguments received at
        runtime. Also call the function in self.call_afterwards and pass it
        all runtime_args and runtime_kwargs.
        If the very first argument is a LogoCode instance, it is removed.
        The active turtle, the Turtles object, the canvas, the LogoCode
        object, or the TurtleArtWindow object will be prepended to the
        arguments (depending on what this Primitive wants). """

        # remove the first argument if it is a LogoCode instance
        if runtime_args and isinstance(runtime_args[0], LogoCode):
            runtime_args = runtime_args[1:]

        if Primitive._DEBUG:
            debug_output(repr(self))
            debug_output("  runtime_args: " + repr(runtime_args))
        # fill the ArgSlots with the runtime arguments
        new_prim = self.fill_slots(runtime_args, runtime_kwargs,
                                   convert_to_ast=False)
        if not new_prim.are_slots_filled():
            raise logoerror("#syntaxerror")
        if Primitive._DEBUG:
            debug_output("  new_prim.arg_descs: " + repr(new_prim.arg_descs))

        # extract the actual values from the (now constant) arguments
        (new_args, new_kwargs) = new_prim.get_values_of_filled_slots()
        if Primitive._DEBUG:
            debug_output("  new_args: " + repr(new_args))
            debug_output("end " + repr(self))

        # what does this primitive want as its first argument?
        first_arg = None
        if not is_bound_method(new_prim.func):
            if new_prim.wants_turtle():
                first_arg = global_objects["turtles"].get_active_turtle()
            elif new_prim.wants_turtles():
                first_arg = global_objects["turtles"]
            elif new_prim.wants_canvas():
                first_arg = global_objects["canvas"]
            elif new_prim.wants_logocode():
                first_arg = global_objects["logo"]
            elif new_prim.wants_heap():
                first_arg = global_objects["logo"].heap
            elif new_prim.wants_tawindow():
                first_arg = global_objects["window"]
            else:
                result, plugin = new_prim.wants_plugin()
                if result:
                    first_arg = plugin

        # execute the actual function
        if first_arg is None:
            return_value = new_prim.func(*new_args, **new_kwargs)
        else:
            return_value = new_prim.func(first_arg, *new_args, **new_kwargs)

        if new_prim.call_afterwards is not None:
            new_prim.call_afterwards(*new_args, **new_kwargs)

        return return_value
    def __call__(self, *runtime_args, **runtime_kwargs):
        """ Execute the function, passing it the arguments received at
        runtime. Also call the function in self.call_afterwards and pass it
        all runtime_args and runtime_kwargs.
        If the very first argument is a LogoCode instance, it is removed.
        The active turtle, the Turtles object, the canvas, the LogoCode
        object, or the TurtleArtWindow object will be prepended to the
        arguments (depending on what this Primitive wants). """

        # remove the first argument if it is a LogoCode instance
        if runtime_args and isinstance(runtime_args[0], LogoCode):
            runtime_args = runtime_args[1:]

        if Primitive._DEBUG:
            debug_output(repr(self))
            debug_output("  runtime_args: " + repr(runtime_args))
        # fill the ArgSlots with the runtime arguments
        new_prim = self.fill_slots(runtime_args, runtime_kwargs,
                                   convert_to_ast=False)
        if not new_prim.are_slots_filled():
            raise logoerror("#syntaxerror")
        if Primitive._DEBUG:
            debug_output("  new_prim.arg_descs: " + repr(new_prim.arg_descs))

        # extract the actual values from the (now constant) arguments
        (new_args, new_kwargs) = new_prim.get_values_of_filled_slots()
        if Primitive._DEBUG:
            debug_output("  new_args: " + repr(new_args))
            debug_output("end " + repr(self))

        # what does this primitive want as its first argument?
        first_arg = None
        if not is_bound_method(new_prim.func):
            if new_prim.wants_turtle():
                first_arg = global_objects["turtles"].get_active_turtle()
            elif new_prim.wants_turtles():
                first_arg = global_objects["turtles"]
            elif new_prim.wants_canvas():
                first_arg = global_objects["canvas"]
            elif new_prim.wants_logocode():
                first_arg = global_objects["logo"]
            elif new_prim.wants_heap():
                first_arg = global_objects["logo"].heap
            elif new_prim.wants_tawindow():
                first_arg = global_objects["window"]
            else:
                result, plugin = new_prim.wants_plugin()
                if result:
                    first_arg = plugin

        # execute the actual function
        if first_arg is None:
            return_value = new_prim.func(*new_args, **new_kwargs)
        else:
            return_value = new_prim.func(first_arg, *new_args, **new_kwargs)

        if new_prim.call_afterwards is not None:
            new_prim.call_afterwards(*new_args, **new_kwargs)

        return return_value