[PATCH 01/15] Raise exceptions as defined in PEP 3109

Łukasz Żarnowiecki dolohow at outlook.com
Tue May 10 00:18:23 BST 2016


Signed-off-by: Łukasz Żarnowiecki <dolohow at outlook.com>
---
 offlineimap/CustomConfig.py             |  4 ++--
 offlineimap/accounts.py                 |  4 ++--
 offlineimap/folder/Gmail.py             |  8 ++++----
 offlineimap/folder/GmailMaildir.py      |  6 +++---
 offlineimap/folder/IMAP.py              |  7 +++----
 offlineimap/folder/LocalStatus.py       |  4 ++--
 offlineimap/folder/LocalStatusSQLite.py |  4 ++--
 offlineimap/folder/Maildir.py           | 15 +++++++--------
 offlineimap/folder/UIDMaps.py           |  4 ++--
 offlineimap/imaplibutil.py              |  2 +-
 offlineimap/imapserver.py               |  8 ++++----
 offlineimap/repository/IMAP.py          |  5 +++--
 offlineimap/repository/__init__.py      | 10 ++++++----
 13 files changed, 41 insertions(+), 40 deletions(-)

diff --git a/offlineimap/CustomConfig.py b/offlineimap/CustomConfig.py
index 44cfcab..71fa675 100644
--- a/offlineimap/CustomConfig.py
+++ b/offlineimap/CustomConfig.py
@@ -75,8 +75,8 @@ class CustomConfigParser(SafeConfigParser):
             val = self.get(section, option).strip()
             return re.split(separator_re, val)
         except re.error as e:
