Beispiel #1
0
    def test_modifying(self):
        At = UniversalObjectType({
            "foo": IntegerType()
        })

        Bt = UniversalObjectType({
            "bar": At
        })

        A = PythonObject({
            "foo": 5
        })
        B = PythonObject({
            "bar": A
        }, bind=Bt)

        self.assertEquals(len(get_manager(A).attached_types), 1)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 1)

        B.bar = PythonObject({
            "foo": 42
        }, bind=At)
        
        self.assertEquals(len(get_manager(A).attached_types), 0)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 0)
Beispiel #2
0
    def test_that_python_constraints_dont_spread_to_constrained_children(self):
        bar = PythonObject({
            "baz": 42
        })
        foo = PythonObject({
            "bar": bar
        })

        # The first, stronger, type prevents the NO_SETTER_ERROR_COMPOSITE_TYPE spreading from foo to bar
        get_manager(bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
        get_manager(foo).add_composite_type(NO_SETTER_ERROR_COMPOSITE_TYPE)

        self.assertIs(foo.bar, bar)

        self.assertEquals(len(get_manager(foo).attached_types), 1)
        self.assertEquals(len(get_manager(foo.bar).attached_types), 1)

        # ... but when bar is replaced with a new object without constraints, the PythonObjectType
        # spreads to the new object
        foo.bar = PythonObject({
            "baz": 123
        })

        self.assertIsNot(foo.bar, bar)

        self.assertEquals(len(get_manager(foo.bar).attached_types), 1)

        # Now that the new object has the PythonObjectType constraint, we can't bind a stronger
        # constraint
        with self.assertRaises(CompositeTypeIncompatibleWithTarget):
            get_manager(foo.bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
Beispiel #3
0
    def test_adding_and_removing(self):
        A = PythonObject({
            "foo": 5
        })
        B = PythonObject({
            "bar": A
        })

        At = UniversalObjectType({
            "foo": IntegerType()
        })

        Bt = UniversalObjectType({
            "bar": At
        })

        get_manager(A).add_composite_type(At)
        self.assertEquals(len(get_manager(A).attached_types), 1)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 1)
        get_manager(B).add_composite_type(Bt)
        self.assertEquals(len(get_manager(A).attached_types), 1)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 2)
        get_manager(B).remove_composite_type(Bt)
        self.assertEquals(len(get_manager(A).attached_types), 1)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 1)
Beispiel #4
0
    def test_interesting(self):
        func = function_lit(
            any_type(), infer_all(),
            match_op(dereference("argument"), [
                prepared_function(
                    object_type({"foo": int_type()}),
                    return_op(
                        addition_op(dereference("argument.foo"),
                                    literal_op(3)))),
                prepared_function(any_type(), return_op(literal_op("invalid")))
            ]))

        _, result = bootstrap_function(func,
                                       argument=PythonObject(
                                           {"foo": 39},
                                           bind=UniversalObjectType(
                                               {"foo": IntegerType()})))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)

        _, result = bootstrap_function(func,
                                       argument=PythonObject({"foo": "hello"}))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
Beispiel #5
0
 def test_misc1(self):
     # Came up testing lockdown local variable binding
     PythonObject({
         "local": PythonList([ 39, 3 ]),
         "types": PythonObject({})
     }, bind=UniversalObjectType({
         "local": UniversalTupleType([ IntegerType(), IntegerType() ]),
         "types": DEFAULT_READONLY_COMPOSITE_TYPE
     }, wildcard_type=RICH_READONLY_TYPE))
Beispiel #6
0
    def test_composite_object_assignment(self):
        obj = PythonObject({ "foo": "hello" })

        get_manager(obj).add_composite_type(CompositeType({
            ("get", "foo"): GetterMicroOpType("foo", StringType()),
            ("set", "foo"): SetterMicroOpType("foo", StringType())
        }, name="test"))

        obj.foo = "what"
Beispiel #7
0
    def test_composite_object_invalid_assignment(self):
        obj = PythonObject({ "foo": "hello" })

        get_manager(obj).add_composite_type(CompositeType({
            ("get", "foo"): GetterMicroOpType("foo", StringType()),
            ("set", "foo"): SetterMicroOpType("foo", StringType())
        }, name="test"))

        with self.assertRaises(TypeError):
            obj.foo = 5
Beispiel #8
0
    def test_const_property(self):
        obj = PythonObject({ "foo": "hello" })

        get_manager(obj).add_composite_type(CompositeType({
            ("get", "foo"): GetterMicroOpType("foo", StringType())
        }, name="test"))

        self.assertEquals(obj.foo, "hello")
        with self.assertRaises(Exception):
            obj.foo = "what"
