Implement iterkeys on top of __iter__ instead of vice versa. Thanks to Brian Harring...
authorZac Medico <zmedico@gentoo.org>
Thu, 21 Jun 2007 21:10:00 +0000 (21:10 -0000)
committerZac Medico <zmedico@gentoo.org>
Thu, 21 Jun 2007 21:10:00 +0000 (21:10 -0000)
svn path=/main/trunk/; revision=6918

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

index 8d72e40ca9f474e7b01cca9fa3bfe375bfe59763..47b8ac1bd4740305bd25788b0a18ff4931469227 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 05b900ea86fd3dfd5636d9f7ed06fa6851db6689..80b23fe4d149cdac267dca59d45dc5c1f351afa2 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 6dbbba96bcc991daad8a272fef1465c0c6e54ba0..d3e480f96e83bb2d23c9923bacb6a9dcc61aea81 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 d1fcda1d1a3d73422357afd870b40035e551da91..2aaa7e07cefc14a9c5de52d992ac8109425ab153 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 4ef3df421e5c620ba4e36745c53ea18fe86fa23f..f32a7284447bb6855f14f02d146b207db5f42d28 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 1cab499787b03ee65721a2c6f63b4a3f385e8637..dae9d4c1d902b16d933030c5c506007cf754c519 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 161b07f55d0cf8be4ecff1303f18f4ff2145810a..d5a07528c6766ce05d03c685951687e5ad81d43e 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 4f0d563d293d49c8c946c69f284a42e3675dd735..3c6bdb70f28ed893689bf2484f4334af64622723 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 555eafc5a107af52d728f55770b253700c8c0335..12c23dd5639779e3f599684bfbb132642d0c4dfb 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__