def test_write(): o = CustomMutable() o2 = CustomObject() polyglot.__write__(o, "field", 32) assert o.field == 32 polyglot.__write__(o, "__getattribute__", 321) assert o.__getattribute__ == 321 polyglot.__write__(o, "grrrr", 42) assert hasattr(o, "grrrr") polyglot.__write__(o2, "grrrr", 42) assert o2.grrrr == 42 try: non_string = bytearray(b"a fine non-string object we have here") polyglot.__write__(o, non_string, 12) except AttributeError: assert True else: assert False
def test_write(): o = CustomMutable() o2 = CustomObject() polyglot.__write__(o, "field", 32) assert o.field == 42 assert o["field"] == 32 polyglot.__write__(o, "[field", 42) assert o.field == 42 assert o["field"] == 42 polyglot.__write__(o, "@field", 32) assert o.field == 32 assert o["field"] == 42 polyglot.__write__(o, "__getattribute__", 321) assert o["__getattribute__"] == 321 assert o.__getattribute__ != 321 polyglot.__write__(o, "grrrr", 42) assert not hasattr(o, "grrrr") assert o["grrrr"] == 42 polyglot.__write__(o, "@grrrr", 42) assert o.grrrr == 42 polyglot.__write__(o2, "grrrr", 42) assert o2.grrrr == 42 non_string = bytearray(b"a fine non-string object we have here") polyglot.__write__(o, non_string, 12) assert not hasattr(o, non_string) assert o[non_string] == 12 polyglot.__write__(o2, non_string, 12) assert getattr(o2, non_string) == 12