Beispiel #9
0
    def test_python_random_read_fails_nicely(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        with self.assertRaises(AttributeError):
            foo.bop
Beispiel #10
0
    def test_python_like_object(self):
        obj = PythonObject({ "foo": "hello" })

        get_manager(obj).add_composite_type(CompositeType({
            ("get", "foo"): GetterMicroOpType("foo", AnyType()),
            ("set", "foo"): SetterMicroOpType("foo", AnyType())
        }, name="test"))

        self.assertEquals(obj.foo, "hello")
        obj.foo = "what"
        self.assertEquals(obj.foo, "what")
Beispiel #11
0
    def test_python_delete_works(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        del foo.bar
        self.assertFalse(hasattr(foo, "bar"))
Beispiel #12
0
    def test_changes_blocked_without_micro_ops(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo)

        with self.assertRaises(Exception):
            foo.bar = "hello"
Beispiel #13
0
    def test_can_fail_micro_ops_are_enforced(self):
        foo = PythonObject({
            "foo": 5,
            "bar": "hello"
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({ "foo": Const(IntegerType()) })
        )

        with self.assertRaises(Exception):
            foo.foo = "hello"
Beispiel #14
0
    def test_python_replacing_object_works(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        foo.bar = PythonObject({ "baz": 123 })

        self.assertEquals(foo.bar.baz, 123)
Beispiel #15
0
    def test_python_object_with_reference_types_are_enforced(self):
        bar = PythonObject({
            "baz": 42
        })
        foo = PythonObject({
            "bar": bar
        })

        get_manager(bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        with self.assertRaises(Exception):
            foo.bar.baz = "hello"
Beispiel #16
0
    def test_python_like_type(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(RICH_TYPE)

        foo.bar.baz = 22

        foo.bar = "hello"
        self.assertEquals(foo.bar, "hello")
Beispiel #17
0
    def test_very_broad_assignment(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({ "bar": AnyType() })
        )

        foo.bar = "hello"
        self.assertEquals(foo.bar, "hello")
Beispiel #18
0
    def test_python_object_with_reference_can_be_modified(self):
        bar = PythonObject({
            "baz": 42
        })
        foo = PythonObject({
            "bar": bar
        })

        get_manager(bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        self.assertEqual(foo.bar.baz, 42)
        foo.bar.baz = 5
        self.assertEqual(foo.bar.baz, 5)
Beispiel #19
0
    def test_to_match_with_one_of_type_combo(self):
        func = function_lit(
            one_of_type([string_type(), int_type(),
                         bool_type()]),
            return_op(
                match_op(dereference("argument"), [
                    prepared_function(int_type(),
                                      literal_op("int is not a string")),
                    prepared_function(bool_type(),
                                      literal_op("bool is not a string")),
                    prepared_function(inferred_type(), dereference("argument"))
                ])))

        _, result = bootstrap_function(func, argument=2)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "int is not a string")
        _, result = bootstrap_function(func, argument=True)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "bool is not a string")
        _, result = bootstrap_function(func, argument="hello world")
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "hello world")

        prepared_func = prepare(func, PythonObject({}), FrameManager())
        self.assertEquals(len(prepared_func.break_types), 1)
        self.assertTrue("return" in prepared_func.break_types)
        for return_break_type in prepared_func.break_types["return"]:
            self.assertTrue(StringType().is_copyable_from(
                return_break_type["out"], DUMMY_REASONER))
Beispiel #20
0
    def test_single_restart(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()
                                  }),
                return_op(
                    addition_op(shift_op(literal_op("hello"), int_type()),
                                literal_op(40)))), context,
            frame_manager).close(None)

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

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

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

        def second():
            yielder.create_continuation(first, {}).invoke(2, frame_manager)

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

        self.assertEquals(returner.value, 42)
Beispiel #21
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)
Beispiel #22
0
    def visitObj(self, ctx):
        result = {}
        for pair in ctx.pair() or []:
            pair = self.visit(pair)
            result[pair[0]] = pair[1]

        return PythonObject(result, bind=DEFAULT_READONLY_COMPOSITE_TYPE)
def loop_op(opcode, **kwargs):
    check_is_opcode(opcode)
    return PythonObject(spread_dict({
        "opcode": "loop",
        "code": opcode
    }, **kwargs),
                        debug_reason="code")
def static_op(expression, **kwargs):
    check_is_opcode(expression)
    return PythonObject(spread_dict({
        "opcode": "static",
        "code": expression
    }, **kwargs),
                        debug_reason="code")
Beispiel #25
0
 def test_misc2(self):
     # Came up writing test_misc1
     PythonObject({
         "local": PythonList([ 39, 3 ])
     }, bind=UniversalObjectType({
         "local": UniversalTupleType([ IntegerType(), IntegerType() ])
     }, wildcard_type=RICH_READONLY_TYPE))
def const_string_type():
    # TODO make neater
    return literal_op(
        PythonObject({
            "type": "String",
            "const": True
        },
                     debug_reason="type-literal"))
def prepare_op(function_expression, **kwargs):
    check_is_opcode(function_expression)
    return PythonObject(spread_dict(
        {
            "opcode": "prepare",
            "code": function_expression
        }, **kwargs),
                        debug_reason="code")
def comma_op(*opcodes):
    for v in opcodes:
        check_is_opcode(v)
    return PythonObject({
        "opcode": "comma",
        "opcodes": PythonList(opcodes)
    },
                        debug_reason="code")
Beispiel #29
0
    def test_failed_setup_broad_writing_property(self):
        with self.assertRaises(CompositeTypeIsInconsistent):
            obj = PythonObject({ "foo": "hello" })

            get_manager(obj).add_composite_type(CompositeType({
                ("get", "foo"): GetterMicroOpType("foo", StringType()),
                ("set", "foo"): SetterMicroOpType("foo", AnyType())
            }, name="test"))
Beispiel #30
0
    def test_deletion_blocked(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({
                "bar": UniversalObjectType({
                    "baz": IntegerType()
                })
            })
        )

        with self.assertRaises(Exception):
            del foo.bar