From e5145157dbdb9c2683d1ab8176641f19d2dc5d4e Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Fri, 22 Jun 2007 02:50:33 +0000 Subject: Implement iterkeys on top of __iter__ instead of vice versa. Thanks to Brian Harring for the suggestion. (trunk r6918) svn path=/main/branches/2.1.2/; revision=6934 --- pym/cache/anydbm.py | 2 +- pym/cache/flat_hash.py | 2 +- pym/cache/flat_list.py | 2 +- pym/cache/metadata_overlay.py | 8 ++++---- pym/cache/sql_template.py | 2 +- pym/cache/sqlite.py | 2 +- pym/cache/template.py | 12 +++++++++++- pym/cache/util.py | 2 +- pym/cache/volatile.py | 2 +- 9 files changed, 22 insertions(+), 12 deletions(-) (limited to 'pym/cache') diff --git a/pym/cache/anydbm.py b/pym/cache/anydbm.py index a4e0003d4..dbc2bc696 100644 --- a/pym/cache/anydbm.py +++ b/pym/cache/anydbm.py @@ -60,7 +60,7 @@ class database(fs_template.FsBased): def _delitem(self, cpv): del self.__db[cpv] - def iterkeys(self): + def __iter__(self): return iter(self.__db.keys()) def __contains__(self, cpv): diff --git a/pym/cache/flat_hash.py b/pym/cache/flat_hash.py index 48145881c..2fe3eafc1 100644 --- a/pym/cache/flat_hash.py +++ b/pym/cache/flat_hash.py @@ -102,7 +102,7 @@ class database(fs_template.FsBased): return os.path.exists(os.path.join(self.location, cpv)) - def iterkeys(self): + def __iter__(self): """generator for walking the dir struct""" dirs = [self.location] len_base = len(self.location) diff --git a/pym/cache/flat_list.py b/pym/cache/flat_list.py index 85efa4c02..a4ac29b4a 100644 --- a/pym/cache/flat_list.py +++ b/pym/cache/flat_list.py @@ -86,7 +86,7 @@ class database(fs_template.FsBased): return os.path.exists(os.path.join(self._base, cpv)) - def iterkeys(self): + def __iter__(self): """generator for walking the dir struct""" dirs = [self._base] len_base = len(self._base) diff --git a/pym/cache/metadata_overlay.py b/pym/cache/metadata_overlay.py index d82ba96f8..d2a0b27fe 100644 --- a/pym/cache/metadata_overlay.py +++ b/pym/cache/metadata_overlay.py @@ -75,14 +75,14 @@ class database(template.database): return False return True - def iterkeys(self): + def __iter__(self): s = set() - for cpv in self.db_rw.iterkeys(): - if self.has_key(cpv): # validates whiteout when necessary + for cpv in self.db_rw: + if cpv in self: # validates whiteout when necessary yield cpv # set includes whiteouts so they won't be yielded later s.add(cpv) - for cpv in self.db_ro.iterkeys(): + for cpv in self.db_ro: if cpv not in s: yield cpv diff --git a/pym/cache/sql_template.py b/pym/cache/sql_template.py index e635616e9..77f854054 100644 --- a/pym/cache/sql_template.py +++ b/pym/cache/sql_template.py @@ -209,7 +209,7 @@ class SQLDatabase(template.database): return self.con.rowcount > 0 - def iterkeys(self): + def __iter__(self): if not self.autocommits: try: self.commit() except self._BaseError, e: diff --git a/pym/cache/sqlite.py b/pym/cache/sqlite.py index 5c1bfa266..0c3670c2f 100644 --- a/pym/cache/sqlite.py +++ b/pym/cache/sqlite.py @@ -223,7 +223,7 @@ class database(fs_template.FsBased): else: raise cache_errors.CacheCorruption(cpv, "key is not unique") - def iterkeys(self): + def __iter__(self): """generator for walking the dir struct""" cursor = self._db_cursor cursor.execute("SELECT %s FROM %s" % \ diff --git a/pym/cache/template.py b/pym/cache/template.py index d19bb8892..6457f59e5 100644 --- a/pym/cache/template.py +++ b/pym/cache/template.py @@ -95,7 +95,7 @@ class database(object): return tuple(self.iterkeys()) def iterkeys(self): - raise NotImplementedError + return iter(self) def iteritems(self): for x in self.iterkeys(): @@ -123,6 +123,16 @@ class database(object): raise NotImplementedError return self.has_key(cpv) + def __iter__(self): + """This method should always be overridden. It is provided only for + backward compatibility with modules that override iterkeys instead. It + will automatically raise a NotImplementedError if iterkeys has not been + overridden.""" + if self.iterkeys is database.iterkeys: + # prevent a possible recursive loop + raise NotImplementedError(self) + return self.iterkeys() + def get(self, k, x=None): try: return self[k] diff --git a/pym/cache/util.py b/pym/cache/util.py index 0e81a399f..90fcd53dc 100644 --- a/pym/cache/util.py +++ b/pym/cache/util.py @@ -18,7 +18,7 @@ def mirror_cache(valid_nodes_iterable, src_cache, trg_cache, eclass_cache=None, else: noise=verbose_instance - dead_nodes = set(trg_cache.iterkeys()) + dead_nodes = set(trg_cache) count=0 if not trg_cache.autocommits: diff --git a/pym/cache/volatile.py b/pym/cache/volatile.py index 0a204b70f..8d9c72bb3 100644 --- a/pym/cache/volatile.py +++ b/pym/cache/volatile.py @@ -16,7 +16,7 @@ class database(template.database): config.pop("gid", None) super(database, self).__init__(*args, **config) self._data = {} - self.iterkeys = self._data.iterkeys + self.__iter__ = self._data.__iter__ self._delitem = self._data.__delitem__ self.__contains__ = self._data.__contains__ -- cgit v1.2.3-1-g7c22