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)
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() }))
def test_double_deep_assignment(self): foo = PythonObject({ "bar": PythonObject({ "baz": PythonObject({ "bam": 10 }) }) }) Baz = UniversalObjectType({ "bam": IntegerType() }) Bar = UniversalObjectType({ "baz": Baz }) get_manager(foo).add_composite_type( UniversalObjectType({ "bar": Bar }) ) self.assertEquals(foo.bar.baz.bam, 10) foo.bar = PythonObject({ "baz": PythonObject({ "bam": 42 }) }, bind=Bar) self.assertEquals(foo.bar.baz.bam, 42)
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)
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")
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))
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
def test_changes_blocked_without_micro_ops(self): foo = PythonObject({ "bar": PythonObject({ "baz": 42 }) }) get_manager(foo) with self.assertRaises(Exception): foo.bar = "hello"
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"))
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)
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"
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")
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")
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)
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)
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))
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)
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 static_op(expression, **kwargs): check_is_opcode(expression) return PythonObject(spread_dict({ "opcode": "static", "code": expression }, **kwargs), debug_reason="code")
def loop_op(opcode, **kwargs): check_is_opcode(opcode) return PythonObject(spread_dict({ "opcode": "loop", "code": opcode }, **kwargs), debug_reason="code")
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")
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"))
def test_basics(self): foo = PythonObject({ "bar": 42 }) get_manager(foo).add_composite_type(UniversalObjectType({ "bar": UnitType(42) })) self.assertEquals(foo.bar, 42)
def test_blocked_basic_assignment(self): foo = PythonObject({ "bar": PythonObject({ "baz": 42 }) }) get_manager(foo).add_composite_type( UniversalObjectType({ "bar": UniversalObjectType({ "baz": IntegerType() }) }) ) with self.assertRaises(Exception): foo.bar = PythonObject({ "baz": "hello" })
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
def test_dereference_argument_parameter(self): code = parse(""" function(|Object { foo: int }|) { return foo; } """) _, result = bootstrap_function(code, argument=PythonObject({"foo": 42})) self.assertEquals(result.caught_break_mode, "value") self.assertEquals(result.value, 42)
def unbound_dereference(name, **kwargs): if not isinstance(name, basestring): raise FatalError() return PythonObject(spread_dict( { "opcode": "unbound_dereference", "reference": name }, **kwargs), debug_reason="code")