Fix ExtendedAtomDict so get() and __getitem__() behave consistently,
authorZac Medico <zmedico@gentoo.org>
Tue, 27 Jul 2010 05:47:23 +0000 (22:47 -0700)
committerZac Medico <zmedico@gentoo.org>
Tue, 27 Jul 2010 05:47:23 +0000 (22:47 -0700)
since otherwise it can be quite confusing.

pym/portage/dep/__init__.py
pym/portage/tests/dep/testExtendedAtomDict.py

index 25cad8fd8be35dff2e7c06eb921c6b47d99e9997..12e32c36d8bddc460bf188046f638e50c2324351 100644 (file)
@@ -791,10 +791,17 @@ class ExtendedAtomDict(portage.cache.mappings.MutableMapping):
                else:
                        return self._normal.setdefault(cp, default)
 
-       def get(self, cp):
+       def __getitem__(self, cp):
+
+               if not isinstance(cp, basestring):
+                       raise KeyError(cp)
+
                ret = self._value_class()
                normal_match = self._normal.get(cp)
+               match = False
+
                if normal_match is not None:
+                       match = True
                        if hasattr(ret, "update"):
                                ret.update(normal_match)
                        elif hasattr(ret, "extend"):
@@ -802,14 +809,29 @@ class ExtendedAtomDict(portage.cache.mappings.MutableMapping):
                        else:
                                raise NotImplementedError()
 
-               for extended_cp in self._extended:
-                       if extended_cp_match(extended_cp, cp):
+               if '*' in cp:
+                       v = self._extended.get(cp)
+                       if v is not None:
+                               match = True
                                if hasattr(ret, "update"):
-                                       ret.update(self._extended[extended_cp])
+                                       ret.update(v)
                                elif hasattr(ret, "extend"):
-                                       ret.extend(self._extended[extended_cp])
+                                       ret.extend(v)
                                else:
                                        raise NotImplementedError()
+               else:
+                       for extended_cp in self._extended:
+                               if extended_cp_match(extended_cp, cp):
+                                       match = True
+                                       if hasattr(ret, "update"):
+                                               ret.update(self._extended[extended_cp])
+                                       elif hasattr(ret, "extend"):
+                                               ret.extend(self._extended[extended_cp])
+                                       else:
+                                               raise NotImplementedError()
+
+               if not match:
+                       raise KeyError(cp)
 
                return ret
 
@@ -819,14 +841,6 @@ class ExtendedAtomDict(portage.cache.mappings.MutableMapping):
                else:
                        self._normal[cp] = val
 
-       def __getitem__(self, cp):
-               if not isinstance(cp, basestring):
-                       raise KeyError(cp)
-               if "*" in cp:
-                       return self._extended[cp]
-               else:
-                       return self._normal[cp]
-
        def clear(self):
                self._extended.clear()
                self._normal.clear()
index 702bec19616a81203073d11dd5b1bc11093138db..69d092e386ce237a9a089ef592940df02f65799e 100644 (file)
@@ -15,4 +15,4 @@ class TestExtendedAtomDict(TestCase):
                self.assertEqual(d.get("dev-libs/A"), { "test1": "x", "test2": "y" })
                self.assertEqual(d.get("sys-apps/portage"), { "test1": "x", "test3": "z" })
                self.assertEqual(d["dev-libs/*"], { "test2": "y" })
-               self.assertEqual(d["sys-apps/portage"], { "test3": "z" })
+               self.assertEqual(d["sys-apps/portage"], {'test1': 'x', 'test3': 'z'})