Пример #1
0
    def test_count_then_return(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()), int_type(),
                literal_op(0),
                loop_op(
                    comma_op(
                        assignment_op(
                            context_op(), literal_op("local"),
                            addition_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(1))),
                        condition_op(
                            equality_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(42)),
                            return_op(
                                dereference_op(context_op(),
                                               literal_op("local"))),
                            nop())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #2
0
    def test_initialization_from_argument(self):
        _, result = bootstrap_function(function_lit(
            int_type(), build_break_types(int_type()), int_type(),
            dereference_op(context_op(), literal_op("argument"), True),
            comma_op(
                return_op(
                    dereference_op(context_op(), literal_op("local"), True)))),
                                       argument=123,
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 123)
Пример #3
0
    def test_doubler(self):
        func = function_lit(
            int_type(), build_break_types(int_type()),
            return_op(
                addition_op(
                    dereference_op(context_op(), literal_op("argument"), True),
                    dereference_op(context_op(), literal_op("argument"),
                                   True))))

        _, result = bootstrap_function(func, argument=21)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #4
0
    def test_assignment(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(int_type()), int_type(),
            literal_op(0),
            comma_op(
                assignment_op(context_op(), literal_op("local"),
                              literal_op(42)),
                return_op(
                    dereference_op(context_op(), literal_op("local"), True)))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #5
0
    def test_restart_into_local_initialization(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(any_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }), int_type(),
                shift_op(literal_op("hello"), int_type()),
                return_op(
                    dereference_op(context_op(), literal_op("local"), True))),
            context, frame_manager).close(None)

        def start():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as yielder:
            start()

        self.assertEquals(yielder.value, "hello")

        def restart():
            yielder_restart_continuation = yielder.create_continuation(
                start,
                func.get_type().break_types)
            yielder_restart_continuation.invoke(32, frame_manager)

        with frame_manager.capture("return") as returner:
            restart()

        self.assertEquals(returner.value, 32)
Пример #6
0
    def test_return_with_dereference4(self):
        _, result = bootstrap_function(function_lit(
            int_type(),
            build_break_types(
                object_type({
                    "foo": any_type(),
                    "bar": any_type()
                })),
            comma_op(
                return_op(
                    object_template_op({
                        "foo":
                        literal_op(42),
                        "bar":
                        dereference_op(context_op(), literal_op("argument"),
                                       True)
                    })))),
                                       argument=42,
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertTrue(isinstance(result.value, Universal))
        get_manager(
            result.value).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
        self.assertEquals(result.value._get("foo"), 42)
        self.assertEquals(result.value._get("bar"), 42)
Пример #7
0
    def test_basic_with_inferred_local_type(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), infer_all(), inferred_type(),
                close_op(
                    static_op(
                        prepare_op(
                            literal_op(
                                function_lit(no_value_type(), infer_all(),
                                             return_op(literal_op(42)))))),
                    context_op()),
                return_op(
                    invoke_op(
                        dereference_op(context_op(), literal_op("local"),
                                       True)))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #8
0
def build_executor(raw_code):
    return function_lit(
        transform_op(
            "yield", "read",
            reset_op(
                close_op(
                    static_op(
                        prepare_op(
                            literal_op(
                                parse(raw_code,
                                      post_chain_function=CodeBlockBuilder(
                                          [build_looper()]))))), context_op()),
                nop())))
Пример #9
0
 def test_catch_real_exception(self):
     #  Function safely handles an internal exception
     func = function_lit(
         try_catch_op(
             dereference_op(context_op(), literal_op("foo"), True),
             prepared_function(
                 object_type({
                     "type": const_string_type(),
                     "message": const_string_type(),
                 }), return_op(dereference("argument.message"))), nop()))
     _, result = bootstrap_function(func)
     self.assertEquals(result.caught_break_mode, "return")
     self.assertEquals(result.value, "DereferenceOp: invalid_dereference")
Пример #10
0
    def test_add_locals(self):
        func = function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(
                addition_op(
                    dereference_op(
                        dereference_op(
                            dereference_op(context_op(), literal_op("outer"),
                                           True), literal_op("local"), True),
                        literal_op(0), True),
                    dereference_op(
                        dereference_op(
                            dereference_op(context_op(), literal_op("outer"),
                                           True), literal_op("local"), True),
                        literal_op(1), True))))

        context = PythonObject(
            {
                "local":
                PythonList([39, 3]),
                "_types":
                PythonObject({
                    "local":
                    UniversalTupleType([IntegerType(),
                                        IntegerType()])
                })
            },
            bind=UniversalObjectType({
                "local":
                UniversalTupleType([IntegerType(),
                                    IntegerType()]),
                "_types":
                DEFAULT_READONLY_COMPOSITE_TYPE
            }))

        _, result = bootstrap_function(func, outer_context=context)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #11
