py3: raise exceptions using six module

There is no other way to make Python2 and Python3 happy, because syntax
raise E, V, T is incompatible with the latter.

Signed-off-by: Łukasz Żarnowiecki <dolohow@outlook.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Łukasz Żarnowiecki
2016-05-17 19:56:52 +02:00
committed by Nicolas Sebrecht
parent 83539f8601
commit 0addcbabf0
13 changed files with 70 additions and 49 deletions

View File

@ -24,6 +24,8 @@ from offlineimap import imaplibutil
import offlineimap.accounts
from .IMAP import IMAPFolder
import six
"""Folder implementation to support features of the Gmail IMAP server."""
class GmailFolder(IMAPFolder):
@ -139,11 +141,10 @@ class GmailFolder(IMAPFolder):
res_type, response = imapobj.fetch("'%s'"% msgsToFetch,
'(FLAGS X-GM-LABELS UID)')
if res_type != 'OK':
raise OfflineImapError("FETCHING UIDs in folder [%s]%s failed. " % \
six.reraise(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]
(res_type, response), OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
finally:
self.imapserver.releaseconnection(imapobj)

View File

@ -23,6 +23,8 @@ from offlineimap import OfflineImapError
import offlineimap.accounts
from offlineimap import imaputil
import six
class GmailMaildirFolder(MaildirFolder):
"""Folder implementation to support adding labels to messages in a Maildir.
"""
@ -170,9 +172,8 @@ 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]
six.reraise(OfflineImapError("Can't rename file '%s' to '%s': %s" % \
(tmppath, filepath, e[1]), OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
# if utime_from_header=true, we don't want to change the mtime.
if self.utime_from_header and mtime:

View File

@ -27,6 +27,8 @@ from offlineimap import imaputil, imaplibutil, emailutil, OfflineImapError
from offlineimap import globals
from offlineimap.imaplib2 import MonthNames
import six
# Globals
CRLF = '\r\n'
@ -82,8 +84,8 @@ class IMAPFolder(BaseFolder):
def getmaxage(self):
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]
six.reraise(OfflineImapError("maxage is not supported on IMAP-IMAP sync",
OfflineImapError.ERROR.REPO), None, exc_info()[2])
# Interface from BaseFolder
def getcopyinstancelimit(self):
@ -607,12 +609,11 @@ class IMAPFolder(BaseFolder):
self.imapserver.releaseconnection(imapobj, True)
imapobj = self.imapserver.acquireconnection()
if not retry_left:
raise OfflineImapError("Saving msg (%s) in folder '%s', "
six.reraise(OfflineImapError("Saving msg (%s) in folder '%s', "
"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), None, exc_info()[2])
# XXX: is this still needed?
self.ui.error(e, exc_info()[2])
except imapobj.error as e: # APPEND failed
@ -621,10 +622,10 @@ class IMAPFolder(BaseFolder):
# drop conn, it might be bad.
self.imapserver.releaseconnection(imapobj, True)
imapobj = None
raise OfflineImapError("Saving msg (%s) folder '%s', repo '%s'"
six.reraise(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), None, 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()

View File

@ -21,6 +21,8 @@ import threading
from .Base import BaseFolder
import six
class LocalStatusFolder(BaseFolder):
"""LocalStatus backend implemented as a plain text file."""
@ -74,7 +76,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]
six.reraise(ValueError(errstr), None, exc_info()[2])
self.messagelist[uid] = self.msglist_item_initializer(uid)
self.messagelist[uid]['flags'] = flags
@ -97,7 +99,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]
six.reraise(ValueError(errstr), None, exc_info()[2])
self.messagelist[uid] = self.msglist_item_initializer(uid)
self.messagelist[uid]['flags'] = flags
self.messagelist[uid]['mtime'] = mtime

View File

@ -17,6 +17,9 @@
import os
from sys import exc_info
from threading import Lock
import six
try:
import sqlite3 as sqlite
except:
@ -68,15 +71,15 @@ class LocalStatusSQLiteFolder(BaseFolder):
self.connection = sqlite.connect(self.filename, check_same_thread=False)
except NameError:
# sqlite import had failed.
raise UserWarning("SQLite backend chosen, but cannot connect "
six.reraise(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), None, exc_info()[2])
except sqlite.OperationalError as e:
# Operation had failed.
raise UserWarning("cannot open database file '%s': %s.\nYou might "
six.reraise(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)), None, exc_info()[2])
# Make sure sqlite is in multithreading SERIALIZE mode.
assert sqlite.threadsafety == 1, 'Your sqlite is not multithreading safe.'

View File

@ -22,6 +22,9 @@ import os
from sys import exc_info
from .Base import BaseFolder
from threading import Lock
import six
try:
from hashlib import md5
except ImportError:
@ -309,8 +312,8 @@ class MaildirFolder(BaseFolder):
time.sleep(0.23)
continue
severity = OfflineImapError.ERROR.MESSAGE
raise OfflineImapError("Unique filename %s already exists."%
filename, severity), None, exc_info()[2]
six.reraise(OfflineImapError("Unique filename %s already exists."%
filename, severity), None, exc_info()[2])
else:
raise
@ -430,10 +433,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" % (
six.reraise(OfflineImapError("Can't rename file '%s' to '%s': %s" % (
oldfilename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER), \
None, exc_info()[2]
OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
self.messagelist[uid]['flags'] = flags
self.messagelist[uid]['filename'] = newfilename
@ -511,10 +513,10 @@ class MaildirFolder(BaseFolder):
try:
os.rename(filename, newfilename)
except OSError as e:
raise OfflineImapError(
six.reraise(OfflineImapError(
"Can't rename file '%s' to '%s': %s" % (
filename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER), None, exc_info()[2]
OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
elif match.group(1) != self._foldermd5:
self.ui.warn(("Inconsistent FMD5 for file `%s':"
" Neither `%s' nor `%s' found")

View File

@ -21,6 +21,8 @@ from offlineimap import OfflineImapError
from .IMAP import IMAPFolder
import os.path
import six
class MappedIMAPFolder(IMAPFolder):
"""IMAP class to map between Folder() instances where both side assign a uid
@ -61,8 +63,8 @@ class MappedIMAPFolder(IMAPFolder):
try:
line = line.strip()
except ValueError:
raise Exception("Corrupt line '%s' in UID mapping file '%s'"%
(line, mapfilename)), None, exc_info()[2]
six.reraise(Exception("Corrupt line '%s' in UID mapping file '%s'"%
(line, mapfilename)), None, exc_info()[2])
(str1, str2) = line.split(':')
loc = int(str1)
rem = int(str2)
@ -88,10 +90,10 @@ class MappedIMAPFolder(IMAPFolder):
try:
return [mapping[x] for x in items]
except KeyError as e:
raise OfflineImapError("Could not find UID for msg '{0}' (f:'{1}'."
six.reraise(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), None, exc_info()[2])
# Interface from BaseFolder
def cachemessagelist(self, min_date=None, min_uid=None):