[PATCH] Replace dictionary iteration methods

Łukasz Żarnowiecki dolohow at outlook.com
Mon May 16 18:48:50 BST 2016


Signed-off-by: Łukasz Żarnowiecki <dolohow at outlook.com>
---

This patch differs from the previous one that it does not blindly wrap
all dictionaries with "list" while looping.  This is only necessary when
the dictionary will be later modified inside of this loop.  Python3 will
will rise a RuntimeError exception.

 offlineimap/folder/GmailMaildir.py | 6 +++---
 offlineimap/folder/Maildir.py      | 4 ++--
 offlineimap/folder/UIDMaps.py      | 4 ++--
 offlineimap/init.py                | 2 +-
 offlineimap/repository/Base.py     | 4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/offlineimap/folder/GmailMaildir.py b/offlineimap/folder/GmailMaildir.py
index 125e440..c27c027 100644
--- a/offlineimap/folder/GmailMaildir.py
+++ b/offlineimap/folder/GmailMaildir.py
@@ -48,11 +48,11 @@ class GmailMaildirFolder(MaildirFolder):
                 sorted(statusfolder.getmessageuidlist()):
             return True
         # check for flag changes, it's quick on a Maildir
-        for (uid, message) in self.getmessagelist().iteritems():
+        for (uid, message) in self.getmessagelist().items():
             if message['flags'] != statusfolder.getmessageflags(uid):
                 return True
         # check for newer mtimes. it is also fast
-        for (uid, message) in self.getmessagelist().iteritems():
+        for (uid, message) in self.getmessagelist().items():
             if message['mtime'] > statusfolder.getmessagemtime(uid):
                 return True
         return False  #Nope, nothing changed
@@ -70,7 +70,7 @@ class GmailMaildirFolder(MaildirFolder):
 
         # Get mtimes
         if self.synclabels:
-            for uid, msg in self.messagelist.items():
+            for uid, msg in list(self.messagelist.items()):
                 filepath = os.path.join(self.getfullname(), msg['filename'])
                 msg['mtime'] = int(os.stat(filepath).st_mtime)
 
diff --git a/offlineimap/folder/Maildir.py b/offlineimap/folder/Maildir.py
index 173bd74..bf40194 100644
--- a/offlineimap/folder/Maildir.py
+++ b/offlineimap/folder/Maildir.py
@@ -230,7 +230,7 @@ class MaildirFolder(BaseFolder):
                 sorted(statusfolder.getmessageuidlist()):
             return True
         # Also check for flag changes, it's quick on a Maildir.
-        for (uid, message) in self.getmessagelist().iteritems():
+        for (uid, message) in self.getmessagelist().items():
             if message['flags'] != statusfolder.getmessageflags(uid):
                 return True
         return False  # Nope, nothing changed.
@@ -495,7 +495,7 @@ class MaildirFolder(BaseFolder):
         """
         oldfmd5 = md5(self.name).hexdigest()
         msglist = self._scanfolder()
-        for mkey, mvalue in msglist.iteritems():
+        for mkey, mvalue in msglist.items():
             filename = os.path.join(self.getfullname(), mvalue['filename'])
             match = re.search("FMD5=([a-fA-F0-9]+)", filename)
             if match is None:
diff --git a/offlineimap/folder/UIDMaps.py b/offlineimap/folder/UIDMaps.py
index 8848e9a..67f220c 100644
--- a/offlineimap/folder/UIDMaps.py
+++ b/offlineimap/folder/UIDMaps.py
@@ -77,7 +77,7 @@ class MappedIMAPFolder(IMAPFolder):
         if dolock: self.maplock.acquire()
         try:
             file = open(mapfilename + ".tmp", 'wt')
-            for (key, value) in self.diskl2r.iteritems():
+            for (key, value) in self.diskl2r.items():
                 file.write("%d:%d\n"% (key, value))
             file.close()
             os.rename(mapfilename + '.tmp', mapfilename)
@@ -162,7 +162,7 @@ class MappedIMAPFolder(IMAPFolder):
         localhash = self._mb.getmessagelist()
         self.maplock.acquire()
         try:
-            for key, value in localhash.items():
+            for key, value in list(localhash.items()):
                 try:
                     key = self.l2r[key]
                 except KeyError:
diff --git a/offlineimap/init.py b/offlineimap/init.py
index c813fd5..8fdbde7 100644
--- a/offlineimap/init.py
+++ b/offlineimap/init.py
@@ -291,7 +291,7 @@ class OfflineImap:
             d = collections.defaultdict(lambda: 0)
             for v in l:
                 d[tuple(v)] += 1
-            return list((k, v) for k, v in d.iteritems())
+            return list((k, v) for k, v in d.items())
 
         stack_displays = []
         for threadId, stack in sys._current_frames().items():
diff --git a/offlineimap/repository/Base.py b/offlineimap/repository/Base.py
index e3f1cc9..8e714ca 100644
--- a/offlineimap/repository/Base.py
+++ b/offlineimap/repository/Base.py
@@ -190,7 +190,7 @@ class BaseRepository(CustomConfig.ConfigHelperMixin, object):
                     dst_repo.getsep(), src_repo.getsep())] = folder
 
         # Find and create new folders on src_repo.
-        for src_name_t, src_folder in src_hash.iteritems():
+        for src_name_t, src_folder in src_hash.items():
             # Don't create on dst_repo, if it is readonly.
             if not dst_repo.get_create_folders():
                 break
@@ -207,7 +207,7 @@ class BaseRepository(CustomConfig.ConfigHelperMixin, object):
                 status_repo.makefolder(src_name_t.replace(dst_repo.getsep(),
                                                    status_repo.getsep()))
         # Find and create new folders on dst_repo.
-        for dst_name_t, dst_folder in dst_hash.iteritems():
+        for dst_name_t, dst_folder in dst_hash.items():
             if not src_repo.get_create_folders():
                 # Don't create missing folder on readonly repo.
                 break
-- 
2.8.2





More information about the OfflineIMAP-project mailing list