# HG changeset patch # User Paul Boddie # Date 1330381353 -3600 # Node ID a898fc7c3998d11ef31f5b685a924c5584794624 # Parent 43d594f9c4367c107ee5cabe6493fdfe662364ee Changed the combination of object set lists to discard empty lists. This should mean that where no attribute usage is defined for a name, the resulting combined object set does not retain the "null usage", thus preventing "bubbles" of absent usage from forming and being incorporated into attribute usage deductions. diff -r 43d594f9c436 -r a898fc7c3998 internal_tests/objectset.py --- a/internal_tests/objectset.py Mon Feb 27 23:17:50 2012 +0100 +++ b/internal_tests/objectset.py Mon Feb 27 23:22:33 2012 +0100 @@ -2,12 +2,16 @@ from micropython.common import ObjectSet, combine_mapping_dicts +# Test storage. + o1 = ObjectSet() o1.add("a") o1.add("b") o1.add("c") print "o1 =", o1 +# Test storage with associated data. + o2 = ObjectSet() o2.add("b") o2["c"] = ["x"] @@ -15,9 +19,11 @@ o2["d"] += ["p", "q"] print "o2 =", o2 -assert o1 != o2 print "o1 == o2", o1 == o2 print "o1 != o2", o1 != o2 +assert o1 != o2 + +# Test merging. o12 = o1.merge(o2) print "o1 =", o1 @@ -26,37 +32,47 @@ o12_keys = o12.keys() o12_keys.sort() +print "o12_keys =", o12_keys assert o12_keys == ["a", "b", "c", "d"] -print "o12_keys =", o12_keys + +# Test copying. o1_copy = ObjectSet(o1) -assert o1 == o1_copy print "o1 =", o1 print "o1_copy =", o1_copy print "o1 == o1_copy", o1 == o1_copy +assert o1 == o1_copy + +# Test updating. o1_copy.update(o2) -assert o1 != o1_copy -assert o12 == o1_copy print "o1_copy =", o1_copy print "o1 != o1_copy", o1 != o1_copy print "o12 == o1_copy", o12 == o1_copy +assert o1 != o1_copy +assert o12 == o1_copy o3 = ObjectSet() o3.add("c") o3["c"].append("y") print "o3 =", o3 +# Test combining. + d1 = {'a' : [ObjectSet(['f', 'g']), ObjectSet(['f', 'h'])]} d2 = {'a' : [ObjectSet(['f']), ObjectSet(['e', 'f', 'g'])]} d3 = combine_mapping_dicts(d1, d2) print "d1 =", d1 print "d2 =", d2 print "d3 =", d3 +assert d3["a"] == [ObjectSet(['f', 'g']), ObjectSet(['f', 'h']), ObjectSet(['e', 'f', 'g']), ObjectSet(['e', 'f', 'g', 'h'])] + +# Test combining with empty sets. d4 = {'a' : None} d5 = combine_mapping_dicts(d1, d4) print "d4 =", d4 print "d5 =", d5 +assert d5 == d1 # vim: tabstop=4 expandtab shiftwidth=4 diff -r 43d594f9c436 -r a898fc7c3998 micropython/common.py --- a/micropython/common.py Mon Feb 27 23:17:50 2012 +0100 +++ b/micropython/common.py Mon Feb 27 23:22:33 2012 +0100 @@ -3,7 +3,7 @@ """ Common classes. -Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie +Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -76,7 +76,10 @@ return hash(tuple(self.keys())) def __eq__(self, other): - return self.objects == other.objects + if hasattr(other, "objects"): + return self.objects == other.objects + else: + return set(self.objects.keys()) == set(other) # Set methods. @@ -228,13 +231,16 @@ members. """ + # If either list is undefined (indicated by None), return the defined list, + # or return None if neither is defined. + if l1 is None: if l2 is None: return None else: - return l2 + [None] + return l2 + [] elif l2 is None: - return l1 + [None] + return l1 + [] combined = ObjectSet() for i1 in l1: diff -r 43d594f9c436 -r a898fc7c3998 tests/attributes_instance_assignment.py --- a/tests/attributes_instance_assignment.py Mon Feb 27 23:17:50 2012 +0100 +++ b/tests/attributes_instance_assignment.py Mon Feb 27 23:22:33 2012 +0100 @@ -15,9 +15,17 @@ pass a.x = 3 +def g(a): # {{z}, {x, z}} + if a.z: + a.x # {x, z} + # {z} (else) + a.z = 4 + c = C(1) -d = D(2) +d = D(0) f(c) +g(d) result_3 = c.x +result_4 = d.z # vim: tabstop=4 expandtab shiftwidth=4 diff -r 43d594f9c436 -r a898fc7c3998 tests/attributes_instance_assignment_on_self.py --- a/tests/attributes_instance_assignment_on_self.py Mon Feb 27 23:17:50 2012 +0100 +++ b/tests/attributes_instance_assignment_on_self.py Mon Feb 27 23:22:33 2012 +0100 @@ -15,9 +15,16 @@ self.y = y self.z = None + def g(self, a): + if self.z: + self.x + self.z = a + c = C(1) d = D(2) c.f(3) +d.g(4) result_3 = c.x +result_4 = d.z # vim: tabstop=4 expandtab shiftwidth=4