Implement iterkeys on top of __iter__ instead of vice versa. Thanks to Brian Harring...
authorZac Medico <zmedico@gentoo.org>
Fri, 22 Jun 2007 02:50:33 +0000 (02:50 -0000)
committerZac Medico <zmedico@gentoo.org>
Fri, 22 Jun 2007 02:50:33 +0000 (02:50 -0000)
svn path=/main/branches/2.1.2/; revision=6934

pym/cache/anydbm.py
pym/cache/flat_hash.py
pym/cache/flat_list.py
pym/cache/metadata_overlay.py
pym/cache/sql_template.py
pym/cache/sqlite.py
pym/cache/template.py
pym/cache/util.py
pym/cache/volatile.py

index a4e0003d423f463d9220cae8946d49198f749b7b..dbc2bc6961b0ba8045943828614a37c13e7f4918 100644 (file)
@@ -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):
index 48145881c9e5ab219d842cc7bc10be02a697b32f..2fe3eafc1e26ebc6ec078fe2eeb7ab0e867ef60f 100644 (file)
@@ -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)
index 85efa4c0214cc7ac56bfd44702be3b282cd126d2..a4ac29b4a40ef0252ac705812d92b211915f2816 100644 (file)
@@ -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)
index d82ba96f8b7542c889a5fab0ac6951aad1f08b76..d2a0b27fe3f05a8556a70036d9ec1265375c4b0e 100644 (file)
@@ -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
 
index e635616e913d09ae2ef4b015924579bc01cbb0c6..77f854054340bd8ec18e3eb99593739f94951808 100644 (file)
@@ -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:
index 5c1bfa26655ef478d9d3e923c04c6c03927f14a1..0c3670c2f8a3913840b21dd5df7a7a196814e22b 100644 (file)
@@ -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" % \
index d19bb88928b720dc8f6145e3103055e6b06e5927..6457f59e505c42035177491a710dbbffc2005b23 100644 (file)
@@ -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]
index 0e81a399f6d64bd0b46d06eef69c38e90df2bc77..90fcd53dc485dd76bb4d594e5cb7cd874c5c8c6c 100644 (file)
@@ -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:
index 0a204b70fb34ed15c26669311b47489e4d61045a..8d9c72bb391266b8169a399735d6b36d1b7348b3 100644 (file)
@@ -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__