0
    def test_basic(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()),
                return_op(
                    invoke_op(
                        close_op(
                            static_op(
                                prepare_op(
                                    literal_op(
                                        function_lit(
                                            no_value_type(),
                                            build_break_types(int_type()),
                                            return_op(literal_op(42)))))),
                            context_op())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #12
0
    def test_restart_into_local_initialization_and_code(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(any_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }), int_type(),
                shift_op(literal_op("first"), int_type()),
                return_op(
                    addition_op(
                        dereference_op(context_op(), literal_op("local"),
                                       True),
                        shift_op(literal_op("second"), int_type())))), context,
            frame_manager).close(None)

        def first():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as first_yielder:
            first()
        self.assertEquals(first_yielder.value, "first")

        def second():
            first_restart_continuation = first_yielder.create_continuation(
                first,
                func.get_type().break_types)
            first_restart_continuation.invoke(40, frame_manager)

        with frame_manager.capture("yield") as second_yielder:
            second()
        self.assertEquals(second_yielder.value, "second")

        with frame_manager.capture("return") as returner:
            second_restart_continuation = second_yielder.create_continuation(
                first,
                func.get_type().break_types)
            second_restart_continuation.invoke(2, frame_manager)

        self.assertEquals(returner.value, 42)
Пример #13
0
 def test_return_with_dereference5(self):
     with self.assertRaises(Exception):
         bootstrap_function(function_lit(
             int_type(),
             build_break_types(
                 object_type({
                     "foo": any_type(),
                     "bar": unit_type(42)
                 })),
             return_op(
                 object_template_op({
                     "foo":
                     literal_op(42),
                     "bar":
                     dereference_op(context_op(), literal_op("argument"),
                                    True)
                 }))),
                            argument=42,
                            check_safe_exit=True)
Пример #14
0
    def search_for_reference(self, reference, debug_info):
        from lockdown.executor.raw_code_factories import dereference_op, assignment_op, \
            literal_op, dereference, context_op

        if not isinstance(reference, basestring):
            raise FatalError()

        if reference in ("prepare", "local", "argument", "outer", "static"):
            return context_op(), False

        types_search = self.search_context_type_for_reference(
            reference, self.context_type, [], debug_info)
        if types_search:
            return types_search, False
        statics_search = self.search_statics_for_reference(
            reference, self.context, [], debug_info)
        if statics_search:
            return statics_search, True

        return None, False
Пример #15
0
    def test_return_variable(self):
        func = function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(
                dereference_op(
                    dereference_op(context_op(), literal_op("outer"), True),
                    literal_op("local"), True)))

        context = PythonObject(
            {
                "local": 42,
                "_types": PythonObject({"local": IntegerType()})
            },
            bind=UniversalObjectType(
                {
                    "local": IntegerType(),
                    "_types": DEFAULT_READONLY_COMPOSITE_TYPE
                },
                wildcard_type=RICH_READONLY_TYPE))

        _, result = bootstrap_function(func, outer_context=context)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Пример #16
0
def build_looper():
    return invoke_op(
        close_op(prepare_op(shift_op(literal_op(42), any_type())),
                 context_op()), nop())