From: Zac Medico Date: Tue, 27 Jul 2010 05:47:23 +0000 (-0700) Subject: Fix ExtendedAtomDict so get() and __getitem__() behave consistently, X-Git-Tag: v2.2_rc68~450 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ed854d3de2fce3b5e78a67afc18763901aa66272;p=portage.git Fix ExtendedAtomDict so get() and __getitem__() behave consistently, since otherwise it can be quite confusing. --- diff --git a/pym/portage/dep/__init__.py b/pym/portage/dep/__init__.py index 25cad8fd8..12e32c36d 100644 --- a/pym/portage/dep/__init__.py +++ b/pym/portage/dep/__init__.py @@ -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() diff --git a/pym/portage/tests/dep/testExtendedAtomDict.py b/pym/portage/tests/dep/testExtendedAtomDict.py index 702bec196..69d092e38 100644 --- a/pym/portage/tests/dep/testExtendedAtomDict.py +++ b/pym/portage/tests/dep/testExtendedAtomDict.py @@ -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'})