# HG changeset patch # User Paul Boddie # Date 1307485272 -7200 # Node ID 2841f0bcebd4efc1b1f1813e5ca5d7e7011f52c7 # Parent e5e090aaacceb3781a1501835d5f53a4ea7bc232 Added missing support for assignment to unknown objects, along with tests of such assignment. diff -r e5e090aaacce -r 2841f0bcebd4 micropython/__init__.py --- a/micropython/__init__.py Mon Jun 06 00:48:40 2011 +0200 +++ b/micropython/__init__.py Wed Jun 08 00:21:12 2011 +0200 @@ -696,8 +696,21 @@ # Test for assignment. if attrvalues: - for attrvalue in attrvalues: - parent.set(attrname, attrvalue, 0) + + # Instance-related accesses may involve any type + # supporting the attribute. + # NOTE: Here, an instance actually represents any kind + # NOTE: of object. + + if isinstance(parent, micropython.data.Instance): + for attrvalue in attrvalues: + for name in objtable.any_possible_objects([attrname]): + parent = objtable.access(name, name) + if not parent.instance_attributes().has_key(attrname): + parent.set(attrname, attrvalue, 0) + else: + for attrvalue in attrvalues: + parent.set(attrname, attrvalue, 0) # Visit attributes of objects known to be used. diff -r e5e090aaacce -r 2841f0bcebd4 tests/attributes_instance_and_class_assignment_external_unknown.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_instance_and_class_assignment_external_unknown.py Wed Jun 08 00:21:12 2011 +0200 @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +class C: + def __init__(self): + self.a = 0 + +class D: + a = 1 + +def f(obj, value): + obj.a = value + +c = C() +f(c, 123) +f(D, 456) +result_123 = c.a +result_456 = D.a + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r e5e090aaacce -r 2841f0bcebd4 tests/attributes_instance_assignment_external.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_instance_assignment_external.py Wed Jun 08 00:21:12 2011 +0200 @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +class C: + def __init__(self): + self.a = 0 + +c = C() +c.a = 123 +result_123 = c.a + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r e5e090aaacce -r 2841f0bcebd4 tests/attributes_instance_assignment_external_unknown.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_instance_assignment_external_unknown.py Wed Jun 08 00:21:12 2011 +0200 @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +class C: + def __init__(self): + self.a = 0 + +class D: + def __init__(self): + self.a = 1 + +def f(obj, value): + obj.a = value + +c = C() +d = D() +f(c, 123) +f(d, 456) +result_123 = c.a +result_456 = d.a + +# vim: tabstop=4 expandtab shiftwidth=4