-            raise Error("Bad split regexp '%s': %s" % \
-              (separator_re, e)), None, exc_info()[2]
+            raise Error("Bad split regexp '%s': %s" %
+              (separator_re, e)).with_traceback(exc_info()[2])
 
     def getdefaultlist(self, section, option, default, separator_re):
         """Same as getlist, but returns the value of `default`
diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py
index c9255c3..a8e8a5e 100644
--- a/offlineimap/accounts.py
+++ b/offlineimap/accounts.py
@@ -225,7 +225,7 @@ class SyncableAccount(Account):
             self._lockfd.close()
             raise OfflineImapError("Could not lock account %s. Is another "
                 "instance using this account?"% self,
-                OfflineImapError.ERROR.REPO), None, exc_info()[2]
+                OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
 
     def _unlock(self):
         """Unlock the account, deleting the lock file"""
@@ -533,7 +533,7 @@ def syncfolder(account, remotefolder, quick):
             raise OfflineImapError("You can set at most one of the "
                 "following: maxage, startdate (for the local folder), "
                 "startdate (for the remote folder)",
-                OfflineImapError.ERROR.REPO), None, exc_info()[2]
+                OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
         if (maxage != None or localstart or remotestart) and quick:
             # IMAP quickchanged isn't compatible with options that
             # involve restricting the messagelist, since the "quick"
diff --git a/offlineimap/folder/Gmail.py b/offlineimap/folder/Gmail.py
index 970d836..2ae9f68 100644
--- a/offlineimap/folder/Gmail.py
+++ b/offlineimap/folder/Gmail.py
@@ -140,10 +140,10 @@ class GmailFolder(IMAPFolder):
               '(FLAGS X-GM-LABELS UID)')
             if res_type != 'OK':
                 raise OfflineImapError("FETCHING UIDs in folder [%s]%s failed. " % \
-                  (self.getrepository(), self) + \
-                  "Server responded '[%s] %s'" % \
-                  (res_type, response), OfflineImapError.ERROR.FOLDER), \
-                  None, exc_info()[2]
+                  (self.getrepository(), self) +
+                  "Server responded '[%s] %s'" %
+                  (res_type, response),
+                  OfflineImapError.ERROR.FOLDER).with_traceback(exc_info()[2])
         finally:
             self.imapserver.releaseconnection(imapobj)
 
diff --git a/offlineimap/folder/GmailMaildir.py b/offlineimap/folder/GmailMaildir.py
index 05f7d79..0e08ad8 100644
--- a/offlineimap/folder/GmailMaildir.py
+++ b/offlineimap/folder/GmailMaildir.py
@@ -170,9 +170,9 @@ class GmailMaildirFolder(MaildirFolder):
         try:
             os.rename(tmppath, filepath)
         except OSError as e:
-            raise OfflineImapError("Can't rename file '%s' to '%s': %s" % \
-              (tmppath, filepath, e[1]), OfflineImapError.ERROR.FOLDER), \
-              None, exc_info()[2]
+            raise OfflineImapError("Can't rename file '%s' to '%s': %s" %
+              (tmppath, filepath, e[1]),
+              OfflineImapError.ERROR.FOLDER).with_traceback(exc_info()[2])
 
         # if utime_from_header=true, we don't want to change the mtime.
         if self.utime_from_header and mtime:
diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index c305334..3c9b6d9 100644
--- a/offlineimap/folder/IMAP.py
+++ b/offlineimap/folder/IMAP.py
@@ -83,7 +83,7 @@ class IMAPFolder(BaseFolder):
         if self.config.getdefault("Account %s"%
                 self.accountname, "maxage", None):
             raise OfflineImapError("maxage is not supported on IMAP-IMAP sync",
-                OfflineImapError.ERROR.REPO), None, exc_info()[2]
+                OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
 
     # Interface from BaseFolder
     def getcopyinstancelimit(self):
@@ -611,8 +611,7 @@ class IMAPFolder(BaseFolder):
                               "repository '%s' failed (abort). Server responded: %s\n"
                               "Message content was: %s"%
                               (msg_id, self, self.getrepository(), str(e), dbg_output),
-                                               OfflineImapError.ERROR.MESSAGE), \
-                              None, exc_info()[2]
+                              OfflineImapError.ERROR.MESSAGE).with_traceback(exc_info()[2])
                     # XXX: is this still needed?
                     self.ui.error(e, exc_info()[2])
                 except imapobj.error as e: # APPEND failed
@@ -624,7 +623,7 @@ class IMAPFolder(BaseFolder):
                     raise OfflineImapError("Saving msg (%s) folder '%s', repo '%s'"
                         "failed (error). Server responded: %s\nMessage content was: "
                         "%s" % (msg_id, self, self.getrepository(), str(e), dbg_output),
-                            OfflineImapError.ERROR.MESSAGE), None, exc_info()[2]
+                            OfflineImapError.ERROR.MESSAGE).with_traceback(exc_info()[2])
             # Checkpoint. Let it write out stuff, etc. Eg searches for
             # just uploaded messages won't work if we don't do this.
             (typ,dat) = imapobj.check()
diff --git a/offlineimap/folder/LocalStatus.py b/offlineimap/folder/LocalStatus.py
index d9b9b70..6028dbd 100644
--- a/offlineimap/folder/LocalStatus.py
+++ b/offlineimap/folder/LocalStatus.py
@@ -74,7 +74,7 @@ class LocalStatusFolder(BaseFolder):
                 errstr = "Corrupt line '%s' in cache file '%s'" % \
                     (line, self.filename)
                 self.ui.warn(errstr)
-                raise ValueError(errstr), None, exc_info()[2]
+                raise ValueError(errstr).with_traceback(exc_info()[2])
             self.messagelist[uid] = self.msglist_item_initializer(uid)
             self.messagelist[uid]['flags'] = flags
 
@@ -97,7 +97,7 @@ class LocalStatusFolder(BaseFolder):
                 errstr = "Corrupt line '%s' in cache file '%s'"% \
                     (line, self.filename)
                 self.ui.warn(errstr)
-                raise ValueError(errstr), None, exc_info()[2]
+                raise ValueError(errstr).with_traceback(exc_info()[2])
             self.messagelist[uid] = self.msglist_item_initializer(uid)
             self.messagelist[uid]['flags'] = flags
             self.messagelist[uid]['mtime'] = mtime
diff --git a/offlineimap/folder/LocalStatusSQLite.py b/offlineimap/folder/LocalStatusSQLite.py
index 306ca18..d7d8472 100644
--- a/offlineimap/folder/LocalStatusSQLite.py
+++ b/offlineimap/folder/LocalStatusSQLite.py
@@ -68,13 +68,13 @@ class LocalStatusSQLiteFolder(BaseFolder):
             # sqlite import had failed.
             raise UserWarning("SQLite backend chosen, but cannot connect "
                 "with available bindings to '%s'. Is the sqlite3 package "
-                "installed?."% self.filename), None, exc_info()[2]
+                "installed?."% self.filename).with_traceback(exc_info()[2])
         except sqlite.OperationalError as e:
             # Operation had failed.
             raise UserWarning("cannot open database file '%s': %s.\nYou might "
                 "want to check the rights to that file and if it cleanly opens "
                 "with the 'sqlite<3>' command."%
-                (self.filename, e)), None, exc_info()[2]
+                (self.filename, e)).with_traceback(exc_info()[2])
 
         # Make sure sqlite is in multithreading SERIALIZE mode.
         assert sqlite.threadsafety == 1, 'Your sqlite is not multithreading safe.'
diff --git a/offlineimap/folder/Maildir.py b/offlineimap/folder/Maildir.py
index d97b0d3..bda452a 100644
--- a/offlineimap/folder/Maildir.py
+++ b/offlineimap/folder/Maildir.py
@@ -305,7 +305,7 @@ class MaildirFolder(BaseFolder):
                         continue
                     severity = OfflineImapError.ERROR.MESSAGE
                     raise OfflineImapError("Unique filename %s already exists."%
-                        filename, severity), None, exc_info()[2]
+                        filename, severity).with_traceback(exc_info()[2])
                 else:
                     raise
 
@@ -425,10 +425,9 @@ class MaildirFolder(BaseFolder):
                 os.rename(os.path.join(self.getfullname(), oldfilename),
                           os.path.join(self.getfullname(), newfilename))
             except OSError as e:
-                raise OfflineImapError("Can't rename file '%s' to '%s': %s" % (
-                                       oldfilename, newfilename, e[1]),
-                                       OfflineImapError.ERROR.FOLDER), \
-                      None, exc_info()[2]
+                raise OfflineImapError("Can't rename file '%s' to '%s': %s" %
+                    (oldfilename, newfilename, e[1]),
+                    OfflineImapError.ERROR.FOLDER).with_traceback(exc_info()[2])
 
             self.messagelist[uid]['flags'] = flags
             self.messagelist[uid]['filename'] = newfilename
@@ -507,9 +506,9 @@ class MaildirFolder(BaseFolder):
                         os.rename(filename, newfilename)
                     except OSError as e:
                         raise OfflineImapError(
-                            "Can't rename file '%s' to '%s': %s" % (
-                                filename, newfilename, e[1]),
-                            OfflineImapError.ERROR.FOLDER), None, exc_info()[2]
+                            "Can't rename file '%s' to '%s': %s" %
+                            (filename, newfilename, e[1]),
+                            OfflineImapError.ERROR.FOLDER).with_traceback(exc_info()[2])
             elif match.group(1) != self._foldermd5:
                 self.ui.warn(("Inconsistent FMD5 for file `%s':"
                               " Neither `%s' nor `%s' found")
diff --git a/offlineimap/folder/UIDMaps.py b/offlineimap/folder/UIDMaps.py
index 1e54f2b..d30d83e 100644
--- a/offlineimap/folder/UIDMaps.py
+++ b/offlineimap/folder/UIDMaps.py
@@ -62,7 +62,7 @@ class MappedIMAPFolder(IMAPFolder):
                     line = line.strip()
                 except ValueError:
                     raise Exception("Corrupt line '%s' in UID mapping file '%s'"%
-                        (line, mapfilename)), None, exc_info()[2]
+                        (line, mapfilename)).with_traceback(exc_info()[2])
                 (str1, str2) = line.split(':')
                 loc = long(str1)
                 rem = long(str2)
@@ -91,7 +91,7 @@ class MappedIMAPFolder(IMAPFolder):
             raise OfflineImapError("Could not find UID for msg '{0}' (f:'{1}'."
                 " This is usually a bad thing and should be reported on the ma"
                 "iling list.".format(e.args[0], self),
-                OfflineImapError.ERROR.MESSAGE), None, exc_info()[2]
+                OfflineImapError.ERROR.MESSAGE).with_traceback(exc_info()[2])
 
     # Interface from BaseFolder
     def cachemessagelist(self, min_date=None, min_uid=None):
diff --git a/offlineimap/imaplibutil.py b/offlineimap/imaplibutil.py
index 9b1095b..c16cf28 100644
--- a/offlineimap/imaplibutil.py
+++ b/offlineimap/imaplibutil.py
@@ -56,7 +56,7 @@ class UsefulIMAPMixIn(object):
             errstr = "Server '%s' closed connection, error on SELECT '%s'. Ser"\
                 "ver said: %s" % (self.host, mailbox, e.args[0])
             severity = OfflineImapError.ERROR.FOLDER_RETRY
-            raise OfflineImapError(errstr, severity), None, exc_info()[2]
+            raise OfflineImapError(errstr, severity).with_traceback(exc_info()[2])
         if result[0] != 'OK':
             #in case of error, bail out with OfflineImapError
             errstr = "Error SELECTing mailbox '%s', server reply:\n%s" %\
diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index 7e4ed85..ce049e3 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -571,7 +571,7 @@ class IMAPServer:
                          "'%s'. Make sure you have configured the ser"\
                          "ver name correctly and that you are online."%\
                          (self.hostname, self.repos)
-                raise OfflineImapError(reason, severity), None, exc_info()[2]
+                raise OfflineImapError(reason, severity).with_traceback(exc_info()[2])
 
             elif isinstance(e, SSLError) and e.errno == errno.EPERM:
                 # SSL unknown protocol error
@@ -584,7 +584,7 @@ class IMAPServer:
                     reason = "Unknown SSL protocol connecting to host '%s' for "\
                          "repository '%s'. OpenSSL responded:\n%s"\
                          % (self.hostname, self.repos, e)
-                raise OfflineImapError(reason, severity), None, exc_info()[2]
+                raise OfflineImapError(reason, severity).with_traceback(exc_info()[2])
 
             elif isinstance(e, socket.error) and e.args[0] == errno.ECONNREFUSED:
                 # "Connection refused", can be a non-existing port, or an unauthorized
@@ -593,14 +593,14 @@ class IMAPServer:
                     "refused. Make sure you have the right host and port "\
                     "configured and that you are actually able to access the "\
                     "network."% (self.hostname, self.port, self.repos)
-                raise OfflineImapError(reason, severity), None, exc_info()[2]
+                raise OfflineImapError(reason, severity).with_traceback(exc_info()[2])
             # Could not acquire connection to the remote;
             # socket.error(last_error) raised
             if str(e)[:24] == "can't open socket; error":
                 raise OfflineImapError("Could not connect to remote server '%s' "\
                     "for repository '%s'. Remote does not answer."
                     % (self.hostname, self.repos),
-                    OfflineImapError.ERROR.REPO), None, exc_info()[2]
+                    OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
             else:
                 # re-raise all other errors
                 raise
diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py
index eeaa5de..a76aadd 100644
--- a/offlineimap/repository/IMAP.py
+++ b/offlineimap/repository/IMAP.py
@@ -108,8 +108,9 @@ class IMAPRepository(BaseRepository):
                 host = self.localeval.eval(host)
             except Exception as e:
                 raise OfflineImapError("remotehosteval option for repository "
-                    "'%s' failed:\n%s"% (self, e), OfflineImapError.ERROR.REPO), \
-                    None, exc_info()[2]
+                    "'%s' failed:\n%s"%
+                    (self, e),
+                    OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
             if host:
                 self._host = host
                 return self._host
diff --git a/offlineimap/repository/__init__.py b/offlineimap/repository/__init__.py
index 0fbbc13..8db17bc 100644
--- a/offlineimap/repository/__init__.py
+++ b/offlineimap/repository/__init__.py
@@ -68,16 +68,18 @@ class Repository(object):
         except NoSectionError as e:
             errstr = ("Could not find section '%s' in configuration. Required "
                       "for account '%s'." % ('Repository %s' % name, account))
-            raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO), \
-                None, exc_info()[2]
+            raise OfflineImapError(
+                errstr,
+                OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
 
         try:
             repo = typemap[repostype]
         except KeyError:
             errstr = "'%s' repository not supported for '%s' repositories."% \
                 (repostype, reqtype)
-            raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO), \
-                None, exc_info()[2]
+            raise OfflineImapError(
+                errstr,
+                OfflineImapError.ERROR.REPO).with_traceback(exc_info()[2])
 
         return repo(name, account)
 
-- 
2.8.2





More information about the OfflineIMAP-project mailing list